46
46
47
47
#: Number of milliseconds between the user successfully logging out and returning to main().
48
48
LOGOUT_SUCCESS_DELAY = 3000
49
- #: Minimum delay between reading posture data from the SQLite database, in do_everything ().
49
+ #: Minimum delay between reading posture data from the SQLite database, in run_user_session ().
50
50
GET_POSTURE_DATA_TIMEOUT = timedelta (milliseconds = 10000 )
51
51
#: Proportion of the time the user must be in frame for any feedback to be given. FIXME: Fine-tune this value later.
52
52
PROPORTION_IN_FRAME_THRESHOLD = 0.3
65
65
#: the plant will move down.
66
66
#: FIXME: Fine-tune this value later.
67
67
PLANT_PROPORTION_GOOD_THRESHOLD = 0.5
68
- """
69
- Threshold for I. Jensen Plant Mover 10000 feedback. If the proportion of "good" sitting posture is below this,
70
- the plant will move down.
71
- FIXME: Fine-tune this value later.
72
- """
73
68
74
- #: DEBUG Number of milliseconds between each loop iteration in do_everything ().
69
+ #: DEBUG Number of milliseconds between each loop iteration in run_user_session ().
75
70
DEBUG_DO_EVERYTHING_INTERVAL = 1000
76
71
77
72
logger = logging .getLogger (__name__ )
@@ -123,8 +118,7 @@ def main():
123
118
124
119
logger .debug ("Login successful" )
125
120
126
- # Run user session
127
- do_everything (user )
121
+ run_user_session (user )
128
122
129
123
# Let the posture tracking process if the user has logged out
130
124
if not args .no_posture_model :
@@ -164,21 +158,21 @@ def initialise_hardware() -> HardwareComponents:
164
158
# SECTION: Control for the logged-in user
165
159
166
160
167
- def do_everything ( auspost : ControlledData ) -> None :
161
+ def run_user_session ( user : ControlledData ) -> None :
168
162
"""
169
163
Main control flow once a user is logged in.
170
164
171
165
Args:
172
- auspost : data encapsulating the current state of the program.
166
+ user : data encapsulating the current state of the program.
173
167
174
168
Requires:
175
- ! auspost .is_failed()
169
+ ! user .is_failed()
176
170
"""
177
- LOGIN_MESSAGE = "Logged in with user id: " + str (auspost .get_user_id ())
178
- LOGOUT_MESSAGE = "Logged out user id " + str (auspost .get_user_id ())
171
+ LOGIN_MESSAGE = "Logged in with user id: " + str (user .get_user_id ())
172
+ LOGOUT_MESSAGE = "Logged out user id " + str (user .get_user_id ())
179
173
180
174
# Initialise posture graph for the current session
181
- hardware .initialise_posture_graph (auspost .get_user_id ())
175
+ hardware .initialise_posture_graph (user .get_user_id ())
182
176
183
177
# Wind plant down all the way
184
178
hardware .wind_plant_safe ()
@@ -195,7 +189,7 @@ def do_everything(auspost: ControlledData) -> None:
195
189
# Set up initial display
196
190
hardware .display .fill (0 )
197
191
hardware .oled_display_texts (
198
- hardware .get_control_messages (auspost .get_user_id ()), 0 , 0 , 1
192
+ hardware .get_control_messages (user .get_user_id ()), 0 , 0 , 1
199
193
)
200
194
hardware .display .show ()
201
195
@@ -206,17 +200,17 @@ def do_everything(auspost: ControlledData) -> None:
206
200
hardware .oled_display_text (LOGOUT_MESSAGE , 0 , 0 , 1 )
207
201
hardware .display .show ()
208
202
sleep_ms (LOGOUT_SUCCESS_DELAY )
209
- print ("<!> END do_everything ()" )
203
+ print ("<!> END run_user_session ()" )
210
204
return
211
205
212
- update_display_screen (auspost )
213
- handle_posture_graph (auspost )
214
- handle_feedback (auspost )
206
+ update_display_screen (user )
207
+ handle_posture_graph (user )
208
+ handle_feedback (user )
215
209
216
210
sleep_ms (DEBUG_DO_EVERYTHING_INTERVAL )
217
211
218
212
219
- def update_display_screen (auspost : ControlledData ) -> bool :
213
+ def update_display_screen (user : ControlledData ) -> bool :
220
214
"""
221
215
Update the display screen with whatever needs to be on there.
222
216
@@ -225,54 +219,54 @@ def update_display_screen(auspost: ControlledData) -> bool:
225
219
Current-session posture graph
226
220
227
221
Args:
228
- auspost : data encapsulating the current state of the program.
222
+ user : data encapsulating the current state of the program.
229
223
230
224
Returns:
231
225
True, always. If you get a False return value, then something has gone VERY wrong.
232
226
233
227
Requires:
234
- ! auspost .is_failed()
228
+ ! user .is_failed()
235
229
236
230
Ensures:
237
- ! auspost .is_failed()
231
+ ! user .is_failed()
238
232
"""
239
233
while (
240
- not auspost .get_posture_data ().empty ()
234
+ not user .get_posture_data ().empty ()
241
235
): # NOTE: This is much more robust than getting a fixed number of things out of the queue
242
236
hardware .display .fill (0 )
243
237
hardware .oled_display_texts (
244
- hardware .get_control_messages (auspost .get_user_id ()), 0 , 0 , 1
238
+ hardware .get_control_messages (user .get_user_id ()), 0 , 0 , 1
245
239
)
246
240
hardware .display .updateGraph2D (
247
- hardware .posture_graph , auspost .get_posture_data ().get_nowait ()
241
+ hardware .posture_graph , user .get_posture_data ().get_nowait ()
248
242
)
249
243
hardware .display .show ()
250
244
251
245
return True
252
246
253
247
254
- def handle_posture_graph (auspost : ControlledData ) -> bool :
248
+ def handle_posture_graph (user : ControlledData ) -> bool :
255
249
"""
256
250
Get a snapshot of the user's posture data.
257
251
Use this information to update the data for the posture graph.
258
252
259
253
Args:
260
- (auspost : ControlledData): Data encapsulating the current state of the program.
254
+ (user : ControlledData): Data encapsulating the current state of the program.
261
255
Returns:
262
256
(bool): True, always. If you get a False return value, then something has gone VERY wrong.
263
257
Requires:
264
- ! auspost .is_failed()
258
+ ! user .is_failed()
265
259
Ensures:
266
- ! auspost .is_failed()
260
+ ! user .is_failed()
267
261
268
262
TODO: Check this
269
263
"""
270
264
now = datetime .now ()
271
265
272
- if now > auspost .get_last_snapshot_time () + GET_POSTURE_DATA_TIMEOUT :
266
+ if now > user .get_last_snapshot_time () + GET_POSTURE_DATA_TIMEOUT :
273
267
# Get the most recent posture data for the user
274
268
recent_posture_data = get_user_postures (
275
- auspost .get_user_id (),
269
+ user .get_user_id (),
276
270
num = - 1 ,
277
271
period_start = now - GET_POSTURE_DATA_TIMEOUT ,
278
272
period_end = now ,
@@ -291,7 +285,7 @@ def handle_posture_graph(auspost: ControlledData) -> bool:
291
285
print (
292
286
"<!> Exiting handle_posturing_monitoring_new() early: Not in frame for a high enough proportion of time."
293
287
)
294
- auspost .set_last_snapshot_time (datetime .now ())
288
+ user .set_last_snapshot_time (datetime .now ())
295
289
return True
296
290
297
291
# Sort the list by period_start
@@ -309,7 +303,7 @@ def handle_posture_graph(auspost: ControlledData) -> bool:
309
303
# Calculate the interval length
310
304
interval = total_time / POSTURE_GRAPH_DATUM_WIDTH
311
305
312
- # Setup sublists, where each sublist is a portion of the overall data
306
+ # Setup sublists, where each sublist is a portion of the overall data
313
307
split_posture_lists : list [list [Posture ]]
314
308
split_posture_lists = [[] for _ in range (POSTURE_GRAPH_DATUM_WIDTH )]
315
309
@@ -332,62 +326,60 @@ def handle_posture_graph(auspost: ControlledData) -> bool:
332
326
[posture .prop_good for posture in posture_list ]
333
327
) / len (posture_list )
334
328
new_prop_good_data += [average_prop_good ] * POSTURE_GRAPH_DATUM_WIDTH
335
- auspost .accept_new_posture_data (new_prop_good_data )
329
+ user .accept_new_posture_data (new_prop_good_data )
336
330
337
- auspost .set_last_snapshot_time (now )
331
+ user .set_last_snapshot_time (now )
338
332
339
333
return True
340
334
341
- def handle_feedback (auspost : ControlledData ) -> bool :
335
+
336
+ def handle_feedback (user : ControlledData ) -> bool :
342
337
"""
343
338
Provide feedback to the user if necessary.
344
339
345
340
Args:
346
- auspost : Data encapsulating the current state of the program.
341
+ user : Data encapsulating the current state of the program.
347
342
Returns:
348
343
(bool): True, always. If you get a False return value, then something has gone VERY wrong.
349
344
350
345
Requires:
351
- ! auspost .is_failed()
346
+ ! user .is_failed()
352
347
353
348
Ensures:
354
- ! auspost .is_failed()
349
+ ! user .is_failed()
355
350
"""
356
- if (
357
- datetime .now ()
358
- > auspost .get_last_cushion_time () + HANDLE_CUSHION_FEEDBACK_TIMEOUT
359
- ):
360
- if not handle_cushion_feedback (auspost ):
351
+ if datetime .now () > user .get_last_cushion_time () + HANDLE_CUSHION_FEEDBACK_TIMEOUT :
352
+ if not handle_cushion_feedback (user ):
361
353
return False
362
- if datetime .now () > auspost .get_last_plant_time () + HANDLE_PLANT_FEEDBACK_TIMEOUT :
363
- if not handle_plant_feedback (auspost ):
354
+ if datetime .now () > user .get_last_plant_time () + HANDLE_PLANT_FEEDBACK_TIMEOUT :
355
+ if not handle_plant_feedback (user ):
364
356
return False
365
357
366
358
return True
367
359
368
360
369
- def handle_cushion_feedback (auspost : ControlledData ) -> bool :
361
+ def handle_cushion_feedback (user : ControlledData ) -> bool :
370
362
"""
371
363
Vibrate cushion (if necessary), and update the timestamp of when cushion feedback was last given.
372
364
373
365
Args:
374
- auspost : Data encapsulating the current state of the program.
366
+ user : Data encapsulating the current state of the program.
375
367
376
368
Returns:
377
369
True, always. If you get a False return value, then something has gone VERY wrong.
378
370
379
371
Requires:
380
- ! auspost .is_failed()
372
+ ! user .is_failed()
381
373
382
374
Ensures:
383
- ! auspost .is_failed()
375
+ ! user .is_failed()
384
376
"""
385
377
print ("<!> handle_cushion_feedback()" )
386
378
387
379
# Load posture records within the last HANDLE_CUSHION_FEEDBACK_TIMEOUT
388
380
now = datetime .now ()
389
381
recent_posture_data = get_user_postures (
390
- auspost .get_user_id (),
382
+ user .get_user_id (),
391
383
num = - 1 ,
392
384
period_start = now - HANDLE_CUSHION_FEEDBACK_TIMEOUT ,
393
385
period_end = now ,
@@ -396,7 +388,7 @@ def handle_cushion_feedback(auspost: ControlledData) -> bool:
396
388
# Conditions for exiting early
397
389
if len (recent_posture_data ) == 0 :
398
390
print ("<!> Exiting handle_cushion_feedback() early: No data" )
399
- auspost .set_last_cushion_time (datetime .now ())
391
+ user .set_last_cushion_time (datetime .now ())
400
392
return True
401
393
average_prop_in_frame = sum (
402
394
[posture .prop_in_frame for posture in recent_posture_data ]
@@ -405,14 +397,14 @@ def handle_cushion_feedback(auspost: ControlledData) -> bool:
405
397
print (
406
398
"<!> Exiting handle_cushion_feedback() early: Not in frame for a high enough proportion of time."
407
399
)
408
- auspost .set_last_cushion_time (datetime .now ())
400
+ user .set_last_cushion_time (datetime .now ())
409
401
return True
410
402
average_prop_good = sum (
411
403
[posture .prop_good for posture in recent_posture_data ]
412
404
) / len (recent_posture_data )
413
405
if average_prop_good >= CUSHION_PROPORTION_GOOD_THRESHOLD :
414
406
print ("<!> Exiting handle_cushion_feedback() early: You sat well :)" )
415
- auspost .set_last_cushion_time (datetime .now ())
407
+ user .set_last_cushion_time (datetime .now ())
416
408
return True
417
409
418
410
buzzer_start_time = datetime .now ()
@@ -424,34 +416,34 @@ def handle_cushion_feedback(auspost: ControlledData) -> bool:
424
416
GPIO .output (CUSHION_GPIO_PIN , GPIO .LOW )
425
417
print ("<!> buzzer off" )
426
418
427
- auspost .set_last_cushion_time (datetime .now ())
419
+ user .set_last_cushion_time (datetime .now ())
428
420
return True
429
421
430
422
431
- def handle_plant_feedback (auspost : ControlledData ) -> bool :
423
+ def handle_plant_feedback (user : ControlledData ) -> bool :
432
424
"""
433
425
Set the plant height according to short-term current session data, and update the timestamp
434
426
of when plant feedback was last given.
435
427
436
428
Args:
437
- auspost : Data encapsulating the current state of the program.
429
+ user : Data encapsulating the current state of the program.
438
430
439
431
Returns:
440
432
(bool): True, always. If you get a False return value, then something has gone VERY wrong.
441
433
442
434
Requires:
443
- ! auspost .is_failed()
435
+ ! user .is_failed()
444
436
Ensures:
445
- ! auspost .is_failed()
437
+ ! user .is_failed()
446
438
"""
447
439
print ("<!> handle_plant_feedback()" )
448
440
449
441
now = datetime .now ()
450
442
451
- if now > auspost .get_last_plant_time () + HANDLE_PLANT_FEEDBACK_TIMEOUT :
443
+ if now > user .get_last_plant_time () + HANDLE_PLANT_FEEDBACK_TIMEOUT :
452
444
# Get the most recent posture data for the user
453
445
recent_posture_data = get_user_postures (
454
- auspost .get_user_id (),
446
+ user .get_user_id (),
455
447
num = - 1 ,
456
448
period_start = now - GET_POSTURE_DATA_TIMEOUT ,
457
449
period_end = now ,
@@ -460,7 +452,7 @@ def handle_plant_feedback(auspost: ControlledData) -> bool:
460
452
# Conditions for exiting early
461
453
if len (recent_posture_data ) == 0 :
462
454
print ("<!> Exiting handle_plant_feedback() early: No data" )
463
- auspost .set_last_plant_time (datetime .now ())
455
+ user .set_last_plant_time (datetime .now ())
464
456
return True
465
457
466
458
average_prop_in_frame = sum (
@@ -470,7 +462,7 @@ def handle_plant_feedback(auspost: ControlledData) -> bool:
470
462
print (
471
463
"<!> Exiting handle_plant_feedback() early: Not in frame for a high enough proportion of time."
472
464
)
473
- auspost .set_last_plant_time (datetime .now ())
465
+ user .set_last_plant_time (datetime .now ())
474
466
return True
475
467
476
468
# Calculate average proportion
@@ -484,7 +476,7 @@ def handle_plant_feedback(auspost: ControlledData) -> bool:
484
476
else :
485
477
hardware .set_plant_height (hardware .plant_height - 1 )
486
478
487
- auspost .set_last_plant_time (datetime .now ())
479
+ user .set_last_plant_time (datetime .now ())
488
480
489
481
return True
490
482
0 commit comments