From 0f308fd6f858d7a38f9cfe75872cb35cdac576b5 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 12 Dec 2024 15:13:32 +0000 Subject: [PATCH] Future module ideas --- lib/pimoroni_yukon/modules/__init__.py | 8 ++- lib/pimoroni_yukon/modules/mech_key.py | 45 +++++++++++++++ lib/pimoroni_yukon/modules/micro_sd.py | 64 +++++++++++++++++++++ lib/pimoroni_yukon/modules/potentiometer.py | 47 +++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 lib/pimoroni_yukon/modules/mech_key.py create mode 100644 lib/pimoroni_yukon/modules/micro_sd.py create mode 100644 lib/pimoroni_yukon/modules/potentiometer.py diff --git a/lib/pimoroni_yukon/modules/__init__.py b/lib/pimoroni_yukon/modules/__init__.py index 6c429e9..004b2fb 100644 --- a/lib/pimoroni_yukon/modules/__init__.py +++ b/lib/pimoroni_yukon/modules/__init__.py @@ -12,6 +12,9 @@ from .quad_servo_direct import QuadServoDirectModule from .quad_servo_reg import QuadServoRegModule from .serial_servo import SerialServoModule +from .micro_sd import MicroSDModule +from .mech_key import MechKeyModule +from .potentiometer import PotModule KNOWN_MODULES = ( @@ -24,4 +27,7 @@ ProtoPotModule, QuadServoDirectModule, QuadServoRegModule, - SerialServoModule) + SerialServoModule, + MicroSDModule, + MechKeyModule, + PotModule) diff --git a/lib/pimoroni_yukon/modules/mech_key.py b/lib/pimoroni_yukon/modules/mech_key.py new file mode 100644 index 0000000..0995bef --- /dev/null +++ b/lib/pimoroni_yukon/modules/mech_key.py @@ -0,0 +1,45 @@ +# SPDX-FileCopyrightText: 2025 Christopher Parrott for Pimoroni Ltd +# +# SPDX-License-Identifier: MIT + +from .common import YukonModule, ADC_FLOAT, ADC_HIGH, IO_HIGH, IO_LOW +from machine import Pin +from pimoroni import RGBLED + + +class MechKeyModule(YukonModule): + NAME = "Mechanical Key + RGB" + + # | ADC1 | ADC2 | SLOW1 | SLOW2 | SLOW3 | Module | Condition (if any) | + # |-------|-------|-------|-------|-------|----------------------|-----------------------------| + # | HIGH | FLOAT | 1 | 0 | 0 | Mechanical Key | | + @staticmethod + def is_module(adc1_level, adc2_level, slow1, slow2, slow3): + return adc1_level is ADC_HIGH and adc2_level is ADC_FLOAT and slow1 is IO_HIGH and slow2 is IO_LOW and slow3 is IO_LOW + + def __init__(self): + super().__init__() + + def initialise(self, slot, adc1_func, adc2_func): + + # Create the key pin object + self.__key = slot.FAST4 + self.__key.init(Pin.IN, Pin.PULL_UP) + + # Create the RGB LED object + self.__led = RGBLED(slot.FAST1, slot.FAST2, slot.FAST3, invert=True) + + # Pass the slot and adc functions up to the parent now that module specific initialisation has finished + super().initialise(slot, adc1_func, adc2_func) + + def reset(self): + self.__led.set_rgb(0, 0, 0) + + def set_rgb(self, r, g, b): + self.__led.set_rgb(r, g, b) + + def set_hsv(self, h, s, v): + self.__led.set_hsv(h, s, v) + + def is_pressed(self): + return self.__key.value() != 1 diff --git a/lib/pimoroni_yukon/modules/micro_sd.py b/lib/pimoroni_yukon/modules/micro_sd.py new file mode 100644 index 0000000..0881947 --- /dev/null +++ b/lib/pimoroni_yukon/modules/micro_sd.py @@ -0,0 +1,64 @@ +# SPDX-FileCopyrightText: 2025 Christopher Parrott for Pimoroni Ltd +# +# SPDX-License-Identifier: MIT + +import vfs +from machine import SPI +from sdcard import SDCard +from .common import YukonModule, ADC_LOW, ADC_FLOAT, IO_LOW, IO_HIGH + + +class MicroSDModule(YukonModule): + NAME = "Micro SD Card" + + CARD_DETECT_LEVEL = 1.65 + + # | ADC1 | ADC2 | SLOW1 | SLOW2 | SLOW3 | Module | Condition (if any) | + # |-------|-------|-------|-------|-------|----------------------|-----------------------------| + # | LOW | LOW | 1 | 0 | 1 | Micro SD Card | Card Inserted | + # | LOW | HIGH | 1 | 0 | 1 | Micro SD Card | No Card | + @staticmethod + def is_module(adc1_level, adc2_level, slow1, slow2, slow3): + return adc1_level == ADC_LOW and adc2_level != ADC_FLOAT and slow1 is IO_HIGH and slow2 is IO_LOW and slow3 is IO_HIGH + + def __init__(self): + super().__init__() + + try: + import sdcard + except ImportError: + raise RuntimeError("This build does not contain sd card support. Please flash your Yukon with a build that supports sd cards in order to use this module.") + + def initialise(self, slot, adc1_func, adc2_func): + try: + # Create the SPI object + spi_id = (((slot.ID - 1) * 4) // 8) % 2 + self.__spi = SPI(spi_id, sck=slot.FAST3, mosi=slot.FAST4, miso=slot.FAST1) + + except ValueError as e: + raise type(e)("SPI perhiperal already in use. Check that a module in another slot does not share the same SPI perhiperal") from None + + self.__card_cs = slot.FAST2 + self.__sd = None + self.__path = None + + # Pass the slot and adc functions up to the parent now that module specific initialisation has finished + super().initialise(slot, adc1_func, adc2_func) + + def reset(self): + self.umount() + + def mount(self, path="/sd", baudrate=26400000): + if self.__sd is None: + self.__sd = SDCard(self.__spi, self.__card_cs, baudrate=baudrate) + self.__path = path + vfs.mount(self.__sd, self.__path) + + def umount(self): + if self.__sd is not None: + vfs.umount(self.__path) + self.__sd = None + self.__path = None + + def card_present(self): + return self.__read_adc2() >= self.CARD_DETECT_LEVEL diff --git a/lib/pimoroni_yukon/modules/potentiometer.py b/lib/pimoroni_yukon/modules/potentiometer.py new file mode 100644 index 0000000..1f98741 --- /dev/null +++ b/lib/pimoroni_yukon/modules/potentiometer.py @@ -0,0 +1,47 @@ +# SPDX-FileCopyrightText: 2023 Christopher Parrott for Pimoroni Ltd +# +# SPDX-License-Identifier: MIT + +from .common import YukonModule, ADC_HIGH, ADC_LOW, IO_HIGH, IO_LOW +from machine import Pin +from pimoroni import RGBLED + + +class PotModule(YukonModule): + NAME = "Potentiometer + RGB" + + # | ADC1 | ADC2 | SLOW1 | SLOW2 | SLOW3 | Module | Condition (if any) | + # |-------|-------|-------|-------|-------|----------------------|-----------------------------| + # | HIGH | LOW | 1 | 1 | 0 | Potentiometer + RGB | | + @staticmethod + def is_module(adc1_level, adc2_level, slow1, slow2, slow3): + return adc1_level is ADC_HIGH and adc2_level is ADC_LOW and slow1 is IO_HIGH and slow2 is IO_HIGH and slow3 is IO_LOW + + def __init__(self): + super().__init__() + + def initialise(self, slot, adc1_func, adc2_func): + + # Create the potentiometer end pin object + self.__pot_a = slot.SLOW1 + self.__pot_b = slot.SLOW2 + self.__pot_a.init(Pin.OUT, value=False) + self.__pot_b.init(Pin.OUT, value=True) + + # Create the RGB LED object + self.__led = RGBLED(slot.FAST1, slot.FAST2, slot.FAST3, invert=True) + + # Pass the slot and adc functions up to the parent now that module specific initialisation has finished + super().initialise(slot, adc1_func, adc2_func) + + def reset(self): + self.__led.set_rgb(0, 0, 0) + + def set_rgb(self, r, g, b): + self.__led.set_rgb(r, g, b) + + def set_hsv(self, h, s, v): + self.__led.set_hsv(h, s, v) + + def read(self, samples=1): + return self.__read_adc1(samples) / 3.3