Skip to content

Commit 1b36330

Browse files
committed
add slides for more talk time
1 parent a3238b5 commit 1b36330

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

bluetooth_distance_sensor.sh

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
11
#!/usr/bin/env bash
22

33
#Global VARS:
4-
device=$1
4+
device=$1 # Bluetooth mac address
55
btconnected=0
66
btcurrent=-1
77
counter=0
88
notconnected="0"
99
connected="1"
1010
rssi=-1
1111

12+
#Linux applications
13+
#1. hcitool - Bluetooth linux driver cli app
14+
# For getting RSSI readings
15+
#2. rfcomm - RF communication linux cli app
16+
# For connecting with mac address
1217
#Command loop:
1318
while [ 1 ]; do
19+
# Trying to get RSSI signal from Bluetooth
1420
cmdout=$(hcitool rssi $device)
1521
btcurrent=$(echo $cmdout | grep -c "RSSI return value") 2> /dev/null
1622
rssi=$(echo $cmdout | sed -e 's/RSSI return value: //g')
23+
1724
if [ $btcurrent = $notconnected ]; then
25+
# Trying to connect to the Bluetooth mac address
1826
echo "Attempting connection..."
1927
rfcomm connect 0 $device 1 2> /dev/null >/dev/null &
2028
sleep 1
2129
fi
30+
2231
if [ $btcurrent = $connected ]; then
32+
# print to STDOUT RSSI signal
2333
echo "Device connected. RSSI: "$rssi
2434
fi
35+
2536
if [ $btconnected -ne $btcurrent ]; then
2637
if [ $btcurrent -eq 0 ]; then
38+
# print to STDOUT No Connection
2739
echo "GONE!"
2840
fi
2941
if [ $btcurrent -eq 1 ]; then
42+
# print to STDOUT have Connection
3043
echo "HERE!"
3144
fi
3245
btconnected=$btcurrent

details.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
from typing import Union
3+
from pathlib import Path
4+
from collections import deque
5+
from subprocess import run, Popen, STDOUT, PIPE
6+
7+
UNLOCK_STATE = 'UNLOCK'
8+
LOCK_STATE = 'LOCK'
9+
SENSOR_SCRIPT_PATH = Path('bluetooth_distance_sensor.sh')
10+
DO_NOT_CARE_SENSOR_MESSAGES = [
11+
'Read RSSI failed: Input/output error',
12+
'Attempting connection...',
13+
'GONE!',
14+
'HERE!',
15+
]
16+
LOCKER_LOCK_COMMAND = os.getenv(
17+
'LOCKER_LOCK_COMMAND',
18+
'gnome-screensaver-command --lock')
19+
LOCKER_UNLOCK_COMMAND = os.getenv(
20+
'LOCKER_UNLOCK_COMMAND',
21+
'gnome-screensaver-command --deactivate')
22+
LOCKER_STATUS_COMMAND = os.getenv(
23+
'LOCKER_STATUS_COMMAND',
24+
'gnome-screensaver-command --query')
25+
LOCKER_UNLOCK_SCREEN_SAVE_PREFIX = os.getenv(
26+
'LOCKER_UNLOCK_SCREEN_SAVE_PREFIX',
27+
'The screensaver is inactive')
28+
29+
30+
def bluetooth_distance_locker(device_mac_address):
31+
commend = [str(SENSOR_SCRIPT_PATH), device_mac_address]
32+
queue = deque(maxlen=5)
33+
state = UNLOCK_STATE
34+
bluetooth_active = False
35+
36+
def parse_line():
37+
"""
38+
Parse the child process line
39+
* check if relevant
40+
* modify the queue
41+
"""
42+
...
43+
44+
def print_state():
45+
"""
46+
Print nice status to stdout
47+
"""
48+
...
49+
50+
def check_screen_save_status() -> Union[UNLOCK_STATE, LOCK_STATE]:
51+
"""
52+
Check status of the screen save
53+
"""
54+
...
55+
56+
with Popen(commend,
57+
stderr=STDOUT, stdout=PIPE,
58+
bufsize=1, universal_newlines=True) as process:
59+
for line in iter(process.stdout.readline, ''):
60+
parse_line()
61+
state = check_screen_save_status()
62+
63+
if state == UNLOCK_STATE and len(queue) == queue.maxlen:
64+
state = LOCK_STATE
65+
run(LOCKER_LOCK_COMMAND.split())
66+
elif state == LOCK_STATE and len(queue) < 4:
67+
state = UNLOCK_STATE
68+
run(LOCKER_UNLOCK_COMMAND.split())
69+
print_state()

story.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,18 @@ def the_research_2():
363363
pass
364364

365365

366+
@explanation_snippets
367+
def bluetooth_distance_sensor():
368+
"""
369+
How does the bluetooth distance sensor works??
370+
"""
371+
bluetooth_distance_sensor.code = (
372+
'def open_sensor_in_editor():',
373+
"run(['vim', 'bluetooth_distance_sensor.sh'])",
374+
'',
375+
)
376+
377+
366378
@explanation_snippets
367379
def developing_1():
368380
"""
@@ -415,13 +427,29 @@ def developing_3():
415427
"""
416428

417429

430+
@explanation_snippets
431+
def finished_project():
432+
"""
433+
How does the finished project looks like??
434+
"""
435+
finished_project.code = (
436+
'def open_finished_project_in_editor():',
437+
"run(['vim', 'details.py'])",
438+
'',
439+
)
440+
441+
418442
@explanation_snippets
419443
def end():
420444
"""
421445
Demo and questions
422-
423-
$ distance_locker -a <mac address>
424446
"""
447+
end.code = (
448+
'def demo():',
449+
'with suppress(KeyboardInterrupt):',
450+
"run(['distance_locker', '-a', PHONE_ADDRESS])",
451+
'',
452+
)
425453

426454

427455
if __name__ == '__main__':

0 commit comments

Comments
 (0)