SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. hardware_serial = ubinascii.hexlify(machine.unique_id()).decode()
  2. l99_serial = 'L99-{}'.format(hardware_serial)
  3.  
  4. def ble_connection_callback(is_connected):
  5.     if is_connected:
  6.         if _DEBUG: print('BLE connected')
  7.         pycom.rgbled(_LED_BLUE)
  8.     else:
  9.         if _DEBUG: print('BLE disconnected')
  10.         pycom.rgbled(_LED_GREEN)
  11.  
  12. ble = BLEGATTS()
  13. ble.init(advert_name=l99_serial,
  14.     advert_mfg_data='L99',
  15.     advert_service_data=None,
  16.     advert_service_uuid=6154,
  17.     connect_callback=ble_connection_callback)
  18.  
  19. info = uos.uname()
  20.  
  21. ble.addService(service_name='device_info',uuid=6154) \
  22.     .addReadChar (name='l99_serial', uuid=0, static_read=l99_serial) \
  23.     .addReadChar (name='hardware_serial', uuid=1, static_read=hardware_serial) \
  24.     .addReadChar (name='sysname', uuid=2, dynamic_read=lambda cn,id: info[0]) \
  25.     .addReadChar (name='nodename', uuid=3, dynamic_read=lambda cn,id: info[1]) \
  26.     .addReadChar (name='release', uuid=4, dynamic_read=lambda cn,id: info[2]) \
  27.     .addReadChar (name='version_number', uuid=5, dynamic_read=lambda cn,id: info[3].split(' on ')[0]) \
  28.     .addReadChar (name='version_date', uuid=6, dynamic_read=lambda cn,id: info[3].split(' on ')[1]) \
  29.     .addReadChar (name='machine', uuid=7, dynamic_read=lambda cn,id: info[4]) \
  30.     .addReadChar (name='lora_version', uuid=8, dynamic_read=lambda cn,id: info[5]) \
  31.     .addReadChar (name='sx_state', uuid=9, static_read=sx_init[0]) \
  32.     .addReadChar (name='sx_fault', uuid=10, static_read=sx_init[1]) \
  33.     .addReadChar (name='ads1_state', uuid=11, static_read=ads1_init[0]) \
  34.     .addReadChar (name='ads1_fault', uuid=12, static_read=ads1_init[1]) \
  35.     .addReadChar (name='ads2_state', uuid=13, static_read=ads2_init[0]) \
  36.     .addReadChar (name='ads2_fault', uuid=14, static_read=ads2_init[1]) \
  37.     .start()
  38.  
  39. dService = ble.addService(service_name='datapoint_monitor',uuid=9999)
  40. uuid_counter = 0
  41. uuid_hw_label_offset = 100
  42. uuid_user_label_offset = 200
  43. uuid_value_offset = 300
  44. char_hw_label_sfx = '+HL'
  45. char_user_label_sfx = '+UL'
  46. char_value_sfx = '+V'
  47.  
  48. dService.addReadChar(name='HLBase', uuid=1, static_read=uuid_hw_label_offset)
  49. dService.addReadChar(name='ULBase', uuid=2, static_read=uuid_user_label_offset)
  50. dService.addReadChar(name='VBase', uuid=3, static_read=uuid_value_offset)
  51.  
  52. for deviceKey, deviceDataPoints in sorted(dPool.devicePool.items()):
  53.     for datapointKey, datapoint in sorted(deviceDataPoints.items()):
  54.  
  55.         dPool.addKey(datapoint,uuid_counter+uuid_hw_label_offset)
  56.         dPool.addKey(datapoint,uuid_counter+uuid_user_label_offset)
  57.         dPool.addKey(datapoint,uuid_counter+uuid_value_offset)
  58.  
  59.         bleCharName = '{}+{}'.format(deviceKey, datapointKey)
  60.  
  61.         dService.addReadChar(name='{}{}'.format(bleCharName,char_hw_label_sfx), uuid=uuid_counter+uuid_hw_label_offset,
  62.             dynamic_read=lambda cn,id: dPool.getHardwareLabelName(id))
  63.  
  64.         dService.addReadWriteChar(name='{}{}'.format(bleCharName,char_user_label_sfx), uuid=uuid_counter+uuid_user_label_offset,
  65.             dynamic_read=lambda cn,id: dPool.getUserLabelName(id),
  66.             dynamic_write=lambda cn,id,val: dPool.setUserLabelName(id,val))
  67.  
  68.         ###if _DEBUG: print('BLEMAP 0x{:02x}/{:02d} => BLEUUID 0x{:02x} BLECHAR {}+V'.format(datapoint.bus_address, datapoint.hw_address,uuid_counter+uuid_value_offset, bleCharName))
  69.  
  70.         if datapoint.readonly:
  71.             if datapoint.datatype == 'analog':
  72.                 # HACK: gotta turn to string before sending out
  73.                 dService.addReadNotifyChar(name='{}{}'.format(bleCharName,char_value_sfx), uuid=uuid_counter+uuid_value_offset,
  74.                     dynamic_read=lambda cn,id: str(dPool.getHardwareValue(id)))
  75.             else:
  76.                 dService.addReadNotifyChar(name='{}{}'.format(bleCharName,char_value_sfx), uuid=uuid_counter+uuid_value_offset,
  77.                     dynamic_read=lambda cn,id: dPool.getHardwareValue(id))
  78.         else:
  79.             dService.addReadWriteNotifyChar(name='{}{}'.format(bleCharName,char_value_sfx), uuid=uuid_counter+uuid_value_offset,
  80.                 dynamic_read=lambda cn,id: dPool.getHardwareValue(id),
  81.                 dynamic_write=lambda cn,id,val: dPool.setHardwareValue(id,int.from_bytes(val,'little')))
  82.  
  83.         uuid_counter += 1
  84.  
  85. dService.start()
  86.  
  87. # update datapoints with current IO values
  88. if sx_init[0]:
  89.     for pin in range(0,16):
  90.         dPool.updateHardwareValue(sx_config.address,pin,sx_inst.digitalRead(pin))
  91.  
  92. ble.advertise()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top