summaryrefslogtreecommitdiff
path: root/ports/nrf/examples
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2021-03-16 12:21:50 -0500
committerJeff Epler <jepler@gmail.com>2021-03-16 12:21:50 -0500
commit58679dc038e475d7cd00ee45256b7f906ecc2240 (patch)
tree30cbc3f9e7e4efc81a43f364d554f33c1df34169 /ports/nrf/examples
parent97b6664201de2530c9b92957a5e8d2a68524fa7e (diff)
parentbc690d4070c69e21d9070badc9f56a15d62ffb5d (diff)
Merge remote-tracking branch 'origin/main' into bitmap-read-2
Diffstat (limited to 'ports/nrf/examples')
-rw-r--r--ports/nrf/examples/ubluepy_eddystone.py28
-rw-r--r--ports/nrf/examples/ubluepy_scan.py8
-rw-r--r--ports/nrf/examples/ubluepy_temp.py11
3 files changed, 32 insertions, 15 deletions
diff --git a/ports/nrf/examples/ubluepy_eddystone.py b/ports/nrf/examples/ubluepy_eddystone.py
index 426a4aa55..96818d01c 100644
--- a/ports/nrf/examples/ubluepy_eddystone.py
+++ b/ports/nrf/examples/ubluepy_eddystone.py
@@ -1,13 +1,16 @@
from ubluepy import Peripheral, constants
-BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE = const(0x02)
-BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED = const(0x04)
+BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE = const(0x02)
+BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED = const(0x04)
-BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE = const(BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED)
+BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE = const(
+ BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED
+)
+
+EDDYSTONE_FRAME_TYPE_URL = const(0x10)
+EDDYSTONE_URL_PREFIX_HTTP_WWW = const(0x00) # "http://www".
+EDDYSTONE_URL_SUFFIX_DOT_COM = const(0x01) # ".com"
-EDDYSTONE_FRAME_TYPE_URL = const(0x10)
-EDDYSTONE_URL_PREFIX_HTTP_WWW = const(0x00) # "http://www".
-EDDYSTONE_URL_SUFFIX_DOT_COM = const(0x01) # ".com"
def string_to_binarray(text):
b = bytearray([])
@@ -15,6 +18,7 @@ def string_to_binarray(text):
b.append(ord(c))
return b
+
def gen_ad_type_content(ad_type, data):
b = bytearray(1)
b.append(ad_type)
@@ -22,6 +26,7 @@ def gen_ad_type_content(ad_type, data):
b[0] = len(b) - 1
return b
+
def generate_eddystone_adv_packet(url):
# flags
disc_mode = bytearray([BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE])
@@ -29,10 +34,12 @@ def generate_eddystone_adv_packet(url):
# 16-bit uuid
uuid = bytearray([0xAA, 0xFE])
- packet_uuid16 = gen_ad_type_content(constants.ad_types.AD_TYPE_16BIT_SERVICE_UUID_COMPLETE, uuid)
+ packet_uuid16 = gen_ad_type_content(
+ constants.ad_types.AD_TYPE_16BIT_SERVICE_UUID_COMPLETE, uuid
+ )
# eddystone data
- rssi = 0xEE # -18 dB, approx signal strength at 0m.
+ rssi = 0xEE # -18 dB, approx signal strength at 0m.
eddystone_data = bytearray([])
eddystone_data.append(EDDYSTONE_FRAME_TYPE_URL)
eddystone_data.append(rssi)
@@ -42,7 +49,9 @@ def generate_eddystone_adv_packet(url):
# service data
service_data = uuid + eddystone_data
- packet_service_data = gen_ad_type_content(constants.ad_types.AD_TYPE_SERVICE_DATA, service_data)
+ packet_service_data = gen_ad_type_content(
+ constants.ad_types.AD_TYPE_SERVICE_DATA, service_data
+ )
# generate advertisement packet
packet = bytearray([])
@@ -52,6 +61,7 @@ def generate_eddystone_adv_packet(url):
return packet
+
def start():
adv_packet = generate_eddystone_adv_packet("micropython")
p = Peripheral()
diff --git a/ports/nrf/examples/ubluepy_scan.py b/ports/nrf/examples/ubluepy_scan.py
index c0a7d05a3..37daa6c69 100644
--- a/ports/nrf/examples/ubluepy_scan.py
+++ b/ports/nrf/examples/ubluepy_scan.py
@@ -1,21 +1,24 @@
from ubluepy import Scanner, constants
+
def bytes_to_str(bytes):
string = ""
for b in bytes:
string += chr(b)
return string
+
def get_device_names(scan_entries):
dev_names = []
for e in scan_entries:
scan = e.getScanData()
if scan:
for s in scan:
- if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME:
- dev_names.append((e, bytes_to_str(s[2])))
+ if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME:
+ dev_names.append((e, bytes_to_str(s[2])))
return dev_names
+
def find_device_by_name(name):
s = Scanner()
scan_res = s.scan(100)
@@ -25,6 +28,7 @@ def find_device_by_name(name):
if name == dev[1]:
return dev[0]
+
# >>> res = find_device_by_name("micr")
# >>> if res:
# ... print("address:", res.addr())
diff --git a/ports/nrf/examples/ubluepy_temp.py b/ports/nrf/examples/ubluepy_temp.py
index 405f77c4b..c70235c4a 100644
--- a/ports/nrf/examples/ubluepy_temp.py
+++ b/ports/nrf/examples/ubluepy_temp.py
@@ -26,6 +26,7 @@ from pyb import LED
from machine import RTC, Temp
from ubluepy import Service, Characteristic, UUID, Peripheral, constants
+
def event_handler(id, handle, data):
global rtc
global periph
@@ -55,6 +56,7 @@ def event_handler(id, handle, data):
# stop low power timer
rtc.stop()
+
def send_temp(timer_id):
global notif_enabled
global char_temp
@@ -62,9 +64,10 @@ def send_temp(timer_id):
if notif_enabled:
# measure chip temperature
temp = Temp.read()
- temp = temp * 100
+ temp = temp * 100
char_temp.write(bytearray([temp & 0xFF, temp >> 8]))
+
# start off with LED(1) off
LED(1).off()
@@ -74,14 +77,14 @@ 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
+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)
+char_temp = Characteristic(uuid_temp, props=temp_props, attrs=temp_attrs)
serv_env_sense.addCharacteristic(char_temp)