Browse Source

init

needs cleaning up seen obisidan notes
cube 2 months ago
parent
commit
3f0d0785a1
1 changed files with 220 additions and 0 deletions
  1. 220
    0
      CARDPUTER CircuitPython/projects/BasicReachMoveTracker.py

+ 220
- 0
CARDPUTER CircuitPython/projects/BasicReachMoveTracker.py View File

@@ -0,0 +1,220 @@
1
+from keyb import Keyboard # type: ignore
2
+import time, os, rtc, json # type: ignore
3
+import board, busio, sdcardio, storage # type: ignore
4
+
5
+def count_moves_hour(moves, hour):
6
+    count = 0
7
+    for move in moves:
8
+        if move[0] == hour:
9
+            count += 1
10
+    return count
11
+
12
+def diffs(moves):
13
+    for move in moves:
14
+        h = move[2].split(":")[0]
15
+        if int(h) >= 5:
16
+            line = str(move[0]) + ":" + str(move[1]) + " / " + str(move[2])
17
+            print(line)
18
+
19
+def write_file(moves, downtime):
20
+    data = {"Moves": moves, "Downtime": downtime}
21
+    filename = "/sd/data.json"
22
+    with open(filename, "w") as f:
23
+        json.dump(data, f)
24
+def read_file():
25
+    print("Reading data")
26
+    filename = "/sd/data.json"
27
+    with open(filename, "r") as f:
28
+        data = json.load(f)
29
+    moves = data["Moves"]
30
+    downtime = data["Downtime"]
31
+    return moves, downtime
32
+
33
+
34
+
35
+spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
36
+cs = board.SD_CS
37
+
38
+try:
39
+    sdcard = sdcardio.SDCard(spi, cs)
40
+    vfs = storage.VfsFat(sdcard)
41
+
42
+    storage.mount(vfs, "/sd") # access files on sd card here
43
+except OSError:
44
+    pass # SD card not inserted/found
45
+
46
+
47
+
48
+
49
+
50
+last = None
51
+moves = []
52
+downtime = []
53
+
54
+r = rtc.RTC()
55
+#r.datetime = time.struct_time((2025, 10, 7, 23, 39, 0, 0, -1, -1))
56
+current_time = r.datetime
57
+print(str(current_time.tm_hour) + ":" + str(current_time.tm_min))
58
+
59
+
60
+keyb = Keyboard()
61
+while True:
62
+    key = keyb.scan()
63
+    if key == "\n":
64
+        if not last:
65
+            last = time.time()
66
+            print("Starting!")
67
+        else:
68
+            time2 = time.time()
69
+            diff = time2 - last
70
+            m, s = divmod(diff, 60)
71
+            diff_delta = f'{m:02d}:{s:02d}'
72
+            print(diff_delta)
73
+
74
+            last = time2
75
+            current_time = r.datetime
76
+            hour = current_time.tm_hour
77
+            mint = current_time.tm_min
78
+            moves.append([hour, mint, diff_delta])
79
+            print("Moves this hour:", count_moves_hour(moves, hour))
80
+            write_file(moves, downtime)
81
+
82
+    elif key == "b":
83
+        print("Battery change")
84
+        ct = r.datetime
85
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
86
+        dt = ["Battery change", ct_str]
87
+        downtime.append(dt)
88
+
89
+    elif key == "n":
90
+        print("Battery change finish")
91
+        ct = r.datetime
92
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
93
+        dt = ["Battery change finish", ct_str]
94
+        downtime.append(dt)
95
+
96
+    elif key == "p":
97
+        print("Issue with pallet")
98
+        ct = r.datetime
99
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
100
+        dt = ["Issue with pallet", ct_str]
101
+        downtime.append(dt)
102
+
103
+    elif key == "q":
104
+        print("Queue/Busy Area/Leaving Space")
105
+        ct = r.datetime
106
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
107
+        dt = ["Queue/Busy Area/Leaving Space", ct_str]
108
+        downtime.append(dt)
109
+
110
+    elif key == "s":
111
+        print("Staff Reason")
112
+        ct = r.datetime
113
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
114
+        dt = ["Staff Reason", ct_str]
115
+        downtime.append(dt)
116
+
117
+    elif key == " ":
118
+        print("Break")
119
+        ct = r.datetime
120
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
121
+        dt = ["Break", ct_str]
122
+        downtime.append(dt)
123
+    elif key == "BACKSP":
124
+        print("Break Finish")
125
+        ct = r.datetime
126
+        ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
127
+        dt = ["Break Finish", ct_str]
128
+        downtime.append(dt)
129
+
130
+    elif key == "CTRLD":
131
+        print("====== INFO =====")
132
+        if downtime:
133
+            for item in downtime:
134
+                print(item[0], ":", item[1])
135
+            print("====== ======== =====")
136
+        diffs(moves)
137
+        print("====== ======== =====")
138
+        current_time = r.datetime
139
+        hour = current_time.tm_hour
140
+        print("Moves this hour:", count_moves_hour(moves, hour))
141
+        print("Total Moves:", len(moves))
142
+        print("====== ======== =====")
143
+
144
+    elif key == "CTRLO":
145
+        print("Overwrite Data From File?")
146
+        while True:
147
+            key = keyb.scan()
148
+            if key == "\n":
149
+                data = read_file()
150
+                moves = data[0]
151
+                downtime = data[1]
152
+                print("DATA LOADED")
153
+                break
154
+            else:
155
+                print("DATA UNCHANGED")
156
+                break
157
+
158
+    elif key == "CTRLS":
159
+        print("Set Time")
160
+        print("ENTER HOUR")
161
+        hour = ""
162
+        while True:
163
+            key = keyb.scan()
164
+            if key == "1":
165
+                hour += key
166
+            elif key == "2":
167
+                hour += key
168
+            elif key == "3":
169
+                hour += key
170
+            elif key == "4":
171
+                hour += key
172
+            elif key == "5":
173
+                hour += key
174
+            elif key == "6":
175
+                hour += key
176
+            elif key == "7":
177
+                hour += key
178
+            elif key == "8":
179
+                hour += key
180
+            elif key == "9":
181
+                hour += key
182
+            elif key == "0":
183
+                hour += key
184
+            elif key == "\n":
185
+                print("HOUR:", hour)
186
+                break
187
+        print("ENTER MINUTE")
188
+        minute = ""
189
+        while True:
190
+            key = keyb.scan()
191
+            if key == "1":
192
+                minute += key
193
+            elif key == "2":
194
+                minute += key
195
+            elif key == "3":
196
+                minute += key
197
+            elif key == "4":
198
+                minute += key
199
+            elif key == "5":
200
+                minute += key
201
+            elif key == "6":
202
+                minute += key
203
+            elif key == "7":
204
+                minute += key
205
+            elif key == "8":
206
+                minute += key
207
+            elif key == "9":
208
+                minute += key
209
+            elif key == "0":
210
+                minute += key
211
+            elif key == "\n":
212
+                print("MINUTE:", minute)
213
+                break
214
+        h = int(hour)
215
+        m = int(minute)
216
+        r.datetime = time.struct_time((2025, 10, 8, h, m, 0, 0, -1, -1))
217
+        current_time = r.datetime
218
+        print(str(current_time.tm_hour) + ":" + str(current_time.tm_min))
219
+
220
+