def connect(self): dev = None # connect to the airplane while not dev: dev = find_device_by_name("TailorToys PowerUp") if dev: self.p = Peripheral() self.p.connect(dev.addr()) # locate interesting characteristics self.map_chars()
def blueGo(u0): # p = Peripheral() # p.advertise(device_name=name) def event_handler(id, handle, data): print("BLE event:", id, "handle:", handle) print(data) if id == constants.EVT_GAP_CONNECTED: Dragos.colorwheel() elif id == constants.EVT_GAP_DISCONNECTED: print("Disconnect") # disconnect #LED(2).off() #u0 = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e") #u0 = UUID(encodeName(badge)) u1 = UUID("6e400002-b5a3-f393-e0a9-e50e24dcca9e") u2 = UUID("6e400003-b5a3-f393-e0a9-e50e24dcca9e") s = Service(u0) c0 = Characteristic(u1, props=Characteristic.PROP_WRITE | Characteristic.PROP_WRITE_WO_RESP) c1 = Characteristic(u2, props=Characteristic.PROP_NOTIFY, attrs=Characteristic.ATTR_CCCD) s.addCharacteristic(c0) s.addCharacteristic(c1) p = Peripheral() p.addService(s) p.setConnectionHandler(event_handler) p.advertise(device_name="Dragos", services=[s])
def start(): adv_packet = generate_eddystone_adv_packet("micropython") p = Peripheral() p.advertise(data=adv_packet, connectable=False)
class PowerUp3: def __init__(self): self.x_adc = ADC(1) self.btn_speed_up = Pin("P13", mode=Pin.IN, pull=Pin.PULL_UP) self.btn_speed_down = Pin("P15", mode=Pin.IN, pull=Pin.PULL_UP) self.btn_speed_full = Pin("P14", mode=Pin.IN, pull=Pin.PULL_UP) self.btn_speed_off = Pin("P16", mode=Pin.IN, pull=Pin.PULL_UP) self.x_mid = 0 self.calibrate() self.connect() self.loop() def read_stick_x(self): return self.x_adc.value() def button_speed_up(self): return not bool(self.btn_speed_up.value()) def button_speed_down(self): return not bool(self.btn_speed_down.value()) def button_speed_full(self): return not bool(self.btn_speed_full.value()) def button_speed_off(self): return not bool(self.btn_speed_off.value()) def calibrate(self): self.x_mid = self.read_stick_x() def __str__(self): return "calibration x: %i, y: %i" % (self.x_mid) def map_chars(self): s = self.p.getServices() service_batt = s[3] service_control = s[4] self.char_batt_lvl = service_batt.getCharacteristics()[0] self.char_control_speed = service_control.getCharacteristics()[0] self.char_control_angle = service_control.getCharacteristics()[2] def battery_level(self): return int(self.char_batt_lvl.read()[0]) def speed(self, new_speed=None): if new_speed == None: return int(self.char_control_speed.read()[0]) else: self.char_control_speed.write(bytearray([new_speed])) def angle(self, new_angle=None): if new_angle == None: return int(self.char_control_angle.read()[0]) else: self.char_control_angle.write(bytearray([new_angle])) def connect(self): dev = None # connect to the airplane while not dev: dev = find_device_by_name("TailorToys PowerUp") if dev: self.p = Peripheral() self.p.connect(dev.addr()) # locate interesting characteristics self.map_chars() def rudder_center(self): if self.old_angle != 0: self.old_angle = 0 self.angle(0) def rudder_left(self, angle): steps = (angle // self.interval_size_left) new_angle = 60 - steps if self.old_angle != new_angle: self.angle(new_angle) self.old_angle = new_angle def rudder_right(self, angle): steps = (angle // self.interval_size_right) new_angle = -steps if self.old_angle != new_angle: self.angle(new_angle) self.old_angle = new_angle def throttle(self, speed): if (speed > 200): speed = 200 elif (speed < 0): speed = 0 if self.old_speed != speed: self.speed(speed) self.old_speed = speed def loop(self): adc_threshold = 10 right_threshold = self.x_mid + adc_threshold left_threshold = self.x_mid - adc_threshold self.interval_size_left = self.x_mid // 60 self.interval_size_right = (255 - self.x_mid) // 60 self.old_angle = 0 self.old_speed = 0 while True: time.sleep_ms(100) # read out new angle new_angle = self.read_stick_x() if (new_angle < 256): if (new_angle > right_threshold): self.rudder_right(new_angle - self.x_mid) elif (new_angle < left_threshold): self.rudder_left(new_angle) else: self.rudder_center() # read out new speed new_speed = self.old_speed if self.button_speed_up(): new_speed += 25 elif self.button_speed_down(): new_speed -= 25 elif self.button_speed_full(): new_speed = 200 elif self.button_speed_off(): new_speed = 0 else: pass self.throttle(new_speed)
def event_handler(id, handle, data): global periph global service if id == constants.EVT_GAP_CONNECTED: pass elif id == constants.EVT_GAP_DISCONNECTED: # restart advertisment periph.advertise(device_name="Nano Blinky", services=[service]) elif id == constants.EVT_GATTS_WRITE: LED(1).on() if int(data[0]) else LED(1).off() # start off with LED(1) off LED(1).off() notif_enabled = False uuid_service = UUID("0x1523") uuid_led = UUID("0x1525") service = Service(uuid_service) char_led = Characteristic(uuid_led, props=Characteristic.PROP_WRITE) service.addCharacteristic(char_led) periph = Peripheral() periph.addService(service) periph.setConnectionHandler(event_handler) periph.advertise(device_name="Nano Blinky", services=[service]) while (True): time.sleep_ms(500)
# measure chip temperature temp = Temp.read() temp = temp * 100 char_temp.write(bytearray([temp & 0xFF, temp >> 8])) # start off with LED(1) off LED(1).off() # use RTC1 as RTC0 is used by bluetooth stack # set up RTC callback every 5 second rtc = RTC(1, period=5, mode=RTC.PERIODIC, callback=send_temp) notif_enabled = False uuid_env_sense = UUID("0x181A") # Environmental Sensing service uuid_temp = UUID("0x2A6E") # Temperature characteristic serv_env_sense = Service(uuid_env_sense) temp_props = Characteristic.PROP_NOTIFY | Characteristic.PROP_READ temp_attrs = Characteristic.ATTR_CCCD char_temp = Characteristic(uuid_temp, props=temp_props, attrs=temp_attrs) serv_env_sense.addCharacteristic(char_temp) periph = Peripheral() periph.addService(serv_env_sense) periph.setConnectionHandler(event_handler) periph.advertise(device_name="micr_temp", services=[serv_env_sense])
else: notif_enabled = False # start off with LED(1) off LED(1).off() notif_enabled = False uuid_service = UUID("0x181A") # Environmental Sensing service uuid_temp = UUID("0x2A6E") # Temperature characteristic service = Service(uuid_service) temp_props = Characteristic.PROP_READ | Characteristic.PROP_NOTIFY temp_attrs = Characteristic.ATTR_CCCD temp_char = Characteristic(uuid_temp, props=temp_props, attrs=temp_attrs) service.addCharacteristic(temp_char) periph = Peripheral() periph.addService(service) periph.setConnectionHandler(event_handler) periph.advertise(device_name="Temperature Sensor", services=[service]) bus = I2C(1, scl=Pin(15), sda=Pin(14)) hts = hts221.HTS221(bus) while (True): if notif_enabled: temp = int(hts.temperature() * 100) temp_char.write(bytearray([temp & 0xFF, temp >> 8])) time.sleep_ms(100)