uasyncio and display

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
IHOXOHI
Posts: 112
Joined: Sat Apr 25, 2020 7:31 am

Re: uasyncio and display

Post by IHOXOHI » Sun Jul 05, 2020 11:09 am

Yes!

It works!

So goood!!!

my "main" :

Code: Select all

import machine
import uasyncio as asyncio
from pms.pms5003 import PMS5003
import pms.pms5003
from time import sleep
import time

import epaper2in9
from pyb import SPI
import pyb

# SPI3 on Black STM32F407VET6
spi = SPI(2, SPI.MASTER, baudrate=2000000, polarity=0, phase=0)
cs = pyb.Pin('Y7')
dc = pyb.Pin('Y5')
rst = pyb.Pin('Y4')
busy = pyb.Pin('Y3')

e = epaper2in9.EPD(spi, cs, dc, rst, busy)
e.init()

w = 128
h = 296
x = 0
y = 0

import framebuf
buf = bytearray(128 * 296 // 8)
fb = framebuf.FrameBuffer(buf, 128, 296, framebuf.MONO_HLSB)
black = 0
white = 1


_DEFAULT_MS = 20

class Lock:
    def __init__(self):
        self._locked = False

    async def __aenter__(self):
        while True:
            if self._locked:
                await asyncio.sleep_ms(_DEFAULT_MS)
            else:
                self._locked = True
                break

    async def __aexit__(self, *args):
        self._locked = False
        await asyncio.sleep_ms(_DEFAULT_MS)

    def locked(self):
        return self._locked

    def release(self):
        self._locked = False



        
lock = Lock()
uart = machine.UART(4, baudrate=9600)
pm = PMS5003(uart, lock) 
pm.registerCallback(pm.print)


async def display_values():
    await asyncio.sleep(50) # wait for sensor to start correctly
    while True:
        PM25 = str(pm.pm25_standard)
        fb.fill(white)
        fb.text(PM25,5,5,black)
        e.set_frame_memory(buf, x, y, w, h)
        e.display_frame()
        await asyncio.sleep(30)
        #asyncio.run(display_values())
        
loop=asyncio.get_event_loop()
loop.run_until_complete(display_values())
#asyncio.run(display_values())

loop=asyncio.get_event_loop()
loop.run_forever()

I'm so grateful for your help...

THANKS.

All the best.

kevinkk525
Posts: 864
Joined: Sat Feb 03, 2018 7:02 pm

Re: uasyncio and display

Post by kevinkk525 » Sun Jul 05, 2020 2:43 pm

I'm glad it works now!

by the way: the last 2 lines are not needed in your code:

Code: Select all

import machine
import uasyncio as asyncio
from pms.pms5003 import PMS5003
import pms.pms5003
import time

import epaper2in9
from pyb import SPI
import pyb

# SPI3 on Black STM32F407VET6
spi = SPI(2, SPI.MASTER, baudrate=2000000, polarity=0, phase=0)
cs = pyb.Pin('Y7')
dc = pyb.Pin('Y5')
rst = pyb.Pin('Y4')
busy = pyb.Pin('Y3')

e = epaper2in9.EPD(spi, cs, dc, rst, busy)
e.init()

w = 128
h = 296
x = 0
y = 0

import framebuf
buf = bytearray(128 * 296 // 8)
fb = framebuf.FrameBuffer(buf, 128, 296, framebuf.MONO_HLSB)
black = 0
white = 1


_DEFAULT_MS = 20

class Lock:
    def __init__(self):
        self._locked = False

    async def __aenter__(self):
        while True:
            if self._locked:
                await asyncio.sleep_ms(_DEFAULT_MS)
            else:
                self._locked = True
                break

    async def __aexit__(self, *args):
        self._locked = False
        await asyncio.sleep_ms(_DEFAULT_MS)

    def locked(self):
        return self._locked

    def release(self):
        self._locked = False
        
lock = Lock()
uart = machine.UART(4, baudrate=9600)
pm = PMS5003(uart, lock) 
pm.registerCallback(pm.print)


async def display_values():
    await asyncio.sleep(50) # wait for sensor to start correctly
    while True:
        PM25 = str(pm.pm25_standard)
        fb.fill(white)
        fb.text(PM25,5,5,black)
        e.set_frame_memory(buf, x, y, w, h)
        e.display_frame()
        await asyncio.sleep(30)
        
loop=asyncio.get_event_loop()
loop.run_until_complete(display_values())
#asyncio.run(display_values())
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

IHOXOHI
Posts: 112
Joined: Sat Apr 25, 2020 7:31 am

Re: uasyncio and display

Post by IHOXOHI » Sun Jul 05, 2020 4:52 pm

Yip,

Data-saving works too with uasyncio (and pms (and display)).

Post Reply