Skip to content

Instantly share code, notes, and snippets.

@StanSvec
Created June 22, 2024 12:38
Show Gist options
  • Save StanSvec/fca41fd972053825e94eba8874c3e5df to your computer and use it in GitHub Desktop.
Save StanSvec/fca41fd972053825e94eba8874c3e5df to your computer and use it in GitHub Desktop.
Async SEN0395 Sensor
import asyncio
import serialio
from sensation.sen0395 import SensorAsync, PresenceHandlerAsync
class Printer:
async def __call__(self, p):
print(f"printer async: {p}")
async def main():
sensor = None
try:
serial_con = serialio.serial_for_url("serial:///dev/ttyAMA1", 115200)
sensor = SensorAsync("my_sensor", serial_con)
await serial_con.open()
presence_handler = PresenceHandlerAsync()
presence_handler.observers.append(Printer().__call__)
sensor.handlers.append(presence_handler)
await sensor.clear_buffer()
sensor.start_reading()
print(await sensor.configure_latency(0, 0))
await asyncio.sleep(5)
await sensor.stop_reading()
finally:
if sensor:
await sensor.close()
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment