import time
from bluezero import microbit
from keyboardRead import NonBlockingConsole
# Set the address of the Pi Bluetooth controler
adapter='DC:A6:32:9D:C9:74'
devices={}
filepath = './microbits.cnf'
print ("Initialising microbits found in ", filepath)
try:
# Read list of Microbit MAC addresses and attempt to connect to each
with open(filepath) as fp:
line = fp.readline()
while line:
key=line.strip()
print ("..Device: [",key.strip(),"]")
try:
# Define microbit object
print ("....Initialising")
devices[key] = microbit.Microbit(adapter_addr=adapter,
device_addr=key,
accelerometer_service=True,
button_service=False,
led_service=True,
magnetometer_service=False,
pin_service=True,
temperature_service=True)
print ("....Initialised")
# Connect to microbit
print ("....Connecting")
devices[key].connect()
print ("....Connected")
devices[key].set_pin(5, True, False)
devices[key].set_pin(11, True, False)
devices[key].set_pin(15, True, False)
devices[key].set_pin(14, True, False)
devices[key].set_pin(13, True, False)
devices[key].set_pin(12, True, False)
devices[key].set_pin(1, True, True)
devices[key].set_pin(2, True, True)
devices[key].set_pin(8, True, False)
except Exception as e:
# If initilisation or connection failed remove this device from device list
print(e)
print ("....Initialisation or Connection failed")
devices.pop(key, None)
line=fp.readline()
except:
print("Failed to open config file")
exit()
# Check that at least one device is connected to - if not exit
if (devices is None or len(devices)==0):
print ("No connected devices - exiting")
exit()
# Loop whilst listening for keyboard input (on the Pi)
with NonBlockingConsole() as nbc:
looping = True
while looping:
# Check each device in turn - getting the unique key of each device
for dKey in devices:
# If the escape key is pressed on the PI then break
keyboardVal = nbc.get_data()
if keyboardVal == '\x1b': # x1b is ESC
print ("Raspberry Pi Escape key pressed")
looping=False
elif keyboardVal == 'm':
msg = input("Enter message to output to all Pis:")
for d in devices:
devices[d].text=msg
# If the A button is pressed - exit the loop (a kill switch)
#if (devices[dKey].button_a > 0):
# print("Device [", dKey, "] Button A Pressed" )
# #looping=False
# Print the temperature from the device
print('Temperature [', dKey, '] = ', devices[dKey].temperature)
print('Accelerometer [', dKey, '] = ', devices[dKey].accelerometer)
#print("Device [", dKey, "] Button A = " , devices[dKey].button_a)
#print("Device [", dKey, "] Button B = " , devices[dKey].button_b)
print("Device [", dKey, "] Pin = " , devices[dKey].pin_values)
time.sleep(1)
# Tidy up by disconnecting from all devices
print ("Disconnecting from all devices")
for dKey in devices:
print ("..Device [",dKey,"]")
print ("....Disconnecting")
devices[key].disconnect()
print ("....Disconnected")