Commit 2dd62b80 authored by Klint Youngmeyer's avatar Klint Youngmeyer
Browse files

Rewrite blechat app to be class based.

Call with:
    import blechat
    ble = blechat.BleChat('periph name')
    ble.start()
Then connect to 'periph name' on BLE android app
parent 9dd93ad1
import pyb
import network
def blechat(name):
rs = network.RS911XBT(pyb.SPI(2), pyb.Pin("I0"), pyb.Pin("E3"), pyb.Pin("E4"))
# rs.setup_peripheral()
handle = 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)
rs.add_attribute(handle, 0x1aa2, rs.ATT_PROPERTY_WRITE)
rs.add_attribute(handle, 0x2bb1, rs.ATT_PROPERTY_NOTIFY | rs.ATT_PROPERTY_READ)
rs.set_antenna(rs.INTERNAL_ANTENNA)
rs.set_adv_data(name)
rs.set_scan_resp_data(name)
rs.set_local_name('{} Peripheral'.format(name))
print('Local Device MAC Address = {}'.format(rs.get_local_addr()))
print('Local Device Name = {}'.format(rs.get_local_name()))
rs.advertise(True)
lcd = pyb.LCD()
lcd.light(True)
lcd.fill(0)
lcd.show()
led = pyb.LED(1)
led.off()
class BleChat:
def __init__(self, name):
self.name = name
self.local_name = None
self.local_mac = None
print("Waiting for connection...")
self.rs = network.RS911XBT(pyb.SPI(2), pyb.Pin("I0"), pyb.Pin("E3"), pyb.Pin("E4"))
self.lcd = pyb.LCD()
self.led = pyb.LED(1)
done = False
while not done:
event = rs.get_event()
if event == rs.NO_EVENT:
rs.clear_event(event)
continue
print("Event: {}".format(event))
if event == rs.CONNECT_EVENT:
print('Connected')
addr = rs.get_remote_addr()
print('Remote Device MAC Address = {}'.format(addr))
print('Remote Device RSSI = {}'.format(rs.get_rssi(addr)))
print('Remote Device State = {}'.format(rs.get_device_status()))
elif event == rs.DISCONNECT_EVENT:
print('Disconnected')
print('Remote Device State = {}'.format(rs.get_device_status()))
done = True
elif event == rs.WRITE_EVENT:
event_rsp = rs.get_event_data(event)
print('Write event')
print('Write Data = {}'.format(event_rsp))
if event_rsp[0] in ('\x00\x00', '\x01\x00'):
if event_rsp[0] is '\x00\x00':
led.off()
self._hardware_setup()
self._radio_setup()
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.rs.set_antenna(self.rs.INTERNAL_ANTENNA)
self.rs.set_adv_data(self.name)
self.rs.set_scan_resp_data(self.name)
self.rs.set_local_name('{} Peripheral'.format(self.name))
self.local_mac = self.rs.get_local_addr()
self.local_name = self.rs.get_local_name()
print('Local Device MAC Address = {}'.format(self.local_mac))
print('Local Device Name = {}'.format(self.local_name))
self.rs.advertise(True)
# rs.setup_peripheral()
def _hardware_setup(self):
self.lcd.light(True)
self.lcd.fill(0)
self.lcd.show()
self.led.off()
def clear_lcd(self):
for i in range(0, 4):
i += 1
self.lcd.write(' \n')
self.lcd.fill(0)
self.lcd.show()
pyb.delay(50)
def start(self):
print("Waiting for connection...")
self.clear_lcd()
self.lcd.write('Connect to:\n')
self.lcd.write('{}\n'.format(self.name))
self.lcd.write('{}\n'.format(self.local_mac[0:17]))
done = False
while not done:
event = self.rs.get_event()
if event == self.rs.NO_EVENT:
self.rs.clear_event(event)
continue
print("Event: {}".format(event))
if event == self.rs.CONNECT_EVENT:
print('Connected')
addr = self.rs.get_remote_addr()
print('Remote Device MAC Address = {}'.format(addr))
print('Remote Device RSSI = {}'.format(self.rs.get_rssi(addr)))
print('Remote Device State = {}'.format(self.rs.get_device_status()))
self.clear_lcd()
self.lcd.write('Connected Dev:\n')
self.lcd.write('{}\n\n'.format(addr[0:17]))
elif event == self.rs.DISCONNECT_EVENT:
print('Disconnected')
print('Remote Device State = {}'.format(self.rs.get_device_status()))
self.clear_lcd()
self.lcd.write('Device\nDisconnected,\nPlease Reset\nCutiPy')
done = True
elif event == self.rs.WRITE_EVENT:
event_rsp = self.rs.get_event_data(event)
print('Write event')
print('Write Data = {}'.format(event_rsp))
if event_rsp[0] in ('\x00\x00', '\x01\x00'):
if event_rsp[0] == '\x00\x00':
self.led.off()
else:
self.led.on()
else:
led.on()
else:
lcd.write(event_rsp[0] + '\n')
self.lcd.write(event_rsp[0] + '\n')
# event_attr_index = rs.event_attr_hiandle()
# if event_attr_index == write_attr_ index:
# print('Write event')
# print('Write Data = {}'.format (rs.event_attr_value()))
# rs.set_attr_value(read_attr_index, rs.event_attr_value())
else:
print('Unexpected event {}'.format(event))
rs.clear_event(event)
# event_attr_index = rs.event_attr_hiandle()
# if event_attr_index == write_attr_ index:
# print('Write event')
# print('Write Data = {}'.format (rs.event_attr_value()))
# rs.set_attr_value(read_attr_index, rs.event_attr_value())
else:
print('Unexpected event {}'.format(event))
self.rs.clear_event(event)
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