1) CircuitPython code for Adafruit UART Ranging module P4665
by Al Pierce
and...
2) 5-Button Keyboard to phone via BLE (using itsybitsy rather than feather)
Adafruit CircuitPython 6.3.0 on 2021-06-01; Adafruit ItsyBitsy nRF52840 Express with nRF52840
I've gotten each section to function properly when I comment out the other, but could use some clues as to how to pass the value 'distanceft' into the BLE portion so it is sent to the phone when button 5 is pressed, or all the time really(it's jumpered).
I'm assuming it's something like I have two loops and I don't know how to connect them???
Help please; thanks
Code: Select all
# CircuitPython code for Adafruit UART Ranging module P4665
# Al Pierce Sept 9, 2020
# Based on Adafruit CircuitPython_UART.py example code
import board
import busio
import digitalio
import time
import adafruit_ble
from digitalio import DigitalInOut, Direction
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from adafruit_ble.services.standard.device_info import DeviceInfoService
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# used for sleep function (but don't use pause function; causes accum count errors -R
# import time
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
uart = busio.UART(board.TX, board.RX, baudrate=9600)
while True:
data = uart.read(32) # read up to 32 bytes
if data is not None:
led.value = True
# convert bytearray to distance in mm
distancemm = data[1] * 256 + data[2]
# print distance in mm
print(distancemm, "mm")
# compute distance mm to cm
distancecm = distancemm/10
# print distance in cm
print(distancecm, "cm")
# compute distance cm to inches
distancein = distancecm/2.54
# print distance in inches
print(distancein, "inches")
# compute distance inches to feet
distanceft = distancein/12
# print distance in feet
print(distanceft, "feet")
print("")
led.value = False
# SPlit Here ==========================================================================
# Split Here ==========================================================================
# This example acts as a BLE single key "keyboard" to peer devices (iPhone),
# for testing purposes.
# Attach five buttons (with pullup resistors) to itsybitsy nRF52840
# button 5(a jumper)will send a string "feet" to mobile device or computer
# import items were here; moved up to beginning
# removed button 1-4 code pin & direction since not needed for testing
button_5 = DigitalInOut(board.D5)
button_5.direction = Direction.INPUT
hid = HIDService()
device_info = DeviceInfoService(software_revision=adafruit_ble.__version__,
manufacturer="Adafruit Industries")
advertisement = ProvideServicesAdvertisement(hid)
advertisement.appearance = 961
scan_response = Advertisement()
scan_response.complete_name = "CircuitPython HID"
ble = adafruit_ble.BLERadio()
if not ble.connected:
print("advertising")
ble.start_advertising(advertisement, scan_response)
else:
print("already connected")
print(ble.connections)
k = Keyboard(hid.devices)
kl = KeyboardLayoutUS(k)
while True:
while not ble.connected:
pass
print("Start typing:")
while ble.connected:
if not button_5.value:
kl.write("feet")
time.sleep(3.0)
ble.start_advertising(advertisement)