From 539cb7ab6646c26032215ef04a526ade4ecad850 Mon Sep 17 00:00:00 2001 From: vcc_gnd gardener Date: Fri, 9 Feb 2024 17:42:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ neopixel.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 main.py create mode 100644 neopixel.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..a9063bc --- /dev/null +++ b/main.py @@ -0,0 +1,52 @@ +import time +import _thread +from pyb import LED +from pyb import Accel +import neopixel + +accel = Accel() +p = machine.Pin.board.RGB +n = neopixel.NeoPixel(p, 1) + +def funcA(sec): + time.sleep(sec) + + + while 1: + LED(1).toggle() + time.sleep(0.5) + LED(2).toggle() + time.sleep(0.5) + LED(3).toggle() + time.sleep(0.5) + LED(4).toggle() + time.sleep(0.5) + + +def funcC(sec): + time.sleep(sec) + + + while 1: + if accel.x()>=10: + n[0] = (18, 0, 0) + n.write() + elif accel.x()<=-10: + n[0] = (0, 0, 20) + n.write() + elif accel.x()<10 and accel.y()>-10: + n[0] = (0, 20, 0) + n.write() + time.sleep(0.2) + +if __name__ == '__main__': + + + _thread.start_new_thread(funcA, (0.5,)) + _thread.start_new_thread(funcC, (1,)) + + + + + + diff --git a/neopixel.py b/neopixel.py new file mode 100644 index 0000000..caa12dc --- /dev/null +++ b/neopixel.py @@ -0,0 +1,50 @@ +# NeoPixel driver for MicroPython +# MIT license; Copyright (c) 2016 Damien P. George, 2021 Jim Mussared + +from machine import bitstream + + +class NeoPixel: + # G R B W + ORDER = (1, 0, 2, 3) + + def __init__(self, pin, n, bpp=3, timing=1): + self.pin = pin + self.n = n + self.bpp = bpp + self.buf = bytearray(n * bpp) + self.pin.init(pin.OUT) + # Timing arg can either be 1 for 800kHz or 0 for 400kHz, + # or a user-specified timing ns tuple (high_0, low_0, high_1, low_1). + self.timing = ( + ((400, 850, 800, 450) if timing else (800, 1700, 1600, 900)) + if isinstance(timing, int) + else timing + ) + + def __len__(self): + return self.n + + def __setitem__(self, i, v): + offset = i * self.bpp + for i in range(self.bpp): + self.buf[offset + self.ORDER[i]] = v[i] + + def __getitem__(self, i): + offset = i * self.bpp + return tuple(self.buf[offset + self.ORDER[i]] for i in range(self.bpp)) + + def fill(self, v): + b = self.buf + l = len(self.buf) + bpp = self.bpp + for i in range(bpp): + c = v[i] + j = self.ORDER[i] + while j < l: + b[j] = c + j += bpp + + def write(self): + # BITSTREAM_TYPE_HIGH_LOW = 0 + bitstream(self.pin, 0, self.timing, self.buf)