From 02cf84a1d10b59c3f0be7915e7b3d1df740b799e Mon Sep 17 00:00:00 2001
From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com>
Date: Thu, 17 Oct 2024 19:13:02 +0100
Subject: [PATCH] Added Pico Scripts

---
 .gitignore                       |  2 +
 ButtonControls/ bluetoothTest.py | 47 ++++++++++++++++++++++
 ButtonControls/Workstations.py   | 68 ++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 ButtonControls/ bluetoothTest.py
 create mode 100644 ButtonControls/Workstations.py

diff --git a/.gitignore b/.gitignore
index fbcede46..f0d5dd17 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,5 @@
 /App/RobobinApp/bin/
 /App/RobobinApp/obj/
 .DS_Store
+/App/RobobinApp/<AndroidSdkPath>/
+/App/RobobinApp/<JavaSdkPath>/
\ No newline at end of file
diff --git a/ButtonControls/ bluetoothTest.py b/ButtonControls/ bluetoothTest.py
new file mode 100644
index 00000000..febbc745
--- /dev/null
+++ b/ButtonControls/ bluetoothTest.py	
@@ -0,0 +1,47 @@
+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)
diff --git a/ButtonControls/Workstations.py b/ButtonControls/Workstations.py
new file mode 100644
index 00000000..73a88b66
--- /dev/null
+++ b/ButtonControls/Workstations.py
@@ -0,0 +1,68 @@
+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)
-- 
GitLab