Skip to content

Commit

Permalink
Logging Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dlktdr committed May 17, 2024
1 parent f7e6260 commit ff1192b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion firmware/src/src/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@

// Buffer Sizes for Serial/JSON
#define JSON_BUF_SIZE 3000
#define TX_RNGBUF_SIZE 1500
#define TX_RNGBUF_SIZE 2000
#define RX_RNGBUF_SIZE 1500

// Math Defines
Expand Down
37 changes: 12 additions & 25 deletions firmware/src/src/log/log_backend_htgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
static char stdout_buff[_STDOUT_BUF_SIZE];
static char outbuf[_JSONOUT_BUF_SIZE];
static int n_pend = 0;
static bool isready = true;

static uint32_t log_format_current = CONFIG_LOG_BACKEND_HTGUI_OUTPUT_DEFAULT;

Expand Down Expand Up @@ -42,18 +41,6 @@ int logescape(const char *inbuf, char *outbuf, uint32_t len, uint32_t maxlen)
return outpos+1;
}

uint16_t logescapeCRC(uint16_t crc)
{
// Characters to escape out
uint8_t crclow = crc & 0xFF;
uint8_t crchigh = (crc >> 8) & 0xFF;
if (crclow == 0x00 || crchigh == 0x01 || crclow == 0x02 || crclow == 0x03 || crclow == 0x06 || crclow == 0x15)
crclow ^= 0xFF; //?? why not..
if (crchigh == 0x00 || crchigh == 0x01 || crchigh == 0x02 || crchigh == 0x03 || crchigh == 0x06 || crchigh == 0x15)
crchigh ^= 0xFF; //?? why not..
return (uint16_t)crclow | ((uint16_t)crchigh << 8);
}

static void preprint_char(int c)
{
int printnow = 0;
Expand All @@ -69,19 +56,17 @@ static void preprint_char(int c)
printnow = 1;
}

if (n_pend >= _STDOUT_BUF_SIZE - 1) {
if (n_pend >= _STDOUT_BUF_SIZE) {
printnow = 1;
}

if (printnow) {
if( isready == true ) {
outbuf[0] = 0x01; // Start of Heading
uint32_t br = logescape(stdout_buff, outbuf+1, n_pend, _JSONOUT_BUF_SIZE-4);
outbuf[br] = 0x03; // End of Text
outbuf[br+1] = '\r';
outbuf[br+2] = '\n';
serialWrite(outbuf, br+3);
}
outbuf[0] = 0x01; // Start of Heading
uint32_t br = logescape(stdout_buff, outbuf+1, n_pend, _JSONOUT_BUF_SIZE-4);
outbuf[br] = 0x03; // End of Text
outbuf[br+1] = '\r';
outbuf[br+2] = '\n';
serialWrite(outbuf, br+3);
n_pend = 0;
stdout_buff[0] = 0;
}
Expand All @@ -98,7 +83,6 @@ static int char_out(uint8_t *data, size_t length, void *ctx)
return length;
}


LOG_OUTPUT_DEFINE(log_output_htgui, char_out, buf, sizeof(buf));

void process(const struct log_backend *const backend, union log_msg_generic *msg)
Expand All @@ -112,7 +96,10 @@ void process(const struct log_backend *const backend, union log_msg_generic *msg

void dropped(const struct log_backend *const backend, uint32_t cnt)
{

char buffer[64];
int len = snprintf(buffer, 64, "\x01 Dropped %d log messages\x03\r\n", cnt);
buffer[len] = 0;
serialWrite(buffer, len);
}

void panic(const struct log_backend *const backend)
Expand Down Expand Up @@ -144,7 +131,7 @@ void notify(const struct log_backend *const backend, enum log_backend_evt event,

const struct log_backend_api lbe = {
.process = process,
.dropped = NULL,
.dropped = dropped,
.panic = panic,
.init = init,
.is_ready = is_ready,
Expand Down
11 changes: 6 additions & 5 deletions firmware/src/src/sense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,20 @@ int sense_Init()
LOG_INF("I2C Config: 0x%x", i2c_cfg);
}

LOG_INF("Waiting for I2C Sensor Bus To Be Ready");
LOG_INF("Waiting for I2C Sensor Bus To Be Ready");
while(!device_is_ready(DEVICE_DT_GET(DT_ALIAS(i2csensor)))) {
k_msleep(10);
}
k_msleep(20);

i2c_recover_bus(i2c_dev);
LOG_INF("I2C Sensor Bus Ready");
LOG_INF("I2C Ready");

hasMag = false;

#if defined(HAS_LSM9DS1)
if (!IMU.begin()) {
LOG_ERR("Failed to initalize sensors");
LOG_ERR("Failed to initalize LSM9DS1 Sensor");
return -1;
}
#endif
Expand Down Expand Up @@ -230,6 +230,8 @@ LOG_INF("Waiting for I2C Sensor Bus To Be Ready");
bmm150_error_codes_print_result("set_config", rbslt);
}
hasMag = true;
} else {
LOG_ERR("Unable to init BMM150 - Continueing with no magnetomer\n");
}
#endif

Expand Down Expand Up @@ -1143,7 +1145,7 @@ void detectDoubleTap()
if (acc_dif > trkset.getRstOnDblTapThres())
{
timediff = time - lasttaptime;
LOG_INF("Tap detected, mag=%.1f, time=%lld, timediff=%lld", (double)acc_dif, time, timediff);
LOG_DBG("Tap detected, mag=%.1f, time=%lld, timediff=%lld", (double)acc_dif, time, timediff);

if (timediff > trkset.getRstOnDblTapMin() && timediff < trkset.getRstOnDblTapMin() + trkset.getRstOnDblTapMax())
{
Expand All @@ -1153,7 +1155,6 @@ void detectDoubleTap()

lasttaptime = time;
}

}

void gyroCalibrate()
Expand Down
12 changes: 3 additions & 9 deletions firmware/src/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int serial_init()

void serial_Thread()
{
uint8_t buffer[64];
uint8_t buffer[256];
static uint32_t datacounter = 0;
LOG_INF("Serial Thread Loaded");

Expand All @@ -154,15 +154,11 @@ void serial_Thread()
#if defined(DT_N_INST_0_zephyr_cdc_acm_uart)
// lost connection
if (dtr && !new_dtr) {
ring_buf_reset(&ringbuf_tx);
uart_tx_abort(dev);
trkset.stopAllData();
}

// gaining new connection
if (!dtr && new_dtr) {
ring_buf_reset(&ringbuf_tx);
uart_tx_abort(dev);

// Force bootloader if baud set to 1200bps TODO (Test Me)
/*uint32_t baud=0;
Expand All @@ -172,20 +168,18 @@ void serial_Thread()
NVIC_SystemReset();
}*/
}

// Port is open, send data
if (new_dtr) {
#endif
int rb_len = ring_buf_get(&ringbuf_tx, buffer, sizeof(buffer));
if (rb_len) {
int send_len = uart_fifo_fill(dev, buffer, rb_len);
int send_len = uart_fifo_fill(dev, buffer, rb_len); // TODO this is wrong, needs to be in an ISR according to the Zephyr docs
if (send_len < rb_len) {
// LOG_ERR("USB CDC Ring Buffer Full, Dropped data");
}
}

#if defined(DT_N_INST_0_zephyr_cdc_acm_uart)
} else {
ring_buf_reset(&ringbuf_tx); // Clear buffer
}
dtr = new_dtr;
#endif
Expand Down
7 changes: 5 additions & 2 deletions gui/src/boardjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ void BoardJson::dataIn(QByteArray &data)
} else if(data.left(1)[0] == (char)0x01 && data.right(1)[0] == (char)0x03) { // Log information
QByteArray unescape = unescapeLog(data.mid(1,data.length()-2));
QString logd= QString::fromLatin1(unescape);
emit addToLog(logd.toHtmlEscaped() + "\n");
if(logd.length()) {
emit addToLog(logd + "\n");
}
}

}
Expand Down Expand Up @@ -464,7 +466,8 @@ QByteArray BoardJson::unescapeLog(QByteArray data)
QByteArray rval;
for(int i=0; i < data.length(); i++) {
if(data[i] == 0x1B) {
rval.append(data[i+1] ^ 0xFF);
if(i+1 < data.length())
rval.append(data[i+1] ^ 0xFF);
i++;
} else {
rval.append(data[i]);
Expand Down
11 changes: 9 additions & 2 deletions gui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,19 @@ void MainWindow::addToLog(QString log, int ll)
QString color = "black";
if(ll==2) // Debug
color = "red";
else if(log.left(1)[0] == '[') // TODO change this to detect logger messages
color = "green";
else if(log.contains("<inf>"))
color = "darkgreen";
else if(log.contains("<err>"))
color = "red";
else if(log.contains("<wrn>"))
color = "orange";
else if(log.contains("<dbg>"))
color = "DodgerBlue";
else if(log.contains("GUI:"))
color = "blue";
else if(log.contains("\"Cmd\":\"Data\"")) // Don't show return measurment data
return;
log = log.toHtmlEscaped();

logd += "<font color=\"" + color + "\">" + log + "</font><br>";

Expand Down

0 comments on commit ff1192b

Please sign in to comment.