Skip to content

Commit a33c833

Browse files
authored
Add ability to wait for autokey buffer to clear. (#41)
1 parent d7d25f1 commit a33c833

6 files changed

Lines changed: 57 additions & 1 deletion

File tree

scripts/superkey/interface.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import serial
1313
import struct
14+
import time
1415
from typing import Iterable, Optional, Tuple
1516

1617
from .constants import *
@@ -111,7 +112,7 @@ def __exit__(self, exc_type, exc_value, traceback):
111112
if self.serial is not None and self.serial.is_open:
112113
self.serial.close()
113114

114-
def autokey(self, string: str, flags: Iterable[AutokeyFlag] = []):
115+
def autokey(self, string: str, flags: Iterable[AutokeyFlag] = [], block: bool = False):
115116
"""
116117
Sends the `REQUEST_AUTOKEY_EX` command. Queues the specified string to be automatically keyed.
117118
"""
@@ -129,13 +130,32 @@ def autokey(self, string: str, flags: Iterable[AutokeyFlag] = []):
129130
self.__send_packet(MessageID.REQUEST_AUTOKEY_EX, payload)
130131
self.__check_reply_empty()
131132

133+
# If blocking was selected, wait for autokey to complete
134+
if block:
135+
self.autokey_wait()
136+
137+
def autokey_count(self) -> int:
138+
"""
139+
Sends the `REQUEST_AUTOKEY_COUNT` command. Returns the number of Morse code elements in the autokey buffer.
140+
"""
141+
self.__send_packet(MessageID.REQUEST_AUTOKEY_COUNT)
142+
return self.__check_reply('<H')[0]
143+
132144
def autokey_quick_msg(self, index: int):
133145
"""
134146
Sends the `REQUEST_AUTOKEY_QUICK_MSG` command. Keys a quick message.
135147
"""
136148
self.__send_packet(MessageID.REQUEST_AUTOKEY_QUICK_MSG, struct.pack('<B', index))
137149
self.__check_reply_empty()
138150

151+
def autokey_wait(self, delay: float = 0.25):
152+
"""
153+
Waits until the autokey buffer is empty.
154+
NOTE: This is not an interface call - it periodically polls `autokey_count()`.
155+
"""
156+
while self.autokey_count() != 0:
157+
time.sleep(delay)
158+
139159
def get_buzzer_enabled(self) -> bool:
140160
"""
141161
Sends the `REQUEST_GET_BUZZER_ENABLED` command. Returns whether the buzzer is enabled or not.

scripts/superkey/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
[
105105
# Requests
106106
'REQUEST_AUTOKEY',
107+
'REQUEST_AUTOKEY_COUNT',
107108
'REQUEST_AUTOKEY_EX',
108109
'REQUEST_AUTOKEY_QUICK_MSG',
109110
'REQUEST_GET_BUZZER_ENABLED',

src/main/application/intf_port.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ static void process_message( intf_header_t const * header, void const * payload
110110
*/
111111
static void process_message_request_autokey( intf_header_t const * header, void const * payload );
112112

113+
/**
114+
* @fn process_message_request_autokey_count( intf_header_t const *, void const * )
115+
* @brief Processes the specified interface message with the `INTF_MESSAGE_REQUEST_AUTOKEY_COUNT` message ID.
116+
*/
117+
static void process_message_request_autokey_count( intf_header_t const * header, void const * payload );
118+
113119
/**
114120
* @fn process_message_request_autokey_ex( intf_header_t const *, void const * )
115121
* @brief Processes the specified interface message with the `INTF_MESSAGE_REQUEST_AUTOKEY_EX` message ID.
@@ -417,6 +423,10 @@ static void process_message( intf_header_t const * header, void const * payload
417423
process_message_request_autokey( header, payload );
418424
break;
419425

426+
case INTF_MESSAGE_REQUEST_AUTOKEY_COUNT:
427+
process_message_request_autokey_count( header, payload );
428+
break;
429+
420430
case INTF_MESSAGE_REQUEST_AUTOKEY_EX:
421431
process_message_request_autokey_ex( header, payload );
422432
break;
@@ -570,6 +580,17 @@ static void process_message_request_autokey( intf_header_t const * header, void
570580
} /* process_message_request_autokey() */
571581

572582

583+
static void process_message_request_autokey_count( intf_header_t const * header, void const * payload )
584+
{
585+
( void )payload;
586+
VALIDATE_PAYLOAD_SIZE_OR_BAIL( 0 );
587+
588+
size_t count = keyer_autokey_count();
589+
send_packet( INTF_MESSAGE_REPLY_SUCCESS, & count, sizeof( count ) );
590+
591+
} /* process_message_request_autokey_count() */
592+
593+
573594
static void process_message_request_autokey_ex( intf_header_t const * header, void const * payload )
574595
{
575596
typedef struct

src/main/application/intf_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef uint16_t intf_message_t;
4444
enum
4545
{
4646
INTF_MESSAGE_REQUEST_AUTOKEY, /**< Queues a string to be autokeyed. */
47+
INTF_MESSAGE_REQUEST_AUTOKEY_COUNT, /**< Get number of Morse code elements in autokey buffer. */
4748
INTF_MESSAGE_REQUEST_AUTOKEY_EX, /**< Queues a string to be autokeyed with flags. */
4849
INTF_MESSAGE_REQUEST_AUTOKEY_QUICK_MSG, /**< Queues a quick message to be autokeyed.*/
4950
INTF_MESSAGE_REQUEST_GET_BUZZER_ENABLED,/**< Get buzzer enablement. */

src/main/application/keyer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ bool keyer_autokey_char_ex( char c, keyer_autokey_flag_field_t flags )
225225
} /* keyer_autokey_char_ex() */
226226

227227

228+
size_t keyer_autokey_count( void )
229+
{
230+
return( autokey_count() );
231+
232+
} /* keyer_autokey_count() */
233+
234+
228235
size_t keyer_autokey_str( char const * str )
229236
{
230237
return( keyer_autokey_str_ex( str, KEYER_AUTOKEY_FLAG_NONE ) );

src/main/application/keyer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ bool keyer_autokey_char( char c );
102102
*/
103103
bool keyer_autokey_char_ex( char c, keyer_autokey_flag_field_t flags );
104104

105+
/**
106+
* @fn keyer_autokey_count( void )
107+
* @brief Returns the number of Morse code elements currently in the autokey buffer.
108+
*/
109+
size_t keyer_autokey_count( void );
110+
105111
/**
106112
* @fn keyer_autokey_str( char const * )
107113
* @brief Adds the specified string to the keyer's autokey buffer.

0 commit comments

Comments
 (0)