uasyncio beep class

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

uasyncio beep class

Post by devnull » Thu Nov 29, 2018 10:31 am

Struggling to get started with uasyncio having never used it before.

I am attempting to create a beep class and this is my best guess on how to implement it, what am I doing wrong ??

gpio8266 is just a file containing pin number > name assignments.

Code: Select all

import uasyncio as asyncio
import gpio8266 as gpio

class Beep:
  
  def __init__(self):
    self.rep    = 0
    self.pause  = 50
    self.dur    = 10
    loop = asyncio.get_event_loop()
    loop.create_task(self.run())

  async def run(self):
    while True:
      for i in range(self.rep):
        gpio.bpin.value(1)
        await asyncio.sleep_ms(self.dur)
        gpio.bpin.value(0)
        await asyncio.sleep_ms(self.pause)
      self.rep = 0
  
  def go(self,rep=1,dur=10,pause=50):
    self.dur = dur
    self.pause = pause
    self.rep = rep

b = Beep()
async def test():
  while True:
    await asyncio.sleep_ms(2000)
    b.go(2)
 
loop = asyncio.get_event_loop()
loop.create_task(test())
loop.run_forever()

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: uasyncio beep class

Post by devnull » Thu Nov 29, 2018 12:06 pm

is it possible to create standalone classes with asyncio that can then be called outside of an asyncio loop or does it mean that if you want to use an asyncio class then the entire code needs to be based on asyncio ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio beep class

Post by pythoncoder » Fri Nov 30, 2018 5:57 am

See my answer to your query on GitHub.

Regarding your code above, you're on the right lines but there is some confusion about when .run() executes. In my view it shouldn't run continuously but only when awaited. The code can be simplified thus (I haven't tested this):

Code: Select all

import uasyncio as asyncio
import gpio8266 as gpio

class Beep:
  
  async def run(self,rep=1,dur=10,pause=50):
    while True:
      for i in range(rep):
        gpio.bpin.value(1)
        await asyncio.sleep_ms(dur)
        gpio.bpin.value(0)
        await asyncio.sleep_ms(pause)
  
b = Beep()

async def test():
  while True:
    await asyncio.sleep_ms(2000)
    await b.run(2)
 
loop = asyncio.get_event_loop()
loop.create_task(test())
loop.run_forever()
In this incarnation the class is clearly redundant and run() could simply be a function.

As a general point I doubt that beep() will produce good audio, especially in the context of a more complex uasyncio application. This is because the delays in uasyncio.sleep_ms() are highly variable: the time is a minimum. As context switching occurs when it sleeps, the maximum time depends on the number and behaviour of other coroutines.
Peter Hinch
Index to my micropython libraries.

Post Reply