collab/pb-hal

git clone https://git.julia.blue/collab/pb-hal

Rahul ButaniRestructuringce16264

imu
1.1 KiB55 linesraw
1import wiringpi as wp
2from time import sleep
3
4A = 23
5B = 24
6
7count = 0
8state = 0
9
10cmax = -9999
11cmin =  9999
12
13""" STOLEN """
14def pinISRB():
15    global count
16    count += 1
17
18def pinISR():
19    global count, state, cmin, cmax
20
21    (a, b) = (wp.digitalRead(A), wp.digitalRead(B))
22
23    # We're in one of four of states:
24    s = state & 3
25
26    # Update state, new state:
27    s |= (a * (4) | b * (8))
28    state = (s >> 2)
29
30    # React based on current state and next state:
31    count +=   (s in (0b0001, 0b0111, 0b1000, 0b1100))
32    count -=   (s in (0b0010, 0b0100, 0b1011, 0b1101))
33    count += 2*(s in (0b0011, 0b1010))
34    count -= 2*(s in (0b0110, 0b1001))
35
36    # If 0 or 5 or 10 or 15: HCF
37
38    (cmax, cmin) = max(count, cmax), min(count, cmin)
39
40wp.wiringPiSetupGpio()
41
42wp.pinMode(A, wp.GPIO.INPUT)
43wp.pinMode(B, wp.GPIO.INPUT)
44
45wp.pullUpDnControl(A, wp.GPIO.PUD_UP)
46wp.pullUpDnControl(B, wp.GPIO.PUD_UP)
47
48wp.wiringPiISR(A, wp.GPIO.INT_EDGE_BOTH, pinISR)
49wp.wiringPiISR(B, wp.GPIO.INT_EDGE_BOTH, pinISR)
50
51
52while True:
53#    sleep(0.5)
54    wp.delay(500)
55    print("Count: ", count, "\t\t Max: ", cmax, "\t\t Min: ", cmin)