2023-09-10 15:33:34 +02:00
|
|
|
#ifndef _RC_SERIAL_H
|
|
|
|
#define _RC_SERIAL_H
|
|
|
|
#define jType_RC_SERIAL 1
|
|
|
|
|
2023-09-17 13:57:28 +02:00
|
|
|
#include <jmutex.h>
|
|
|
|
#include <jqueue.h>
|
|
|
|
|
2023-09-10 15:33:34 +02:00
|
|
|
#include "JuSDK.h" //https://jusax.de/git/jusax23/JuSDK
|
|
|
|
#include "juRCReceiver.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
byte type = 0x01;
|
|
|
|
byte command = 0;
|
|
|
|
char data[30];
|
|
|
|
} rcSerialData;
|
|
|
|
|
|
|
|
#define serialRFBufSize 2048
|
|
|
|
mutex_t serial_mut;
|
|
|
|
queue_t serial_rec_queue;
|
|
|
|
int serial_lb = 0;
|
|
|
|
queue_t serial_write_queue;
|
|
|
|
rcSerialData rcSerial;
|
|
|
|
|
|
|
|
void _rc_serial_init() {
|
|
|
|
mutex_init(&serial_mut);
|
|
|
|
mutex_enter_blocking(&serial_mut);
|
|
|
|
queue_init(&serial_rec_queue, 1, serialRFBufSize);
|
|
|
|
queue_init(&serial_write_queue, 1, serialRFBufSize);
|
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rc_serial_available() {
|
|
|
|
mutex_enter_blocking(&serial_mut);
|
|
|
|
bool emp_queue = queue_is_empty(&serial_rec_queue);
|
|
|
|
bool out = serial_lb > 0;
|
|
|
|
if (out && emp_queue) serial_lb = 0;
|
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
return out && !emp_queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String rc_serial_read() {
|
|
|
|
mutex_enter_blocking(&serial_mut);
|
|
|
|
|
|
|
|
String out = "";
|
|
|
|
|
|
|
|
if (serial_lb > 0) {
|
|
|
|
serial_lb--;
|
|
|
|
char curr = 0;
|
|
|
|
queue_remove_blocking(&serial_rec_queue, &curr);
|
|
|
|
while (curr != '\n') {
|
|
|
|
out += curr;
|
|
|
|
if (!queue_try_remove(&serial_rec_queue, &curr)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
bool rc_serial_write(String inn) {
|
|
|
|
mutex_enter_blocking(&serial_mut);
|
|
|
|
size_t len = inn.length();
|
2023-09-17 13:57:28 +02:00
|
|
|
if (len + 1 > serialRFBufSize - queue_get_level(&serial_write_queue)) {
|
2023-09-10 15:33:34 +02:00
|
|
|
#if defined(debug) && debug
|
|
|
|
Serial.println("<rfSerial>BufOverflow");
|
|
|
|
#endif
|
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
const char* data = inn.c_str();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
queue_try_add(&serial_write_queue, data + i);
|
|
|
|
}
|
|
|
|
const char lb = '\n';
|
|
|
|
queue_try_add(&serial_write_queue, &lb);
|
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _rc_serial_add(const char* data, size_t len) {
|
|
|
|
mutex_enter_blocking(&serial_mut);
|
|
|
|
for (size_t i = 0; i < len && i < 30; i++) {
|
|
|
|
queue_try_add(&serial_rec_queue, data + i);
|
|
|
|
if ((char)*(data + i) == '\n') serial_lb++;
|
|
|
|
}
|
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handle_serial_send() {
|
|
|
|
mutex_enter_blocking(&serial_mut);
|
|
|
|
if (rcSerial.command == 0) {
|
|
|
|
while (rcSerial.command < 30 && !queue_is_empty(&serial_write_queue)) {
|
|
|
|
queue_remove_blocking(&serial_write_queue,
|
|
|
|
rcSerial.data + (rcSerial.command++));
|
|
|
|
}
|
|
|
|
}
|
2023-09-17 13:57:28 +02:00
|
|
|
if (rcSerial.command > 0)
|
|
|
|
if (jSendNRF(&rcSerial, sizeof(rcSerial))) rcSerial.command = 0;
|
2023-09-10 15:33:34 +02:00
|
|
|
mutex_exit(&serial_mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|