From 716e6c209c9269021cda9b7e6d9abc61dc8ecbaf Mon Sep 17 00:00:00 2001 From: Marek Bel Date: Wed, 25 Jul 2018 21:18:29 +0200 Subject: [PATCH] Save 88B FLASH and fix compiler warnings: sketch/cmdqueue.cpp: In function 'bool cmdqueue_pop_front()': sketch/cmdqueue.cpp:67:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (++ bufindr; bufindr < sizeof(cmdbuffer) && cmdbuffer[bufindr] == 0; ++ bufindr) ; ^ sketch/cmdqueue.cpp: In function 'bool cmdqueue_could_enqueue_back(int, bool)': sketch/cmdqueue.cpp:170:63: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) || ^ sketch/cmdqueue.cpp:172:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr)) ^ sketch/cmdqueue.cpp:196:63: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) || ^ sketch/cmdqueue.cpp:198:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr)) ^ sketch/cmdqueue.cpp: In function 'void get_command()': sketch/cmdqueue.cpp:380:10: warning: variable 'rx_buffer_full' set but not used [-Wunused-but-set-variable] bool rx_buffer_full = false; //flag that serial rx buffer is full ^ sketch/cmdqueue.cpp: In function 'uint16_t cmdqueue_calc_sd_length()': sketch/cmdqueue.cpp:697:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ; ^ --- Firmware/cmdqueue.cpp | 29 +++++++++-------------------- Firmware/cmdqueue.h | 5 +---- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/Firmware/cmdqueue.cpp b/Firmware/cmdqueue.cpp index 1b18acfe..6bb7e42f 100644 --- a/Firmware/cmdqueue.cpp +++ b/Firmware/cmdqueue.cpp @@ -7,9 +7,9 @@ extern bool Stopped; // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT. char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT]; // Head of the circular buffer, where to read. -int bufindr = 0; +size_t bufindr = 0; // Tail of the buffer, where to write. -int bufindw = 0; +static size_t bufindw = 0; // Number of lines in cmdbuffer. int buflen = 0; // Flag for processing the current command inside the main Arduino loop(). @@ -100,7 +100,7 @@ void cmdqueue_reset() // How long a string could be pushed to the front of the command queue? // If yes, adjust bufindr to the new position, where the new command could be enqued. // len_asked does not contain the zero terminator size. -bool cmdqueue_could_enqueue_front(int len_asked) +static bool cmdqueue_could_enqueue_front(size_t len_asked) { // MAX_CMD_SIZE has to accommodate the zero terminator. if (len_asked >= MAX_CMD_SIZE) @@ -145,7 +145,7 @@ bool cmdqueue_could_enqueue_front(int len_asked) // len_asked does not contain the zero terminator size. // This function may update bufindw, therefore for the power panic to work, this function must be called // with the interrupts disabled! -bool cmdqueue_could_enqueue_back(int len_asked, bool atomic_update) +static bool cmdqueue_could_enqueue_back(size_t len_asked, bool atomic_update = false) { // MAX_CMD_SIZE has to accommodate the zero terminator. if (len_asked >= MAX_CMD_SIZE) @@ -161,7 +161,7 @@ bool cmdqueue_could_enqueue_back(int len_asked, bool atomic_update) // serial data. // How much memory to reserve for the commands pushed to the front? // End of the queue, when pushing to the end. - int endw = bufindw + len_asked + (1 + CMDHDRSIZE); + size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE); if (bufindw < bufindr) // Simple case. There is a contiguous space between the write buffer and the read buffer. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr; @@ -187,7 +187,7 @@ bool cmdqueue_could_enqueue_back(int len_asked, bool atomic_update) } else { // How much memory to reserve for the commands pushed to the front? // End of the queue, when pushing to the end. - int endw = bufindw + len_asked + (1 + CMDHDRSIZE); + size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE); if (bufindw < bufindr) // Simple case. There is a contiguous space between the write buffer and the read buffer. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr; @@ -274,7 +274,7 @@ void cmdqueue_dump_to_serial() // Currently the maximum length of a command piped through this function is around 20 characters void enquecommand(const char *cmd, bool from_progmem) { - int len = from_progmem ? strlen_P(cmd) : strlen(cmd); + size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd); // Does cmd fit the queue while leaving sufficient space at the front for the chained commands? // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd. if (cmdqueue_could_enqueue_back(len)) { @@ -317,7 +317,7 @@ bool cmd_buffer_empty() void enquecommand_front(const char *cmd, bool from_progmem) { - int len = from_progmem ? strlen_P(cmd) : strlen(cmd); + size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd); // Does cmd fit the queue? This call shall move bufindr, so the command may be copied. if (cmdqueue_could_enqueue_front(len)) { cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI; @@ -375,14 +375,10 @@ void get_command() // Test and reserve space for the new command string. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE - 1, true)) return; - - bool rx_buffer_full = false; //flag that serial rx buffer is full if (MYSERIAL.available() == RX_BUFFER_SIZE - 1) { //compare number of chars buffered in rx buffer with rx buffer size MYSERIAL.flush(); SERIAL_ECHOLNPGM("Full RX Buffer"); //if buffer was full, there is danger that reading of last gcode will not be completed - - rx_buffer_full = true; //sets flag that buffer was full } // start of serial line processing loop @@ -548,13 +544,6 @@ void get_command() } } - //add comment - /*if (rx_buffer_full == true && serial_count > 0) { //if rx buffer was full and string was not properly terminated - rx_buffer_full = false; - bufindw = bufindw - serial_count; //adjust tail of the buffer to prepare buffer for writing new command - serial_count = 0; - }*/ - #ifdef SDSUPPORT if(!card.sdprinting || serial_count!=0){ // If there is a half filled buffer from serial line, wait until return before @@ -682,7 +671,7 @@ uint16_t cmdqueue_calc_sd_length() uint16_t value; } sdlen_single; uint16_t sdlen = 0; - for (int _buflen = buflen, _bufindr = bufindr;;) { + for (size_t _buflen = buflen, _bufindr = bufindr;;) { if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) { sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1]; sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2]; diff --git a/Firmware/cmdqueue.h b/Firmware/cmdqueue.h index eb063a42..6c81db29 100644 --- a/Firmware/cmdqueue.h +++ b/Firmware/cmdqueue.h @@ -32,8 +32,7 @@ #define CMDBUFFER_RESERVE_FRONT (5*21) extern char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT]; -extern int bufindr; -extern int bufindw; +extern size_t bufindr; extern int buflen; extern bool cmdbuffer_front_already_processed; @@ -59,8 +58,6 @@ extern long Stopped_gcode_LastN; extern bool cmdqueue_pop_front(); extern void cmdqueue_reset(); -extern bool cmdqueue_could_enqueue_front(int len_asked); -extern bool cmdqueue_could_enqueue_back(int len_asked, bool atomic_update = false); #ifdef CMDBUFFER_DEBUG extern void cmdqueue_dump_to_serial_single_line(int nr, const char *p); extern void cmdqueue_dump_to_serial();