Skip to content
Snippets Groups Projects
Commit 02cf84a1 authored by Paul-Winpenny's avatar Paul-Winpenny
Browse files

Added Pico Scripts

parent a052b055
No related branches found
No related tags found
1 merge request!1App now has a basic structure and BLE support
......@@ -5,3 +5,5 @@
/App/RobobinApp/bin/
/App/RobobinApp/obj/
.DS_Store
/App/RobobinApp/<AndroidSdkPath>/
/App/RobobinApp/<JavaSdkPath>/
\ No newline at end of file
import ubluetooth
import struct
import time
class BLEAdvertising:
def __init__(self):
self.ble = ubluetooth.BLE()
self.ble.active(True)
self.ble.irq(self.ble_irq)
self.register_services()
self.advertising_payload = self.generate_advertising_payload("RPi Pico")
self.ble.gap_advertise(100_000, self.advertising_payload)
def ble_irq(self, event, data):
if event == 1:
print("Device connected")
elif event == 2:
print("Device disconnected")
self.ble.gap_advertise(100_000, self.advertising_payload)
elif event == 3:
conn_handle, attr_handle = data
value = self.ble.gatts_read(self.rx_handle)
print("Received message:", value.decode('utf-8'))
def generate_advertising_payload(self, name):
name_bytes = bytes(name, 'utf-8')
payload = bytearray()
payload.extend(struct.pack('BB', 0x02, 0x01))
payload.append(0x06)
payload.extend(struct.pack('BB', len(name_bytes) + 1, 0x09))
payload.extend(name_bytes)
return payload
def register_services(self):
UART_SERVICE_UUID = ubluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
UART_TX_UUID = ubluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
UART_RX_UUID = ubluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
self.rx_char = (UART_RX_UUID, ubluetooth.FLAG_WRITE)
self.tx_char = (UART_TX_UUID, ubluetooth.FLAG_NOTIFY)
UART_SERVICE = (UART_SERVICE_UUID, (self.tx_char, self.rx_char))
self.services = (UART_SERVICE,)
((self.tx_handle, self.rx_handle),) = self.ble.gatts_register_services(self.services)
ble_adv = BLEAdvertising()
while True:
time.sleep(1)
import time
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
from machine import Pin
from pimoroni import Button
pico_display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=180)
width, height = pico_display.get_bounds()
background_color = pico_display.create_pen(0, 0, 0)
text_color = pico_display.create_pen(255, 255, 255)
text_options = [
("Home", (0, 0)),
("Station 1", (10, 10)),
("Station 2", (20, 20)),
("Station 3", (30, 30))
]
current_index = 0
scroll_x = width
pico_display.set_font("sans")
button_pins = [12, 13, 14, 15]
buttons = [Button(pin) for pin in button_pins]
while True:
for index, button in enumerate(buttons):
if button.read() == True:
if index == 0:
current_index = (current_index + 1) % len(text_options)
print(f"Cycled to: {text_options[current_index][0]}")
elif index == 2:
current_index = (current_index - 1) % len(text_options)
print(f"Cycled to: {text_options[current_index][0]}")
elif index == 1:
selected_text, position = text_options[current_index]
print(f"Sending to {selected_text}, {position}")
text, position = text_options[current_index]
max_width = width - 20
scale = 2
while True:
text_width = pico_display.measure_text(text, scale)
if text_width <= max_width:
break
scale -= 1
if scale < 1:
scale = 1
break
pico_display.set_pen(background_color)
pico_display.clear()
pico_display.set_pen(text_color)
text_width = pico_display.measure_text(text, scale)
text_x = (width - text_width) // 2
text_y = (height // 2) - (scale * 4)
pico_display.text(text, text_x, text_y, width, scale)
pico_display.update()
scroll_x -= 2
if scroll_x < -text_width:
scroll_x = width
time.sleep(0.05)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment