diff --git a/.github/workflows/micropython-with-blinka.yml b/.github/workflows/micropython-with-blinka.yml index 52535233..50549058 100644 --- a/.github/workflows/micropython-with-blinka.yml +++ b/.github/workflows/micropython-with-blinka.yml @@ -61,6 +61,7 @@ jobs: # Copy module files from Blinka/PlatformDetect into MicroPython - name: Copy modules run: | + cp -r pimoroni-pico-${GITHUB_SHA}/micropython/modules_py/* micropython/ports/rp2/modules/ mkdir -p micropython/ports/rp2/modules/adafruit_blinka/microcontroller/ cp -r Adafruit_Blinka/src/adafruit_blinka/microcontroller/rp2040 micropython/ports/rp2/modules/adafruit_blinka/microcontroller/ mkdir -p micropython/ports/rp2/modules/adafruit_blinka/board/raspberrypi/ diff --git a/.github/workflows/micropython.yml b/.github/workflows/micropython.yml index 9198dc67..4dd2efeb 100644 --- a/.github/workflows/micropython.yml +++ b/.github/workflows/micropython.yml @@ -41,6 +41,11 @@ jobs: submodules: true path: pimoroni-pico-${{ github.sha }} + # Copy Python module files + - name: Copy modules + run: | + cp -r pimoroni-pico-${GITHUB_SHA}/micropython/modules_py/* micropython/ports/rp2/modules/ + # Linux deps - name: Install deps if: runner.os == 'Linux' diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 0735e3a4..a2792aa3 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -32,4 +32,4 @@ jobs: - name: Lint shell: bash run: | - python3 -m flake8 --ignore E501 micropython/examples \ No newline at end of file + python3 -m flake8 --ignore E501 micropython/modules_py micropython/examples \ No newline at end of file diff --git a/micropython/modules_py/pimoroni.py b/micropython/modules_py/pimoroni.py new file mode 100644 index 00000000..0791c068 --- /dev/null +++ b/micropython/modules_py/pimoroni.py @@ -0,0 +1,69 @@ +import time +from machine import Pin, PWM + + +class Button: + def __init__(self, button, invert=True, repeat_time=200, hold_time=1000): + self.invert = invert + self.repeat_time = repeat_time + self.hold_time = hold_time + self.pin = Pin(button, pull=Pin.PULL_UP if invert else Pin.PULL_DOWN) + self.last_state = False + self.pressed = False + self.pressed_time = 0 + + def read(self): + current_time = time.ticks_ms() + state = self.raw() + changed = state != self.last_state + self.last_state = state + + if changed: + if state: + self.pressed_time = current_time + self.pressed = True + self.last_time = current_time + return True + else: + self.pressed_time = 0 + self.pressed = False + self.last_time = 0 + + if self.repeat_time == 0: + return False + + if self.pressed: + repeat_rate = self.repeat_time + if self.hold_time > 0 and current_time - self.pressed_time > self.hold_time: + repeat_rate /= 3 + if current_time - self.last_time > repeat_rate: + self.last_time = current_time + return True + + return False + + def raw(self): + if self.invert: + return not self.pin.value() + else: + return self.pin.value() + + +class RGBLED: + def __init__(self, r, g, b, invert=True): + self.invert = invert + self.led_r = PWM(Pin(r)) + self.led_r.freq(1000) + self.led_g = PWM(Pin(g)) + self.led_g.freq(1000) + self.led_b = PWM(Pin(b)) + self.led_b.freq(1000) + + def set_rgb(self, r, g, b): + if self.invert: + r = 255 - r + g = 255 - g + b = 255 - b + self.led_r.duty_u16(r * 255) + self.led_g.duty_u16(g * 255) + self.led_b.duty_u16(b * 255)