mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-03-06 23:05:53 +08:00
c-api : add toggleRxProtocol() (#60)
This function allows to enable/disable Rx protocols during the decoding process. This is useful when the Tx/Rx protocol is known in advance.
This commit is contained in:
@@ -27,10 +27,10 @@ int main() {
|
||||
const char * payload = "test";
|
||||
char decoded[16];
|
||||
|
||||
int n = ggwave_encode(instance, payload, 4, GGWAVE_TX_PROTOCOL_AUDIBLE_FAST, 50, NULL, 1);
|
||||
int n = ggwave_encode(instance, payload, 4, GGWAVE_TX_PROTOCOL_AUDIBLE_FASTEST, 50, NULL, 1);
|
||||
char waveform[n];
|
||||
|
||||
int ne = ggwave_encode(instance, payload, 4, GGWAVE_TX_PROTOCOL_AUDIBLE_FAST, 50, waveform, 0);
|
||||
int ne = ggwave_encode(instance, payload, 4, GGWAVE_TX_PROTOCOL_AUDIBLE_FASTEST, 50, waveform, 0);
|
||||
CHECK(ne > 0);
|
||||
|
||||
// not enough output buffer size to store the decoded message
|
||||
@@ -39,12 +39,22 @@ int main() {
|
||||
|
||||
// just enough size to store it
|
||||
ret = ggwave_ndecode(instance, waveform, sizeof(signed short)*ne, decoded, 4);
|
||||
CHECK(ret == 4); // success
|
||||
CHECK(ret == 4); // success
|
||||
|
||||
// unsafe method - will write the decoded output to the output buffer regardless of the size
|
||||
ret = ggwave_decode(instance, waveform, sizeof(signed short)*ne, decoded);
|
||||
CHECK(ret == 4);
|
||||
|
||||
// disable Rx protocol
|
||||
ggwave_toggleRxProtocol(instance, GGWAVE_TX_PROTOCOL_AUDIBLE_FASTEST, 0);
|
||||
ret = ggwave_ndecode(instance, waveform, sizeof(signed short)*ne, decoded, 4);
|
||||
CHECK(ret == -1); // fail
|
||||
|
||||
// enable Rx protocol
|
||||
ggwave_toggleRxProtocol(instance, GGWAVE_TX_PROTOCOL_AUDIBLE_FASTEST, 1);
|
||||
ret = ggwave_ndecode(instance, waveform, sizeof(signed short)*ne, decoded, 4);
|
||||
CHECK(ret == 4); // success
|
||||
|
||||
decoded[ret] = 0; // null-terminate the received data
|
||||
CHECK(strcmp(decoded, payload) == 0);
|
||||
|
||||
|
||||
@@ -17,3 +17,17 @@ res = ggwave.decode(instance, waveform)
|
||||
|
||||
if res != payload.encode():
|
||||
sys.exit(1)
|
||||
|
||||
# disable the Rx protocol - the decoding should fail
|
||||
ggwave.toggleRxProtocol(instance, rxProtocolId = 1, state = 0)
|
||||
res = ggwave.decode(instance, waveform)
|
||||
|
||||
if res != None:
|
||||
sys.exit(1)
|
||||
|
||||
# re-enable the Rx protocol - the decoding should succeed
|
||||
ggwave.toggleRxProtocol(instance, rxProtocolId = 1, state = 1)
|
||||
res = ggwave.decode(instance, waveform)
|
||||
|
||||
if res != payload.encode():
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user