summaryrefslogtreecommitdiff
path: root/micropython/fedibadge.py
blob: db8d942636bf3d5000f65247a039a59964609083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from machine import Pin
import neopixel
import time

class Fedi:
    def __init__(self):
        self.led = neopixel.NeoPixel(Pin(2), 5)
        self.butt = Pin(5, Pin.IN, Pin.PULL_UP)

        self.butt_count = 0
        self.butt_latest = time.ticks_ms()

        self.butt.irq(self.button_pressed, Pin.IRQ_RISING)

    def button_pressed(self, pin):
        if self.butt_count == 0:
            self.butt_latest = time.ticks_ms()
            self.butt_count += 1
        else:
            now = time.ticks_ms()
            delta = now - self.butt_latest
            if delta > 5000:
                self.button_counted()
            elif delta > 500:
                self.butt_latest = now
                self.butt_count += 1

    def button_counted(self):
        print("We have counted", self.butt_count)
        self.butt_count = 0

    def test_loop(self):
        count = 0
        cols = [
            (16, 0, 0),
            (16, 16, 0),
            (0, 16, 0),
            (0, 16, 16),
            (0, 0, 16),
            (16, 0, 16)
        ]
        while True:
            self.led.fill(cols[count])
            self.led.write()
            count += 1
            count = count % 6
            print("butt_count", self.butt_count)
            time.sleep(1)