11import json
2+ import supervisor
23import time
34
45from pysquared .cdh import CommandDataHandler
@@ -25,13 +26,20 @@ def __init__(
2526 def listen (self ):
2627 try :
2728 while True :
29+ if supervisor .runtime .serial_bytes_available :
30+ typed = input ().strip ()
31+ if typed :
32+ self .handle_input (typed )
33+
2834 b = self ._packet_manager .listen (1 )
2935 if b is not None :
3036 self ._log .info (
3137 message = "Received response" , response = b .decode ("utf-8" )
3238 )
39+
3340 except KeyboardInterrupt :
34- self ._log .debug ("Keyboard interrupt received, exiting listen mode." )
41+ self ._log .debug (
42+ "Keyboard interrupt received, exiting listen mode." )
3543
3644 def send_receive (self ):
3745 try :
@@ -45,61 +53,66 @@ def send_receive(self):
4553 ===============================
4654 """
4755 )
48- if cmd_selection not in ["1" , "2" , "3" ]:
49- self ._log .warning ("Invalid command selection. Please try again." )
50- return
51-
52- message : dict [str , object ] = {
53- "password" : self ._config .super_secret_code ,
54- }
55-
56- if cmd_selection == "1" :
57- message ["command" ] = self ._cdh .command_reset
58- elif cmd_selection == "2" :
59- message ["command" ] = self ._cdh .command_change_radio_modulation
60- modulation = input ("Enter new radio modulation [FSK | LoRa]: " )
61- message ["args" ] = [modulation ]
62- elif cmd_selection == "3" :
63- message ["command" ] = self ._cdh .command_send_joke
6456
65- while True :
66- # Turn on the radio so that it captures any received packets to buffer
67- self ._packet_manager .listen (1 )
57+ self .handle_input (cmd_selection )
6858
69- # Send the message
70- self ._log .info (
71- "Sending command" ,
72- cmd = message ["command" ],
73- args = message .get ("args" , []),
74- )
75- self ._packet_manager .send (json .dumps (message ).encode ("utf-8" ))
59+ except KeyboardInterrupt :
60+ self ._log .debug ("Keyboard interrupt received, exiting send mode." )
7661
77- # Listen for ACK response
78- b = self ._packet_manager .listen (1 )
79- if b is None :
80- self ._log .info ("No response received, retrying..." )
81- continue
62+ def handle_input (self , cmd_selection ):
63+ if cmd_selection not in ["1" , "2" , "3" ]:
64+ self ._log .warning (
65+ "Invalid command selection. Please try again." )
66+ return
67+
68+ message : dict [str , object ] = {
69+ "password" : self ._config .super_secret_code ,
70+ }
71+
72+ if cmd_selection == "1" :
73+ message ["command" ] = self ._cdh .command_reset
74+ elif cmd_selection == "2" :
75+ message ["command" ] = self ._cdh .command_change_radio_modulation
76+ modulation = input ("Enter new radio modulation [FSK | LoRa]: " )
77+ message ["args" ] = [modulation ]
78+ elif cmd_selection == "3" :
79+ message ["command" ] = self ._cdh .command_send_joke
8280
83- if b != b"ACK" :
84- self ._log .info (
85- "No ACK response received, retrying..." ,
86- response = b .decode ("utf-8" ),
87- )
88- continue
81+ while True :
82+ # Turn on the radio so that it captures any received packets to buffer
83+ self ._packet_manager .listen (1 )
84+
85+ # Send the message
86+ self ._log .info (
87+ "Sending command" ,
88+ cmd = message ["command" ],
89+ args = message .get ("args" , []),
90+ )
91+ self ._packet_manager .send (json .dumps (message ).encode ("utf-8" ))
8992
90- self ._log .info ("Received ACK" )
93+ # Listen for ACK response
94+ b = self ._packet_manager .listen (1 )
95+ if b is None :
96+ self ._log .info ("No response received, retrying..." )
97+ continue
9198
92- # Now listen for the actual response
93- b = self ._packet_manager .listen (1 )
94- if b is None :
95- self ._log .info ("No response received, retrying..." )
96- continue
99+ if b != b"ACK" :
100+ self ._log .info (
101+ "No ACK response received, retrying..." ,
102+ response = b .decode ("utf-8" ),
103+ )
104+ continue
97105
98- self ._log .info ("Received response" , response = b .decode ("utf-8" ))
99- break
106+ self ._log .info ("Received ACK" )
100107
101- except KeyboardInterrupt :
102- self ._log .debug ("Keyboard interrupt received, exiting send mode." )
108+ # Now listen for the actual response
109+ b = self ._packet_manager .listen (1 )
110+ if b is None :
111+ self ._log .info ("No response received, retrying..." )
112+ continue
113+
114+ self ._log .info ("Received response" , response = b .decode ("utf-8" ))
115+ break
103116
104117 def run (self ):
105118 while True :
0 commit comments