Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/race_behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,47 @@ def onDraw(self):
self.ctx.strip.setPixelColor(i,Color(*self.ctx.baseColor))
self.ctx.strip.show()

@Associate("fullstop")
class StopItNow(Behaviour):
index = 0
goingUp = 1
power_ratio = 0
def onEnter(self):
getLogger().getChild(LOG_TAG).debug("Enter %s" % (type(self).__name__))
self.color = Color(0,0,0)
self.launchSubtask(self.update())

#TODO Lifetime issue here as this task could outlive the parent. Add Awaitable subtasks?
self.loop.create_task(self.ctx.throttle_ctrl.lerpThrottle(self.power_ratio, 1))

def onExit(self):
getLogger().getChild(LOG_TAG).debug("Exit %s" % (type(self).__name__))

async def onPreExit(self):
getLogger().getChild(LOG_TAG).debug("ExitLERP %s" % (type(self).__name__))
await self.loop.create_task(
self.ctx.throttle_ctrl.lerpThrottle(self.ctx.throttle_ctrl.BASE_RATIO, 0.5)
)

def onDraw(self):
for i in range(self.ctx.strip.numPixels()):
c = self.color
self.ctx.strip.setPixelColor(i,c)
self.ctx.strip.show()

async def update(self):
while True:
await asyncio.sleep(0.1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works great, with co-routines we can simplify things by removing the conditional logic. In a coroutine when you 'await' you are suspending the function, so it resumes where it left off.

async def update():
    n = 255
    while True:
        for x in range(n+1):
            await asyncio.sleep(0.1)
            self.color = Color(x,0,0)
        for x in range(n-1,0,-1):
            await asyncio.sleep(0.1)
            self.color = Color(x,0,0)

Better yet we could also do something like if you wanted to reuse the functionality.

async def update():
    async for x in thereAndBackIncrementor(0,10):
        self.color = Color(x,0,0)
        await asyncio.sleep(0.1)


async def thereAndBackIncrementor(firstVal,lastVal):
    for x in range(lastVal + 1):
        yield x
    for x in range(lastVal - 1, firstVal, -1):
        yield x

if (self.goingUp==1):
self.index +=1
if (self.index==255):
self.goingUp = 0
if (self.goingUp==0):
self.index -=1
if (self.index==0):
self.goingUp = 1
self.color = Color(self.index,0,0)

@Associate("banana")
class BananaPowerup(Behaviour):
index = 0
Expand Down