Commit 4ef8fd23 authored by Klint Youngmeyer's avatar Klint Youngmeyer
Browse files

Add timer that updates the read attributes at 2Hz. One has the value of SW1,...

Add timer that updates the read attributes at 2Hz. One has the value of SW1, the other has a RNG value.
parent 7b97850c
Pipeline #18 failed with stages
import pyb
import network
import micropython
class BleChat:
def __init__(self, name):
self.name = name
self.local_name = None
self.local_mac = None
self.write_handle = None
self.read_sw_handle = None
self.read_rand_handle = None
self.rs = network.RS911XBT(pyb.SPI(2), pyb.Pin("I0"), pyb.Pin("E3"), pyb.Pin("E4"))
self.rs = network.RS9116()
self.rs.init()
self.lcd = pyb.LCD()
self.led = pyb.LED(1)
self.tim = pyb.Timer(4)
self._set_attr = self._set_attr_method
self._hardware_setup()
self._radio_setup()
self._timer_setup()
def _timer_setup(self):
self.tim.init(freq=2)
self.tim.callback(self._timer_callback)
def _timer_callback(self, tim):
micropython.schedule(self._set_attr, 0)
def _set_attr_method(self, _):
self.rs.set_attr_value(self.read_sw_handle, "Pressed" if pyb.Switch(1).value() else "Not Pressed")
self.rs.set_attr_value(self.read_rand_handle, "{}".format(pyb.rng()))
def _radio_setup(self):
handle = self.rs.add_service(0xaabb)
# write_attr_index = rs.add_attribute(handle, 0x1aa2, rs.ATT_PROPERTY_WRITE)
# read_attr_index = rs.add_attribute(handle, 0x2bb1, rs.ATT_PROPERTY_NOTIFY | rs.ATT_PROPERTY_READ)
self.rs.add_attribute(handle, 0x1aa2, self.rs.ATT_PROPERTY_WRITE)
self.rs.add_attribute(handle, 0x2bb1, self.rs.ATT_PROPERTY_NOTIFY | self.rs.ATT_PROPERTY_READ)
self.write_handle = self.rs.add_attribute(handle, 0x1aa2, self.rs.ATT_PROPERTY_WRITE)
self.read_sw_handle = self.rs.add_attribute(handle, 0x2bb1, self.rs.ATT_PROPERTY_NOTIFY | self.rs.ATT_PROPERTY_READ)
self.read_rand_handle = self.rs.add_attribute(handle, 0x3cc4, self.rs.ATT_PROPERTY_READ)
self.rs.set_antenna(self.rs.INTERNAL_ANTENNA)
self.rs.set_adv_data(self.name)
self.rs.set_scan_resp_data(self.name)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment