Browse Source

some improvements in DIn

Ondřej Hruška 2 years ago
parent
commit
adee8b7b0f
Signed by: Ondřej Hruška <ondra@ondrovo.com> GPG key ID: 2C5FD5035250423D
4 changed files with 126 additions and 12 deletions
  1. 11 3
      gex/units/DIn.py
  2. 1 1
      gex/units/Neopixel.py
  3. 22 8
      main.py
  4. 92 0
      pymodoro.py

+ 11 - 3
gex/units/DIn.py View File

@@ -46,11 +46,19 @@ class DIn(gex.Unit):
46 46
     def on_trigger(self, sensitive_pins, callback):
47 47
         """
48 48
         Assign a trigger callback.
49
+        Pins are passed as a list of indices (packed), or a bitmap
49 50
         Arguments are: pins snapshot, timestamp
50 51
         """
51
-        for i in range(0, 16):
52
-            if sensitive_pins & (1 << i):
53
-                self.handlers[i] = callback
52
+
53
+        if type(sensitive_pins) == int:
54
+            L = []
55
+            for i in range(0,16):
56
+                if sensitive_pins & (1 << i) != 0:
57
+                    L.append(i)
58
+            sensitive_pins = L
59
+
60
+        for i in sensitive_pins:
61
+            self.handlers[i] = callback
54 62
 
55 63
     def _on_event(self, evt:EventReport):
56 64
         if evt.code == 0x00:

+ 1 - 1
gex/units/Neopixel.py View File

@@ -6,7 +6,7 @@ class Neopixel(gex.Unit):
6 6
     """
7 7
 
8 8
     def _type(self):
9
-        return 'NEOPIXEL'
9
+        return 'NPX'
10 10
 
11 11
     def get_len(self):
12 12
         """ Get the neopixel strip length """

+ 22 - 8
main.py View File

@@ -10,6 +10,11 @@ transport = gex.TrxRawUSB(sn='0029002F-42365711-32353530')
10 10
 #transport = gex.TrxSerialSync(port='/dev/ttyACM0')
11 11
 
12 12
 with gex.Client(transport) as client:
13
+    #
14
+    # if True:
15
+    #     s = client.ini_read()
16
+    #     print(s)
17
+    #     client.ini_write(s)
13 18
 
14 19
     if False:
15 20
         adc = gex.ADC(client, 'adc')
@@ -28,15 +33,24 @@ with gex.Client(transport) as client:
28 33
     if True:
29 34
         adc = gex.ADC(client, 'adc')
30 35
 
31
-        adc.set_active_channels([16])
32
-        fs = adc.set_sample_rate(30000)
36
+        adc.set_active_channels([1])
37
+        fs = adc.set_sample_rate(1000)
38
+
39
+        data = adc.capture(1000)
40
+
41
+        if data is not None:
42
+            plt.plot(data, 'r-', lw=1)
43
+            plt.show()
44
+        else:
45
+            print("Nothing rx")
46
+
33 47
 
34
-        for r in range(0,8):
35
-            adc.set_sample_time(r)
36
-            data = adc.capture(1000)
37
-            print("sr = %d" % r)
38
-            std = np.std(data)
39
-            print(std)
48
+        # for r in range(0,8):
49
+        #     adc.set_sample_time(r)
50
+        #     data = adc.capture(10000)
51
+        #     print("sr = %d" % r)
52
+        #     std = np.std(data)
53
+        #     print(std)
40 54
 
41 55
 
42 56
         #

+ 92 - 0
pymodoro.py View File

@@ -0,0 +1,92 @@
1
+import time
2
+import gex
3
+
4
+WK_TIME = 25
5
+BK_TIME = 5
6
+LIGHT_CNT = 30
7
+
8
+PH_BREAK = 'Break'
9
+PH_BREAK_OVER = 'BreakOver'
10
+PH_WORK = 'Work'
11
+PH_WORK_OVER = 'WorkOver'
12
+
13
+class Pymodoro:
14
+    def __init__(self):
15
+        self.phase = PH_BREAK_OVER
16
+        self.work_s = 0
17
+        self.break_s = 0
18
+        self.colors = [0x000000 for _ in range(0, LIGHT_CNT)]
19
+
20
+        client = gex.Client(gex.TrxRawUSB())
21
+        self.btn = gex.DIn(client, 'btn')
22
+        self.neo = gex.Neopixel(client, 'neo')
23
+        self.btn.on_trigger([0], self.on_btn)
24
+
25
+        self.switch(PH_BREAK_OVER)
26
+        self.display()
27
+
28
+    def display(self):
29
+        self.neo.load(self.colors)
30
+
31
+    def on_btn(self, snapshot, timestamp):
32
+        if self.phase == PH_BREAK_OVER:
33
+            self.switch(PH_WORK)
34
+            
35
+        elif self.phase == PH_WORK:
36
+            self.switch(PH_WORK) # restart
37
+
38
+        elif self.phase == PH_WORK_OVER:
39
+            self.switch(PH_BREAK)
40
+
41
+    def switch(self, phase):
42
+        print("Switch to %s" % phase)
43
+
44
+        if phase == PH_BREAK:
45
+            self.colors = [0x009900 for _ in range(0, LIGHT_CNT)]
46
+            self.break_s = BK_TIME * 60
47
+
48
+        elif phase == PH_BREAK_OVER:
49
+            self.colors = [0x662200 for _ in range(0, LIGHT_CNT)]
50
+
51
+        elif phase == PH_WORK:
52
+            self.colors = [0x990000 for _ in range(0, LIGHT_CNT)]
53
+            self.work_s = WK_TIME * 60
54
+
55
+        elif phase == PH_WORK_OVER:
56
+            self.colors = [0x113300 for _ in range(0, LIGHT_CNT)]
57
+
58
+        self.phase = phase
59
+
60
+    def extinguish(self, dark, total):
61
+        per_light = total / LIGHT_CNT
62
+        lights = int((dark + per_light / 2) / per_light)
63
+        for n in range(0, LIGHT_CNT - lights):
64
+            self.colors[n] = 0x000000
65
+
66
+    def tick(self):
67
+        if self.phase == PH_BREAK:
68
+            self.break_s -= 1
69
+            print("Break remain: %d s" % self.break_s)
70
+            self.extinguish(self.break_s, BK_TIME * 60)
71
+
72
+            if self.break_s == 0:
73
+                self.switch(PH_BREAK_OVER)
74
+
75
+        elif self.phase == PH_WORK:
76
+            self.work_s -= 1
77
+            print("Work remain: %d s" % self.work_s)
78
+            self.extinguish(self.work_s, WK_TIME * 60)
79
+
80
+            if self.work_s == 0:
81
+                self.switch(PH_WORK_OVER)
82
+
83
+        self.display()
84
+
85
+    def run(self):
86
+        while True:
87
+            time.sleep(1)
88
+            self.tick()
89
+
90
+
91
+a = Pymodoro()
92
+a.run()