Last active
May 1, 2016 08:48
-
-
Save robo8080/afdebe12e566eb3f7b099dfe77332843 to your computer and use it in GitHub Desktop.
Genuino101BlinkLED.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# for iOS + Pythonista | |
import cb | |
import time | |
class MyCentralManagerDelegate (object): | |
def __init__(self): | |
self.peripheral = None | |
self.toggle = False | |
def did_discover_peripheral(self, p): | |
print '+++ Discovered peripheral: %s (%s)' % (p.name, p.uuid) | |
if p.name and 'GENUINO 101-0000' in p.name and not self.peripheral: | |
# Keep a reference to the peripheral, so it doesn't get garbage-collected: | |
self.peripheral = p | |
cb.connect_peripheral(self.peripheral) | |
def did_connect_peripheral(self, p): | |
print '*** Connected: %s' % p.name | |
print 'Discovering services...' | |
p.discover_services() | |
def did_fail_to_connect_peripheral(self, p, error): | |
print 'Failed to connect' | |
def did_disconnect_peripheral(self, p, error): | |
print 'Disconnected, error: %s' % (error,) | |
self.peripheral = None | |
def did_discover_services(self, p, error): | |
for s in p.services: | |
if '19B10000-E8F2-537E-4F6C-D104768A1214' in s.uuid: | |
print '+++ LED Service' | |
p.discover_characteristics(s) | |
def did_discover_characteristics(self, s, error): | |
if '19B10000-E8F2-537E-4F6C-D104768A1214' in s.uuid: | |
for c in s.characteristics: | |
if '19B10001-E8F2-537E-4F6C-D104768A1214' in c.uuid: | |
print 'LED On...' | |
self.peripheral.write_characteristic_value(c, chr(0x01), True) | |
def did_write_value(self, c, error): | |
time.sleep(1) | |
if self.toggle : | |
self.peripheral.write_characteristic_value(c, chr(0x01), True) | |
print 'LED On...' | |
else: | |
self.peripheral.write_characteristic_value(c, chr(0x00), True) | |
print 'LED Off...' | |
self.toggle = not self.toggle | |
delegate = MyCentralManagerDelegate() | |
print 'Scanning for peripherals...' | |
cb.set_central_delegate(delegate) | |
cb.scan_for_peripherals() | |
# Keep the connection alive until the 'Stop' button is pressed: | |
try: | |
while True: pass | |
except KeyboardInterrupt: | |
# Disconnect everything: | |
cb.reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment