diff options
68 files changed, 489 insertions, 550 deletions
diff --git a/client/cmd.cpp b/client/cmd.cpp index e365a59..539e42d 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -4,42 +4,37 @@ #include <string.h> #include "cmd.h" -#include "pb-types.h" +#include "i2c.h" +#include "parse.h" #include "pb-buf.h" #include "pb-send.h" +#include "pb-types.h" #include "rl.h" -#include "i2c.h" -#include "parse.h" -char* consume_token(char* input, const char* ifs) { +char * consume_token(char * input, const char * ifs) { strtok(input, ifs); return strtok(NULL, "\0"); } -void cmd_exit(char*) { - exit(EXIT_SUCCESS); -} +void cmd_exit(char *) { exit(EXIT_SUCCESS); } -void cmd_test(char*) { - const char* data = "Hello world!"; +void cmd_test(char *) { + const char * data = "Hello world!"; i2c_send(0x39, data, strlen(data)); } -void cmd_help(char*) { +void cmd_help(char *) { printf("List of available commands:\n"); for (size_t i = 0; i < cmds_length; i++) { cmd_t cmd = cmds[i]; printf(" %-*s", 10, cmd.name); - if (cmd.info != NULL) - printf(" %s", cmd.info); + if (cmd.info != NULL) printf(" %s", cmd.info); printf("\n"); } - printf( - "\n" - "See man pbc(1) for more info about specific commands\n" - "Hint: you can use the TAB key to autocomplete commands\n" - ); + printf("\n" + "See man pbc(1) for more info about specific commands\n" + "Hint: you can use the TAB key to autocomplete commands\n"); } void cmd_send(char * addr_str) { @@ -48,30 +43,31 @@ void cmd_send(char * addr_str) { return; } - char* data_str = consume_token(addr_str, IFS); + char * data_str = consume_token(addr_str, IFS); if (data_str == NULL) { printf("error: no data\n"); return; } - char* end; + char * end; uint16_t addr = strtol(addr_str, &end, 0); if (addr_str + strlen(addr_str) != end) { printf("address format error\n"); return; } - char* data; + char * data; size_t data_size; int err = strtodata(data_str, &data, &data_size); if (err <= 0) { - printf("data format error at index %d:\n%s\n%*s^\n", - -err, data_str, -err, ""); + printf("data format error at index %d:\n%s\n%*s^\n", -err, data_str, + -err, ""); return; } - printf("sending char data[%lu = 0x%02lx] to 0x%02x\n", data_size, data_size, addr); + printf("sending char data[%lu = 0x%02lx] to 0x%02x\n", data_size, data_size, + addr); i2c_send(addr, data, data_size); free(data); @@ -82,7 +78,7 @@ static void cmd_set_state(char * line, pb_global_state_t state) { printf("error: no address\n"); return; } - + i2c_addr_t addr = strtol(line, NULL, 0); pb_buf_t buf = pb_send_state_set(state); @@ -90,22 +86,14 @@ static void cmd_set_state(char * line, pb_global_state_t state) { pb_buf_free(&buf); } -void cmd_reset(char * line) { - cmd_set_state(line, PB_GS_IDLE); -} +void cmd_reset(char * line) { cmd_set_state(line, PB_GS_IDLE); } -void cmd_skip(char * line) { - cmd_set_state(line, PB_GS_SOLVED); -} +void cmd_skip(char * line) { cmd_set_state(line, PB_GS_SOLVED); } extern bool i2c_dump_send; extern bool i2c_dump_recv; const char * dump_modes[] = { - "none", - "send", - "recv", - "both", - NULL, + "none", "send", "recv", "both", NULL, }; void cmd_dump(char * mode) { consume_token(mode, IFS); @@ -121,7 +109,7 @@ void cmd_dump(char * mode) { printf("mode \"%s\" unknown\n", mode); } -char** cmd_dump_complete(const char * text, int begin, int end) { +char ** cmd_dump_complete(const char * text, int begin, int end) { int word = rl_word(rl_line_buffer, begin); if (word == 1) return rl_complete_list(text, dump_modes); return NULL; diff --git a/client/cmd.h b/client/cmd.h index 7fee700..babc8f2 100644 --- a/client/cmd.h +++ b/client/cmd.h @@ -28,7 +28,7 @@ typedef void cmd_handle_t(char * line); * \return Array of \c char* with suggestions. The array is terminated by a * NULL pointer. */ -typedef char** cmd_complete_t(const char* text, int start, int end); +typedef char ** cmd_complete_t(const char * text, int start, int end); /** * \internal @@ -36,8 +36,9 @@ typedef char** cmd_complete_t(const char* text, int start, int end); */ typedef struct { cmd_handle_t * handle; //!< Handler function (required) - const char* name; //!< Command name (required) - const char* info; //!< Command info (shown in help command) (optional = NULL) + const char * name; //!< Command name (required) + const char * + info; //!< Command info (shown in help command) (optional = NULL) cmd_complete_t * complete; //!< Completion function (optional = NULL) } cmd_t; @@ -247,4 +248,3 @@ static const cmd_t cmds[] = { static const size_t cmds_length = sizeof(cmds) / sizeof(cmds[0]); /// \} - diff --git a/client/i2c.cpp b/client/i2c.cpp index 18be4bc..3613baf 100644 --- a/client/i2c.cpp +++ b/client/i2c.cpp @@ -6,11 +6,11 @@ #include "sock.h" #include "xxd.h" -#include "pb.h" #include "pb-buf.h" +#include "pb-mod.h" #include "pb-msg.h" #include "pb-types.h" -#include "pb-mod.h" +#include "pb.h" #ifdef DEBUG bool i2c_dump_send = true; @@ -27,7 +27,7 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) { .length = data_size, }; - char* packed; + char * packed; size_t size; if (!i2ctcp_write(&msg, &packed, &size)) return; @@ -66,9 +66,9 @@ void i2c_dump(const i2ctcp_msg_t * msg) { _rl_printf_start(); const char * direction = send ? "send" : "recv"; - printf("[I2C %s] 0x%02x -> 0x%02x data(0x%02lx):\n", direction, sender, receiver, msg->length); + printf("[I2C %s] 0x%02x -> 0x%02x data(0x%02lx):\n", direction, sender, + receiver, msg->length); xxd(msg->data, msg->length); _rl_printf_stop(); } - diff --git a/client/i2c.h b/client/i2c.h index 538624a..30d9d8a 100644 --- a/client/i2c.h +++ b/client/i2c.h @@ -1,7 +1,7 @@ #pragma once -#include <stdint.h> #include <stddef.h> +#include <stdint.h> #include "i2ctcpv1.h" @@ -36,4 +36,3 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size); void i2c_dump(const i2ctcp_msg_t * msg); /// \} - diff --git a/client/main.cpp b/client/main.cpp index 5c26107..c69fbcc 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,21 +1,21 @@ +#include <cstdint> #include <cstdio> #include <cstdlib> -#include <cstdint> #include <exception> #include "rl.h" #include "sock.h" -PBSocket* sock; +PBSocket * sock; -int main(int argc, char** argv) { +int main(int argc, char ** argv) { if (argc < 2) { printf("usage: %s addr [port]\n", argv[0]); return EXIT_FAILURE; } // parse arguments - char* addr = argv[1]; + char * addr = argv[1]; uint16_t port = 9191; if (argc >= 3) port = atoi(argv[2]); @@ -23,7 +23,7 @@ int main(int argc, char** argv) { try { // connect to TCP socket (automatically spawns thread) sock->sock_connect(); - } catch (const std::exception& e) { + } catch (const std::exception & e) { printf("error: %s\n", e.what()); return EXIT_FAILURE; } @@ -35,4 +35,3 @@ int main(int argc, char** argv) { return ret; } - diff --git a/client/mod.c b/client/mod.c index b67a61d..4ce4e8e 100644 --- a/client/mod.c +++ b/client/mod.c @@ -1,9 +1,8 @@ -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "client"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_MAIN; void pb_i2c_recv(const uint8_t * buf, size_t sz) {} void pb_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz) {} - diff --git a/client/parse.cpp b/client/parse.cpp index 16f0781..516445b 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -1,7 +1,7 @@ +#include <netinet/in.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdio.h> -#include <netinet/in.h> #include "parse.h" @@ -25,15 +25,14 @@ static int parse_string(const char * str, char * data, size_t * offset) { for (i = 1; i < len && str[i] != '\0'; i++, *offset += 1) { char c = str[i]; - if (c == closing) - return i + 1; // +1 for closing quote + if (c == closing) return i + 1; // +1 for closing quote if (escape && c == '\\') { char x = str[i + 1]; - if (x == '0') c = '\0'; - else if (x == 't') c = '\t'; - else if (x == 'n') c = '\n'; - else if (x == 'r') c = '\r'; + if (x == '0') c = '\0'; + else if (x == 't') c = '\t'; + else if (x == 'n') c = '\n'; + else if (x == 'r') c = '\r'; else if (x == '\\') c = '\\'; else if (x == '\"') c = '\"'; else if (x == '\'') c = '\''; @@ -41,8 +40,7 @@ static int parse_string(const char * str, char * data, size_t * offset) { i++; } - if (data != NULL) - data[*offset] = c; + if (data != NULL) data[*offset] = c; } return -i; @@ -53,7 +51,7 @@ static int parse_hexstr(const char * str, char * data, size_t * offset) { int i = 0; // check if token contains at least one colon - const char* colon = strchr(str, ':'); + const char * colon = strchr(str, ':'); if (colon == NULL) return -i; if (colon >= str + len) return -i; @@ -63,11 +61,9 @@ static int parse_hexstr(const char * str, char * data, size_t * offset) { size_t c = 0; while (c < len) { // count bytes in bytestring - if (strspn(str + c, SET_HEX) != 2) - return -i -c; + if (strspn(str + c, SET_HEX) != 2) return -i - c; - if (data != NULL) - data[*offset] = strtol(str + c, NULL, 16) & 0xff; + if (data != NULL) data[*offset] = strtol(str + c, NULL, 16) & 0xff; c += 2; *offset += 1; @@ -92,16 +88,16 @@ static int parse_number(const char * str, char * data, size_t * offset) { if (len > 2 && strncmp(str, "0x", 2) == 0) { // hexadecimal prefix base = 16; i += 2; - }/* else if (len > 1 && strncmp(str, "0", 1) == 0) { // octal prefix + } /* else if (len > 1 && strncmp(str, "0", 1) == 0) { // octal prefix base = 8; i += 1; }*/ - const char* set; + const char * set; // if (base == 8) set = SET_OCT; if (base == 10) set = SET_DEC; if (base == 16) set = SET_HEX; - + size_t len_ok = strspn(str + i, set) + i; if (len != len_ok) return -len_ok; @@ -109,9 +105,9 @@ static int parse_number(const char * str, char * data, size_t * offset) { if (base == 16) { size_t prefixless = len - i; switch (prefixless) { - case 2: // 8-bit (2 hex characters) - case 4: // 16-bit - case 8: // 32-bit + case 2: // 8-bit (2 hex characters) + case 4: // 16-bit + case 8: // 32-bit case 16: // 64-bit break; default: @@ -133,12 +129,12 @@ static int parse_number(const char * str, char * data, size_t * offset) { case 2: number = htons(number); // TODO: check if the endianness is OK, or reverse these *offset indices* - data[*offset + 1] = (number) & 0xff; + data[*offset + 1] = (number) & 0xff; data[*offset + 0] = (number >>= 8) & 0xff; break; case 4: number = htonl(number); - data[*offset + 3] = (number) & 0xff; + data[*offset + 3] = (number) & 0xff; data[*offset + 2] = (number >>= 8) & 0xff; data[*offset + 1] = (number >>= 8) & 0xff; data[*offset + 0] = (number >>= 8) & 0xff; @@ -150,7 +146,7 @@ static int parse_number(const char * str, char * data, size_t * offset) { return len; } -static int _strtodata_main(const char * str, char* data, size_t * offset) { +static int _strtodata_main(const char * str, char * data, size_t * offset) { size_t len = strlen(str); int i, run; @@ -177,8 +173,7 @@ int strtodata(const char * str, char ** data, size_t * size) { if (ret <= 0) return ret; // on error // 2nd pass: convert string literals into binary data - *data = (char*) malloc(*size); + *data = (char *) malloc(*size); size_t written = 0; return _strtodata_main(str, *data, &written); } - diff --git a/client/parse.h b/client/parse.h index 1beb714..8427bb1 100644 --- a/client/parse.h +++ b/client/parse.h @@ -17,9 +17,9 @@ //! Decimal digit character set #define SET_DEC "0123456789" //! Hexadecimal digit character set -#define SET_HEX SET_DEC"abcdefABCDEF" +#define SET_HEX SET_DEC "abcdefABCDEF" //! (Hexadecimal) byte string character set -#define SET_HEX_STR SET_HEX":" +#define SET_HEX_STR SET_HEX ":" /** * \brief modify \p token to point to the first token when broken up on \p ifs @@ -34,7 +34,7 @@ * * \return the remaining data after \p token and the first \p ifs */ -char* consume_token(char * token, const char * ifs); +char * consume_token(char * token, const char * ifs); /** * \brief convert string with literals into raw data @@ -53,4 +53,3 @@ char* consume_token(char * token, const char * ifs); int strtodata(const char * str, char ** data, size_t * size); /// \} - diff --git a/client/rl.cpp b/client/rl.cpp index 82d1412..cc3de69 100644 --- a/client/rl.cpp +++ b/client/rl.cpp @@ -1,16 +1,16 @@ -#include <stdlib.h> -#include <stdio.h> -#include <stdbool.h> #include <stdarg.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> -#include <readline/readline.h> #include <readline/history.h> +#include <readline/readline.h> -#include "rl.h" #include "cmd.h" #include "parse.h" +#include "rl.h" -static char* saved_line; +static char * saved_line; static int saved_point, saved_end; void _rl_printf_start() { @@ -35,7 +35,7 @@ void _rl_printf_stop() { free(saved_line); } -void rl_printf(const char *fmt, ...) { +void rl_printf(const char * fmt, ...) { _rl_printf_start(); va_list args; @@ -46,13 +46,12 @@ void rl_printf(const char *fmt, ...) { _rl_printf_stop(); } -static void cli_cmd(char* cmd) { +static void cli_cmd(char * cmd) { cmd += strspn(cmd, IFS); // skip leading whitespace - char* line = consume_token(cmd, IFS); + char * line = consume_token(cmd, IFS); for (size_t i = 0; i < cmds_length; i++) { - if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0) - continue; + if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0) continue; cmds[i].handle(line); return; @@ -61,7 +60,7 @@ static void cli_cmd(char* cmd) { printf("unknown command!\n"); } -static char* rl_completion_entries(const char *text, int state) { +static char * rl_completion_entries(const char * text, int state) { static size_t i = 0; if (state == 0) i = 0; @@ -76,7 +75,7 @@ static char* rl_completion_entries(const char *text, int state) { return NULL; } -static char** rl_attempted_completion(const char * text, int start, int end) { +static char ** rl_attempted_completion(const char * text, int start, int end) { // do not suggest filenames rl_attempted_completion_over = 1; @@ -90,7 +89,8 @@ static char** rl_attempted_completion(const char * text, int start, int end) { for (size_t i = 0; i < cmds_length; i++) { cmd_t cmd = cmds[i]; if (cmd.complete == NULL) continue; - if (strncmp(cmd.name, rl_line_buffer + cmd_start, cmd_len) != 0) continue; + if (strncmp(cmd.name, rl_line_buffer + cmd_start, cmd_len) != 0) + continue; return cmd.complete(text, start, end); } @@ -99,7 +99,7 @@ static char** rl_attempted_completion(const char * text, int start, int end) { } int cli_main() { - char* input = NULL; + char * input = NULL; rl_attempted_completion_function = rl_attempted_completion; while (1) { @@ -111,7 +111,7 @@ int cli_main() { add_history(input); cli_cmd(input); - } + } return EXIT_SUCCESS; } @@ -132,22 +132,22 @@ typedef struct { const char * word; const char ** options; } _rl_complete_list_data_t; -char** rl_complete_list(const char * word, const char ** options) { +char ** rl_complete_list(const char * word, const char ** options) { _rl_complete_list_data_t data = { .word = word, .options = options, }; - return rl_completion_matches((char *) &data, [](const char * text, int state) -> char * { - _rl_complete_list_data_t data = *(_rl_complete_list_data_t *) text; - static size_t i = 0; - if (state == 0) i = 0; - - while (data.options[i] != NULL) { - const char * option = data.options[i++]; - if (strncmp(data.word, option, strlen(data.word)) == 0) - return strdup(option); - } - return NULL; - }); + return rl_completion_matches( + (char *) &data, [](const char * text, int state) -> char * { + _rl_complete_list_data_t data = *(_rl_complete_list_data_t *) text; + static size_t i = 0; + if (state == 0) i = 0; + + while (data.options[i] != NULL) { + const char * option = data.options[i++]; + if (strncmp(data.word, option, strlen(data.word)) == 0) + return strdup(option); + } + return NULL; + }); } - diff --git a/client/rl.h b/client/rl.h index 4a5b2c1..ff30602 100644 --- a/client/rl.h +++ b/client/rl.h @@ -55,4 +55,3 @@ int rl_word(const char * line, int cursor); char ** rl_complete_list(const char * word, const char * options[]); /// \} - diff --git a/client/sock.cpp b/client/sock.cpp index c123411..7c959c6 100644 --- a/client/sock.cpp +++ b/client/sock.cpp @@ -1,23 +1,23 @@ #include <arpa/inet.h> +#include <cstdio> #include <cstring> +#include <errno.h> +#include <netinet/in.h> #include <stdexcept> -#include <unistd.h> -#include <cstdio> -#include <sys/types.h> #include <sys/socket.h> -#include <netinet/in.h> -#include <errno.h> +#include <sys/types.h> #include <thread> +#include <unistd.h> +#include "i2c.h" #include "i2ctcpv1.h" -#include "sock.h" #include "rl.h" -#include "i2c.h" +#include "sock.h" using std::logic_error; using std::thread; -PBSocket::PBSocket() { } +PBSocket::PBSocket() {} PBSocket::PBSocket(const char * addr, uint16_t port) : PBSocket() { set_server(addr, port); } @@ -47,7 +47,7 @@ void PBSocket::sock_connect() { _fd = socket(AF_INET, SOCK_STREAM, 0); if (_fd < 0) throw logic_error("socket create failed"); - + struct sockaddr_in server = { .sin_family = AF_INET, .sin_port = htons(_port), @@ -55,7 +55,7 @@ void PBSocket::sock_connect() { .s_addr = inet_addr(_addr), }, }; - int ret = connect(_fd, (struct sockaddr*) &server, sizeof(server)); + int ret = connect(_fd, (struct sockaddr *) &server, sizeof(server)); if (ret != 0) throw logic_error(strerror(errno)); this->_thread = new thread(&PBSocket::sock_task, this); @@ -75,7 +75,7 @@ void PBSocket::sock_task() { i2ctcp_msg_t input; i2ctcp_read_reset(&input); - while(1) { + while (1) { char buf[80]; ssize_t bytes = read(_fd, buf, sizeof(buf)); @@ -105,4 +105,3 @@ void PBSocket::sock_task() { sock_close(); } - diff --git a/client/sock.h b/client/sock.h index 5b62861..dbe6d6a 100644 --- a/client/sock.h +++ b/client/sock.h @@ -52,7 +52,7 @@ private: void sock_close(); //! Pointer to thread running \c sock_task() - std::thread* _thread = nullptr; + std::thread * _thread = nullptr; /** * \brief IP address of server to connect to @@ -69,7 +69,6 @@ private: }; //! Singleton \c PBSocket instance -extern PBSocket* sock; +extern PBSocket * sock; /// \} - diff --git a/client/xxd.c b/client/xxd.c index 5d83635..2b30dfb 100644 --- a/client/xxd.c +++ b/client/xxd.c @@ -1,5 +1,5 @@ -#include <stdio.h> #include <ctype.h> +#include <stdio.h> #include "xxd.h" @@ -33,12 +33,9 @@ void xxd(const char * data, size_t size) { continue; } - if (isprint(data[i])) - printf("%c", data[i]); - else - printf("."); + if (isprint(data[i])) printf("%c", data[i]); + else printf("."); } printf("|\n"); } } - diff --git a/client/xxd.h b/client/xxd.h index ede9fff..b9ade3c 100644 --- a/client/xxd.h +++ b/client/xxd.h @@ -23,4 +23,3 @@ void xxd(const char * data, size_t size); #ifdef __cplusplus } #endif - diff --git a/lib/i2ctcp/i2ctcpv1.c b/lib/i2ctcp/i2ctcpv1.c index f70cbb1..1855cff 100644 --- a/lib/i2ctcp/i2ctcpv1.c +++ b/lib/i2ctcp/i2ctcpv1.c @@ -1,6 +1,6 @@ #include <mpack.h> #ifndef MIN -#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif #include "i2ctcpv1.h" @@ -26,9 +26,7 @@ int i2ctcp_read(i2ctcp_msg_t * target, const char * buf, size_t buf_sz) { return target->_rdata; } -void i2ctcp_read_reset(i2ctcp_msg_t * target) { - target->_rdata = 0; -} +void i2ctcp_read_reset(i2ctcp_msg_t * target) { target->_rdata = 0; } bool i2ctcp_write(const i2ctcp_msg_t * target, char ** buf, size_t * buf_sz) { mpack_writer_t writer; @@ -40,4 +38,3 @@ bool i2ctcp_write(const i2ctcp_msg_t * target, char ** buf, size_t * buf_sz) { // finish writing return mpack_writer_destroy(&writer) == mpack_ok; } - diff --git a/lib/i2ctcp/i2ctcpv1.h b/lib/i2ctcp/i2ctcpv1.h index e9bc0d9..f79b2e2 100644 --- a/lib/i2ctcp/i2ctcpv1.h +++ b/lib/i2ctcp/i2ctcpv1.h @@ -1,8 +1,8 @@ #pragma once +#include <stdbool.h> #include <stddef.h> #include <stdint.h> -#include <stdbool.h> #ifdef __cplusplus extern "C" { @@ -37,9 +37,10 @@ extern "C" { //! I2C over TCP message (v1) typedef struct { uint16_t addr; //!< I2C address - char * data; //!< message content + char * data; //!< message content size_t length; //!< message size - size_t _rdata; //!< \private remaining bytes to read until message is complete + size_t + _rdata; //!< \private remaining bytes to read until message is complete } i2ctcp_msg_t; /** @@ -96,4 +97,3 @@ bool i2ctcp_write(const i2ctcp_msg_t * target, char ** buf, size_t * buf_sz); #ifdef __cplusplus } #endif - diff --git a/lib/mpack/mpack.h b/lib/mpack/mpack.h index 33521c4..1fbea67 100644 --- a/lib/mpack/mpack.h +++ b/lib/mpack/mpack.h @@ -30,11 +30,11 @@ extern "C" { * * \return Amount of bytes read */ -size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, size_t count); +size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, + size_t count); /// \} #ifdef __cplusplus } #endif - diff --git a/lib/mpack/read-remaining.c b/lib/mpack/read-remaining.c index 46b5815..8161c25 100644 --- a/lib/mpack/read-remaining.c +++ b/lib/mpack/read-remaining.c @@ -1,10 +1,9 @@ #include "mpack.h" -size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, size_t count) { +size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, + size_t count) { size_t limit = mpack_reader_remaining(reader, NULL); - if (0 < count && count < limit) - limit = count; + if (0 < count && count < limit) limit = count; MPACK_MEMCPY(p, reader->data, limit); return limit; } - diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp index 27237d2..500edfa 100644 --- a/lib/pbdrv/drv/arduino/mod.cpp +++ b/lib/pbdrv/drv/arduino/mod.cpp @@ -3,14 +3,14 @@ #include <avr/delay.h> #include <FreeRTOS.h> -#include <timers.h> #include <task.h> +#include <timers.h> -#include "../../pb.h" -#include "../../pb-mod.h" -#include "../../pb-types.h" #include "../../pb-buf.h" #include "../../pb-mem.h" +#include "../../pb-mod.h" +#include "../../pb-types.h" +#include "../../pb.h" static void async_pb_i2c_recv(void * _msg, uint32_t _) { pb_buf_t * msg = (pb_buf_t *) _msg; @@ -23,8 +23,7 @@ static void recv_event(int bytes) { pb_buf_t * msg = (pb_buf_t *) pb_malloc(sizeof(pb_buf_t)); msg->data = (char *) pb_malloc(bytes); msg->size = 0; - while (Wire.available()) - msg->data[msg->size++] = Wire.read(); + while (Wire.available()) msg->data[msg->size++] = Wire.read(); // defer pb_i2c_recv call xTimerPendFunctionCallFromISR(async_pb_i2c_recv, msg, 0, NULL); @@ -66,7 +65,7 @@ void init(void); //! FreeRTOS loop task void loop_task() { - for(;;) { + for (;;) { loop(); if (serialEventRun) serialEventRun(); } @@ -90,8 +89,8 @@ int main(void) { init(); // call arduino internal setup setup(); // call regular arduino setup pb_setup(); // call pbdrv-mod setup - xTaskCreate((TaskFunction_t) loop_task, "loop", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL); + xTaskCreate((TaskFunction_t) loop_task, "loop", configMINIMAL_STACK_SIZE, + NULL, tskIDLE_PRIORITY + 1, NULL); vTaskStartScheduler(); // start freertos scheduler return 0; } - diff --git a/lib/pbdrv/drv/rp2040/mod.c b/lib/pbdrv/drv/rp2040/mod.c index bca38d0..b031af9 100644 --- a/lib/pbdrv/drv/rp2040/mod.c +++ b/lib/pbdrv/drv/rp2040/mod.c @@ -1,11 +1,11 @@ -#include "../../pb.h" -#include "../../pb-types.h" +#include "../../pb-buf.h" #include "../../pb-mod.h" #include "../../pb-send.h" -#include "../../pb-buf.h" +#include "../../pb-types.h" +#include "../../pb.h" -#include <hardware/i2c.h> #include <hardware/gpio.h> +#include <hardware/i2c.h> #include <pico/i2c_slave.h> #define PB_I2C_S i2c0 @@ -17,7 +17,7 @@ uint8_t i2c_msg_buf[BUF_SIZE]; size_t i2c_msg_buf_sz = 0; // This function is called from the I2C ISR -static void recv_event(i2c_inst_t *i2c, i2c_slave_event_t event) { +static void recv_event(i2c_inst_t * i2c, i2c_slave_event_t event) { switch (event) { case I2C_SLAVE_RECEIVE: { if (i2c_msg_buf_sz == BUF_SIZE) return; @@ -29,7 +29,8 @@ static void recv_event(i2c_inst_t *i2c, i2c_slave_event_t event) { i2c_msg_buf_sz = 0; break; } - default: break; + default: + break; } } @@ -46,4 +47,3 @@ __weak void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) { // false to write stop condition to i2c bus i2c_write_timeout_us(PB_I2C_M, addr, buf, sz, false, PB_TIMEOUT_US); } - diff --git a/lib/pbdrv/drv/rp2040/pb-mod.h b/lib/pbdrv/drv/rp2040/pb-mod.h index 96edc70..7a4399e 100644 --- a/lib/pbdrv/drv/rp2040/pb-mod.h +++ b/lib/pbdrv/drv/rp2040/pb-mod.h @@ -18,4 +18,3 @@ void pb_setup(); #endif #include "../../pb-mod.h" - diff --git a/lib/pbdrv/ext/freertos/pb-mem.c b/lib/pbdrv/ext/freertos/pb-mem.c index 6647f05..d5e615a 100644 --- a/lib/pbdrv/ext/freertos/pb-mem.c +++ b/lib/pbdrv/ext/freertos/pb-mem.c @@ -4,14 +4,10 @@ #include "../../pb-types.h" /// \ingroup pb_ext_freertos -inline void * pb_malloc(size_t sz) { - return pvPortMalloc(sz); -} +inline void * pb_malloc(size_t sz) { return pvPortMalloc(sz); } /// \ingroup pb_ext_freertos -inline void pb_free(void * ptr) { - vPortFree(ptr); -} +inline void pb_free(void * ptr) { vPortFree(ptr); } /// \ingroup pb_ext_freertos __weak inline void * pb_realloc(void * ptr, size_t sz) { @@ -21,16 +17,15 @@ __weak inline void * pb_realloc(void * ptr, size_t sz) { /// \ingroup pb_ext_freertos __weak void * pb_memcpy(void * dest, const void * src, size_t sz) { for (size_t offset = 0; offset < sz; offset++) - *((char*) dest + offset) = *((char*) src + offset); + *((char *) dest + offset) = *((char *) src + offset); return dest; } /// \ingroup pb_ext_freertos __weak int pb_memcmp(const void * a, const void * b, size_t sz) { for (size_t offset = 0; offset < sz; offset++) { - int diff = *((char*) a + offset) - *((char*) b + offset); + int diff = *((char *) a + offset) - *((char *) b + offset); if (diff != 0) return diff; } return 0; } - diff --git a/lib/pbdrv/ext/stdlib/pb-mem.c b/lib/pbdrv/ext/stdlib/pb-mem.c index 328efbb..1b0b6e6 100644 --- a/lib/pbdrv/ext/stdlib/pb-mem.c +++ b/lib/pbdrv/ext/stdlib/pb-mem.c @@ -4,19 +4,13 @@ #include "../../pb-mem.h" /// \ingroup pb_ext_stdlib -inline void * pb_malloc(size_t sz) { - return malloc(sz); -} +inline void * pb_malloc(size_t sz) { return malloc(sz); } /// \ingroup pb_ext_stdlib -inline void pb_free(void * ptr) { - free(ptr); -} +inline void pb_free(void * ptr) { free(ptr); } /// \ingroup pb_ext_stdlib -inline void * pb_realloc(void * ptr, size_t sz) { - return realloc(ptr, sz); -} +inline void * pb_realloc(void * ptr, size_t sz) { return realloc(ptr, sz); } /// \ingroup pb_ext_stdlib void * pb_memcpy(void * dest, const void * src, size_t sz) { @@ -27,4 +21,3 @@ void * pb_memcpy(void * dest, const void * src, size_t sz) { int pb_memcmp(const void * a, const void * b, size_t sz) { return memcmp(a, b, sz); } - diff --git a/lib/pbdrv/mpack-config.h b/lib/pbdrv/mpack-config.h index 5b1215c..f08dd3b 100644 --- a/lib/pbdrv/mpack-config.h +++ b/lib/pbdrv/mpack-config.h @@ -20,4 +20,3 @@ // disable unused features (causes errors?) #define MPACK_NODE 0 #define MPACK_BUILDER 0 - diff --git a/lib/pbdrv/pb-buf.c b/lib/pbdrv/pb-buf.c index 3d6cb8a..44befa4 100644 --- a/lib/pbdrv/pb-buf.c +++ b/lib/pbdrv/pb-buf.c @@ -6,4 +6,3 @@ void pb_buf_free(pb_buf_t * buf) { pb_free(buf->data); buf->data = NULL; } - diff --git a/lib/pbdrv/pb-buf.h b/lib/pbdrv/pb-buf.h index 8b4bb10..403f420 100644 --- a/lib/pbdrv/pb-buf.h +++ b/lib/pbdrv/pb-buf.h @@ -33,4 +33,3 @@ void pb_buf_free(pb_buf_t * buf); #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-mem.h b/lib/pbdrv/pb-mem.h index 4d0f995..6dd15ea 100644 --- a/lib/pbdrv/pb-mem.h +++ b/lib/pbdrv/pb-mem.h @@ -71,4 +71,3 @@ int pb_memcmp(const void * a, const void * b, size_t sz); #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-mod.c b/lib/pbdrv/pb-mod.c index 0342391..8375e9a 100644 --- a/lib/pbdrv/pb-mod.c +++ b/lib/pbdrv/pb-mod.c @@ -1,8 +1,8 @@ +#include "pb-mod.h" #include "pb-buf.h" #include "pb-msg.h" -#include "pb-types.h" -#include "pb-mod.h" #include "pb-route.h" +#include "pb-types.h" //! fallback module name __weak const char * PB_MOD_NAME = "???"; @@ -10,9 +10,7 @@ __weak const char * PB_MOD_NAME = "???"; //! [private] placeholder global state variable static pb_global_state_t _global_state = PB_GS_NOINIT; -__weak pb_global_state_t pb_hook_mod_state_read() { - return _global_state; -} +__weak pb_global_state_t pb_hook_mod_state_read() { return _global_state; } __weak void pb_hook_mod_state_write(pb_global_state_t state) { _global_state = state; @@ -28,17 +26,15 @@ __weak void pb_i2c_recv(const uint8_t * data, size_t sz) { pb_msg_t * msg = pb_msg_read(&buf); if (msg == NULL) return; // invalid message if (msg->cmd == NULL) return; // invalid message - + pb_route_msg(msg); pb_msg_free(msg); } -__weak bool pb_hook_i2c_recv(const uint8_t * data, size_t sz) { - return false; -} +__weak bool pb_hook_i2c_recv(const uint8_t * data, size_t sz) { return false; } -__weak bool pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * data, size_t sz) { +__weak bool pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * data, + size_t sz) { return false; } - diff --git a/lib/pbdrv/pb-mod.h b/lib/pbdrv/pb-mod.h index 0050869..e1c4ca7 100644 --- a/lib/pbdrv/pb-mod.h +++ b/lib/pbdrv/pb-mod.h @@ -135,4 +135,3 @@ bool pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz); #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-msg.c b/lib/pbdrv/pb-msg.c index 7fd6662..502beff 100644 --- a/lib/pbdrv/pb-msg.c +++ b/lib/pbdrv/pb-msg.c @@ -2,12 +2,12 @@ #include <mpack.h> +#include "pb-mem.h" #include "pb-msg.h" #include "pb-serial.h" -#include "pb-mem.h" pb_buf_t pb_msg_write(const pb_msg_t * msg) { - pb_buf_t buf = { 0 }; + pb_buf_t buf = {0}; if (msg == NULL) return buf; buf.data = pb_malloc(MPACK_BUFFER_SIZE); @@ -43,4 +43,3 @@ void pb_msg_free(pb_msg_t * msg) { // free message container that was created in \p pb_msg_read pb_free(msg); } - diff --git a/lib/pbdrv/pb-msg.h b/lib/pbdrv/pb-msg.h index ff5bcde..12b908f 100644 --- a/lib/pbdrv/pb-msg.h +++ b/lib/pbdrv/pb-msg.h @@ -1,7 +1,7 @@ #pragma once -#include "pb-types.h" #include "pb-buf.h" +#include "pb-types.h" #ifdef __cplusplus extern "C" { @@ -57,4 +57,3 @@ void pb_msg_free(pb_msg_t * msg); #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-route.c b/lib/pbdrv/pb-route.c index 5a7bd67..94d15d1 100644 --- a/lib/pbdrv/pb-route.c +++ b/lib/pbdrv/pb-route.c @@ -1,18 +1,22 @@ #include "pb-route.h" +#include "pb-mem.h" #include "pb-mod.h" #include "pb-send.h" #include "pb-types.h" -#include "pb-mem.h" __weak bool pb_hook_route_msg(pb_msg_t * msg) { return false; } __weak void pb_route_msg(pb_msg_t * msg) { if (pb_hook_route_msg(msg)) return; switch (msg->type) { - case PB_CMD_PROP: return pb_route_cmd_prop(msg); - case PB_CMD_STATE: return pb_route_cmd_state(msg); - case PB_CMD_MAGIC: return pb_route_cmd_magic(msg); - default: return; + case PB_CMD_PROP: + return pb_route_cmd_prop(msg); + case PB_CMD_STATE: + return pb_route_cmd_state(msg); + case PB_CMD_MAGIC: + return pb_route_cmd_magic(msg); + default: + return; } } @@ -21,10 +25,14 @@ __weak void pb_route_cmd_prop(pb_msg_t * msg) { if (pb_hook_route_cmd_prop(msg)) return; switch (msg->action) { - case PB_ACTION_REQ: return pb_route_cmd_prop_req(msg); - case PB_ACTION_RES: return pb_route_cmd_prop_res(msg); - case PB_ACTION_SET: return pb_route_cmd_prop_set(msg); - default: return; + case PB_ACTION_REQ: + return pb_route_cmd_prop_req(msg); + case PB_ACTION_RES: + return pb_route_cmd_prop_res(msg); + case PB_ACTION_SET: + return pb_route_cmd_prop_set(msg); + default: + return; } } @@ -33,10 +41,14 @@ __weak void pb_route_cmd_state(pb_msg_t * msg) { if (pb_hook_route_cmd_state(msg)) return; switch (msg->action) { - case PB_ACTION_REQ: return pb_route_cmd_state_req(msg); - case PB_ACTION_RES: return pb_route_cmd_state_res(msg); - case PB_ACTION_SET: return pb_route_cmd_state_set(msg); - default: return; + case PB_ACTION_REQ: + return pb_route_cmd_state_req(msg); + case PB_ACTION_RES: + return pb_route_cmd_state_res(msg); + case PB_ACTION_SET: + return pb_route_cmd_state_set(msg); + default: + return; } } @@ -45,9 +57,12 @@ __weak void pb_route_cmd_magic(pb_msg_t * msg) { if (pb_hook_route_cmd_magic(msg)) return; switch (msg->action) { - case PB_ACTION_REQ: return pb_route_cmd_magic_req(msg); - case PB_ACTION_RES: return pb_route_cmd_magic_res(msg); - default: return; + case PB_ACTION_REQ: + return pb_route_cmd_magic_req(msg); + case PB_ACTION_RES: + return pb_route_cmd_magic_res(msg); + default: + return; } } @@ -59,9 +74,7 @@ __weak void pb_route_cmd_prop_set(pb_msg_t * msg) {} //! last known global state of last STATE REQ sender (i.e. main controller) static pb_global_state_t _main_state = PB_GS_NOINIT; __weak void pb_hook_ev_main_state_update(pb_global_state_t state) {} -__weak void pb_hook_ev_module_init() { - pb_hook_mod_state_write(PB_GS_IDLE); -} +__weak void pb_hook_ev_module_init() { pb_hook_mod_state_write(PB_GS_IDLE); } __weak void pb_route_cmd_state_req(pb_msg_t * msg) { pb_global_state_t own_state = pb_hook_mod_state_read(); @@ -91,12 +104,12 @@ __weak void pb_route_cmd_magic_req(pb_msg_t * msg) { // return early if magic has wrong size if (cmd->_magic_size != sizeof(pb_cmd_magic_req)) return; // // return early if magic doesn't match - if (pb_memcmp(cmd->magic, pb_cmd_magic_req, sizeof(pb_cmd_magic_req)) != 0) return; + if (pb_memcmp(cmd->magic, pb_cmd_magic_req, sizeof(pb_cmd_magic_req)) != 0) + return; pb_buf_t buf = pb_send_magic_res(); pb_send_reply(msg, &buf); pb_buf_free(&buf); } -__weak void pb_route_cmd_magic_res(pb_msg_t * msg) { } - +__weak void pb_route_cmd_magic_res(pb_msg_t * msg) {} diff --git a/lib/pbdrv/pb-route.h b/lib/pbdrv/pb-route.h index 5c28f4d..ac9672d 100644 --- a/lib/pbdrv/pb-route.h +++ b/lib/pbdrv/pb-route.h @@ -215,4 +215,3 @@ void pb_hook_ev_module_init(); #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-send.c b/lib/pbdrv/pb-send.c index dc34c44..b6efd3e 100644 --- a/lib/pbdrv/pb-send.c +++ b/lib/pbdrv/pb-send.c @@ -117,4 +117,3 @@ pb_buf_t pb_send_magic_res() { }; return pb_msg_write(&msg); } - diff --git a/lib/pbdrv/pb-send.h b/lib/pbdrv/pb-send.h index 7e21eda..2b6afa0 100644 --- a/lib/pbdrv/pb-send.h +++ b/lib/pbdrv/pb-send.h @@ -1,7 +1,7 @@ #pragma once -#include "pb-types.h" #include "pb-buf.h" +#include "pb-types.h" #ifdef __cplusplus extern "C" { @@ -130,4 +130,3 @@ pb_buf_t pb_send_magic_res(); #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-serial.c b/lib/pbdrv/pb-serial.c index b9ee4b1..b1b48a3 100644 --- a/lib/pbdrv/pb-serial.c +++ b/lib/pbdrv/pb-serial.c @@ -13,10 +13,14 @@ void pb_ser_w(mpack_writer_t * writer, const pb_msg_t * cmd) { if (cmd->cmd == NULL) return; switch (cmd->type) { - case PB_CMD_PROP: return pb_ser_w_cmd_prop(writer, cmd); - case PB_CMD_STATE: return pb_ser_w_cmd_state(writer, cmd); - case PB_CMD_MAGIC: return pb_ser_w_cmd_magic(writer, cmd); - default: break; + case PB_CMD_PROP: + return pb_ser_w_cmd_prop(writer, cmd); + case PB_CMD_STATE: + return pb_ser_w_cmd_state(writer, cmd); + case PB_CMD_MAGIC: + return pb_ser_w_cmd_magic(writer, cmd); + default: + break; } } void pb_ser_r(mpack_reader_t * reader, pb_msg_t * cmd) { @@ -25,20 +29,28 @@ void pb_ser_r(mpack_reader_t * reader, pb_msg_t * cmd) { cmd->sender = mpack_expect_u16(reader); switch (cmd->type) { - case PB_CMD_PROP: return pb_ser_r_cmd_prop(reader, cmd); - case PB_CMD_STATE: return pb_ser_r_cmd_state(reader, cmd); - case PB_CMD_MAGIC: return pb_ser_r_cmd_magic(reader, cmd); - default: break; + case PB_CMD_PROP: + return pb_ser_r_cmd_prop(reader, cmd); + case PB_CMD_STATE: + return pb_ser_r_cmd_state(reader, cmd); + case PB_CMD_MAGIC: + return pb_ser_r_cmd_magic(reader, cmd); + default: + break; } } void pb_ser_free(pb_msg_t * cmd) { if (cmd == NULL) return; switch (cmd->type) { - case PB_CMD_PROP: return pb_ser_free_cmd_prop(cmd); - case PB_CMD_STATE: return pb_ser_free_cmd_state(cmd); - case PB_CMD_MAGIC: return pb_ser_free_cmd_magic(cmd); - default: break; + case PB_CMD_PROP: + return pb_ser_free_cmd_prop(cmd); + case PB_CMD_STATE: + return pb_ser_free_cmd_state(cmd); + case PB_CMD_MAGIC: + return pb_ser_free_cmd_magic(cmd); + default: + break; } } @@ -108,4 +120,3 @@ void pb_ser_free_cmd_magic(pb_msg_t * _msg) { _msg->cmd = NULL; } } - diff --git a/lib/pbdrv/pb-serial.h b/lib/pbdrv/pb-serial.h index 79f08d7..433a9a5 100644 --- a/lib/pbdrv/pb-serial.h +++ b/lib/pbdrv/pb-serial.h @@ -83,4 +83,3 @@ pb_ser_free_t pb_ser_free_cmd_magic; #ifdef __cplusplus } #endif - diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h index ef3df54..686ec64 100644 --- a/lib/pbdrv/pb-types.h +++ b/lib/pbdrv/pb-types.h @@ -1,8 +1,8 @@ #pragma once #include <stdbool.h> -#include <stdint.h> #include <stddef.h> +#include <stdint.h> #ifdef __cplusplus extern "C" { @@ -82,13 +82,13 @@ typedef enum { * * The size of this array can be obtained by \c sizeof(pb_cmd_magic_req). */ -static const char pb_cmd_magic_req[] = { 0x70, 0x75, 0x7a, 0x62, 0x75, 0x73 }; +static const char pb_cmd_magic_req[] = {0x70, 0x75, 0x7a, 0x62, 0x75, 0x73}; /** * \brief Magic reply from puzzle module back to main controller (="gaming") * * The size of this array can be obtained by \c sizeof(pb_cmd_magic_res). */ -static const char pb_cmd_magic_res[] = { 0x67, 0x61, 0x6d, 0x69, 0x6e, 0x67 }; +static const char pb_cmd_magic_res[] = {0x67, 0x61, 0x6d, 0x69, 0x6e, 0x67}; //! puzzle bus message header / container (shared by all commands) typedef struct { @@ -142,4 +142,3 @@ typedef struct { #ifdef __cplusplus } #endif - diff --git a/main/FreeRTOSConfig.h b/main/FreeRTOSConfig.h index c811296..546780d 100644 --- a/main/FreeRTOSConfig.h +++ b/main/FreeRTOSConfig.h @@ -66,4 +66,3 @@ #define INCLUDE_xTaskGetHandle 1 #define INCLUDE_xTaskResumeFromISR 1 #define INCLUDE_xQueueGetMutexHolder 1 - diff --git a/main/blink.c b/main/blink.c index 956e910..1a7c205 100644 --- a/main/blink.c +++ b/main/blink.c @@ -1,6 +1,6 @@ #include <FreeRTOS.h> -#include <task.h> #include <pico/cyw43_arch.h> +#include <task.h> #include "blink.h" #include "config.h" @@ -13,4 +13,3 @@ void blink_task() { vTaskDelay(1000 / portTICK_PERIOD_MS); } } - diff --git a/main/config.def.h b/main/config.def.h index 1ec8a5c..2639c40 100644 --- a/main/config.def.h +++ b/main/config.def.h @@ -1,6 +1,6 @@ #pragma once -#include <pico/cyw43_arch.h> #include <cyw43_country.h> +#include <pico/cyw43_arch.h> /** * \ingroup main @@ -130,4 +130,3 @@ /// \} /// \} - @@ -1,15 +1,15 @@ #include <FreeRTOS.h> -#include <task.h> -#include <stdio.h> +#include <hardware/i2c.h> +#include <pico/stdlib.h> #include <stddef.h> #include <stdint.h> -#include <pico/stdlib.h> -#include <hardware/i2c.h> +#include <stdio.h> +#include <task.h> -#include "i2c.h" -#include "pb-mod.h" #include "config.h" +#include "i2c.h" #include "pb-buf.h" +#include "pb-mod.h" #include "pb-send.h" //! Puzzle module handle @@ -21,7 +21,8 @@ typedef struct { static pb_global_state_t _global_state = PB_GS_IDLE; puzzle_module_t modules[CFG_PB_MOD_MAX]; // stolen from lib/pico-sdk/src/rp2_common/hardware_i2c/i2c.c -#define i2c_reserved_addr(addr) (((addr) & 0x78) == 0 || ((addr) & 0x78) == 0x78) +#define i2c_reserved_addr(addr) \ + (((addr) & 0x78) == 0 || ((addr) & 0x78) == 0x78) size_t modules_size = 0; static void bus_scan() { @@ -63,11 +64,11 @@ static void update_state() { for (size_t i = 0; i < modules_size; i++) { // find first module that is idle - pb_global_state_t module_state = modules[i].state; + pb_global_state_t module_state = modules[i].state; if (module_state != PB_GS_IDLE) continue; pb_buf_t buff = pb_send_state_set(PB_GS_PLAYING); - pb_i2c_send(modules[i].sender, (uint8_t*)buff.data, buff.size); + pb_i2c_send(modules[i].sender, (uint8_t *) buff.data, buff.size); pb_buf_free(&buff); } } @@ -84,7 +85,7 @@ void bus_task() { // do a scan of the bus bus_scan(); - while(1) { + while (1) { // send my state to all puzzle modules state_exchange(); @@ -106,7 +107,7 @@ void bus_task() { */ void pb_route_cmd_magic_res(pb_msg_t * msg) { if (modules_size == CFG_PB_MOD_MAX) return; - modules[modules_size++] = (puzzle_module_t) { + modules[modules_size++] = (puzzle_module_t){ .sender = msg->sender, .state = PB_GS_NOINIT, }; @@ -123,11 +124,6 @@ void pb_route_cmd_state_res(pb_msg_t * msg) { } } -pb_global_state_t pb_hook_mod_state_read() { - return _global_state; -} - -void pb_hook_mod_state_write(pb_global_state_t state) { - _global_state = state; -} +pb_global_state_t pb_hook_mod_state_read() { return _global_state; } +void pb_hook_mod_state_write(pb_global_state_t state) { _global_state = state; } @@ -15,4 +15,3 @@ void bus_task(); /// \} - diff --git a/main/init.c b/main/init.c index 25fa5e3..6105c0d 100644 --- a/main/init.c +++ b/main/init.c @@ -1,17 +1,15 @@ #include <FreeRTOS.h> #include <task.h> -#include <pico/stdio.h> #include <pico/cyw43_arch.h> +#include <pico/stdio.h> #include "config.h" #include "init.h" -#include "tasks.h" #include "pb-mod.h" +#include "tasks.h" -static void init_stdio() { - stdio_init_all(); -} +static void init_stdio() { stdio_init_all(); } static void init_cyw34() { if (cyw43_arch_init_with_country(CFG_NET_COUNTRY)) @@ -22,7 +20,8 @@ static void init_wifi() { // enable 'station' mode (connect to an access point instead of acting like one) cyw43_arch_enable_sta_mode(); - if (cyw43_arch_wifi_connect_timeout_ms(CFG_NET_SSID, CFG_NET_PASS, CFG_NET_AUTH, CFG_NET_CONN_TIMEOUT)) + if (cyw43_arch_wifi_connect_timeout_ms(CFG_NET_SSID, CFG_NET_PASS, + CFG_NET_AUTH, CFG_NET_CONN_TIMEOUT)) panic("cyw43_arch_wifi_connect failed\n"); // TODO: announce hostname(?) @@ -59,6 +58,6 @@ void init() { init_stdio(); // defer other initialization until the task scheduler is running (important) - xTaskCreate((TaskFunction_t) async_init, "init", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 4, NULL); + xTaskCreate((TaskFunction_t) async_init, "init", configMINIMAL_STACK_SIZE, + NULL, tskIDLE_PRIORITY + 4, NULL); } - diff --git a/main/lwipopts.h b/main/lwipopts.h index b2b6e76..eb000d8 100644 --- a/main/lwipopts.h +++ b/main/lwipopts.h @@ -89,4 +89,3 @@ #define LWIP_TIMEVAL_PRIVATE 0 #define LWIP_TCPIP_CORE_LOCKING_INPUT 1 - diff --git a/main/main.c b/main/main.c index 1c615fc..3e69de0 100644 --- a/main/main.c +++ b/main/main.c @@ -7,4 +7,3 @@ int main() { init(); vTaskStartScheduler(); } - @@ -1,9 +1,8 @@ #include <FreeRTOS.h> #include <task.h> -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "main controller"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_MAIN; - diff --git a/main/sock.c b/main/sock.c index c97ad04..69c1341 100644 --- a/main/sock.c +++ b/main/sock.c @@ -1,20 +1,21 @@ #include <pico/stdio.h> +#include <lwip/api.h> #include <lwip/opt.h> #include <lwip/sys.h> -#include <lwip/api.h> #include <string.h> -#include "init.h" #include "config.h" #include "i2ctcpv1.h" -#include "sock.h" +#include "init.h" #include "pb-mod.h" +#include "sock.h" -struct netconn* current_connection = NULL; +struct netconn * current_connection = NULL; i2ctcp_msg_t recv_msg; -static void sock_dump_msg(i2c_addr_t addr, const uint8_t * data, size_t data_size) { +static void sock_dump_msg(i2c_addr_t addr, const uint8_t * data, + size_t data_size) { if (current_connection == NULL) return; i2ctcp_msg_t send_msg = { @@ -37,7 +38,8 @@ static void sock_dump_msg(i2c_addr_t addr, const uint8_t * data, size_t data_siz bool pb_hook_i2c_send(i2c_addr_t addr, const uint8_t * data, size_t data_size) { sock_dump_msg(addr, data, data_size); - return addr == PB_MOD_ADDR; // stop processing message if it is sent to myself + return addr + == PB_MOD_ADDR; // stop processing message if it is sent to myself } bool pb_hook_i2c_recv(const uint8_t * data, size_t data_size) { @@ -45,7 +47,8 @@ bool pb_hook_i2c_recv(const uint8_t * data, size_t data_size) { return false; } -static void sock_fwd_msg(i2c_addr_t addr, const uint8_t * data, size_t data_size) { +static void sock_fwd_msg(i2c_addr_t addr, const uint8_t * data, + size_t data_size) { if (addr == PB_MOD_ADDR) { // addressed to me = act as recieved pb_i2c_recv(data, data_size); @@ -55,14 +58,14 @@ static void sock_fwd_msg(i2c_addr_t addr, const uint8_t * data, size_t data_size } } -void recv_handler(struct netconn* conn, struct netbuf* buf) { +void recv_handler(struct netconn * conn, struct netbuf * buf) { i2ctcp_read_reset(&recv_msg); do { - char* data; + char * data; uint16_t len; - netbuf_data(buf, (void**)&data, &len); - + netbuf_data(buf, (void **) &data, &len); + // continue early if more data is needed to complete message if (i2ctcp_read(&recv_msg, data, len) != 0) continue; @@ -74,12 +77,11 @@ void recv_handler(struct netconn* conn, struct netbuf* buf) { netbuf_delete(buf); } -void accept_handler(struct netconn* conn) { +void accept_handler(struct netconn * conn) { current_connection = conn; - struct netbuf* buf; - while (netconn_recv(conn, &buf) == ERR_OK) - recv_handler(conn, buf); + struct netbuf * buf; + while (netconn_recv(conn, &buf) == ERR_OK) recv_handler(conn, buf); netconn_close(conn); netconn_delete(conn); @@ -89,15 +91,14 @@ void accept_handler(struct netconn* conn) { void serve_task() { printf("starting server...\n"); - struct netconn* conn = netconn_new(NETCONN_TCP); + struct netconn * conn = netconn_new(NETCONN_TCP); netconn_bind(conn, IP_ADDR_ANY, CFG_SRV_PORT); netconn_listen(conn); - printf("listening on %s:%d\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), CFG_SRV_PORT); + printf("listening on %s:%d\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), + CFG_SRV_PORT); while (1) { - struct netconn* incoming; - if (netconn_accept(conn, &incoming) == ERR_OK) - accept_handler(incoming); + struct netconn * incoming; + if (netconn_accept(conn, &incoming) == ERR_OK) accept_handler(incoming); } } - diff --git a/main/sock.h b/main/sock.h index a151973..5abc87f 100644 --- a/main/sock.h +++ b/main/sock.h @@ -14,4 +14,3 @@ void serve_task(); /// \} - diff --git a/main/tasks.c b/main/tasks.c index 253c47b..d83525d 100644 --- a/main/tasks.c +++ b/main/tasks.c @@ -9,9 +9,12 @@ #include "sock.h" void init_tasks() { - xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); + xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, + NULL, tskIDLE_PRIORITY + 2, NULL); #ifndef CFG_SRV_DISABLE - xTaskCreate((TaskFunction_t) serve_task, "serve", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); + xTaskCreate((TaskFunction_t) serve_task, "serve", configMINIMAL_STACK_SIZE, + NULL, tskIDLE_PRIORITY + 2, NULL); #endif - xTaskCreate((TaskFunction_t) bus_task, "bus", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); + xTaskCreate((TaskFunction_t) bus_task, "bus", configMINIMAL_STACK_SIZE, + NULL, tskIDLE_PRIORITY + 2, NULL); } diff --git a/puzzle/dummy/FreeRTOSConfig.h b/puzzle/dummy/FreeRTOSConfig.h index 072fdf7..35d9cae 100644 --- a/puzzle/dummy/FreeRTOSConfig.h +++ b/puzzle/dummy/FreeRTOSConfig.h @@ -49,4 +49,3 @@ #define INCLUDE_xTaskGetHandle 1 #define INCLUDE_xTaskResumeFromISR 1 #define INCLUDE_xQueueGetMutexHolder 1 - diff --git a/puzzle/dummy/main.cpp b/puzzle/dummy/main.cpp index d611014..5f78086 100644 --- a/puzzle/dummy/main.cpp +++ b/puzzle/dummy/main.cpp @@ -1,5 +1,4 @@ #include <Arduino.h> -void setup() { } -void loop() { } - +void setup() {} +void loop() {} diff --git a/puzzle/dummy/mod.c b/puzzle/dummy/mod.c index 058a585..559c4d3 100644 --- a/puzzle/dummy/mod.c +++ b/puzzle/dummy/mod.c @@ -1,6 +1,5 @@ -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "dummy"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_DUMMY; - diff --git a/puzzle/neo/FreeRTOSConfig.h b/puzzle/neo/FreeRTOSConfig.h index c0acc49..d3a5d2f 100644 --- a/puzzle/neo/FreeRTOSConfig.h +++ b/puzzle/neo/FreeRTOSConfig.h @@ -49,4 +49,3 @@ #define INCLUDE_xTaskGetHandle 1 #define INCLUDE_xTaskResumeFromISR 1 #define INCLUDE_xQueueGetMutexHolder 1 - diff --git a/puzzle/neo/main.cpp b/puzzle/neo/main.cpp index 831f97e..f636b55 100644 --- a/puzzle/neo/main.cpp +++ b/puzzle/neo/main.cpp @@ -1,9 +1,9 @@ -#include <Arduino.h> -#include <Wire.h> -#include <Adafruit_NeoTrellis.h> -#include "pb-types.h" #include "pb-mod.h" +#include "pb-types.h" #include "pb.h" +#include <Adafruit_NeoTrellis.h> +#include <Arduino.h> +#include <Wire.h> #define MATRIX_SIZE 8 #define LED_COLOR_ON 0x0000FF // Color of the LEDs in ON state @@ -12,11 +12,14 @@ #define LED_COLOR_ORANGE 0xFFA500 // Orange color for IDLE state Adafruit_NeoTrellis t_array[MATRIX_SIZE / 4][MATRIX_SIZE / 4] = { - { Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_1), Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_2) }, - { Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_3), Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_4) }, + {Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_1), + Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_2)}, + {Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_3), + Adafruit_NeoTrellis(PB_ADDR_ADA_NEO_4)}, }; -Adafruit_MultiTrellis trellis((Adafruit_NeoTrellis *)t_array, MATRIX_SIZE / 4, MATRIX_SIZE / 4); +Adafruit_MultiTrellis trellis((Adafruit_NeoTrellis *) t_array, MATRIX_SIZE / 4, + MATRIX_SIZE / 4); bool neoMatrix[MATRIX_SIZE][MATRIX_SIZE]; // To track state of each pixel @@ -36,17 +39,19 @@ void toggleAdjacentLEDs(int x, int y) { int nx = x + dx, ny = y + dy; if (nx >= 0 && nx < MATRIX_SIZE && ny >= 0 && ny < MATRIX_SIZE) { neoMatrix[nx][ny] = !neoMatrix[nx][ny]; - trellis.setPixelColor(nx * MATRIX_SIZE + ny, neoMatrix[nx][ny] ? LED_COLOR_ON : LED_COLOR_OFF); + trellis.setPixelColor(nx * MATRIX_SIZE + ny, + neoMatrix[nx][ny] ? LED_COLOR_ON + : LED_COLOR_OFF); } } } } - bool isNeoPuzzleSolved() { for (int i = 0; i < MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE; j++) { - if (neoMatrix[i][j]) return false; // If any LED is on, puzzle is not solved + if (neoMatrix[i][j]) + return false; // If any LED is on, puzzle is not solved } } return true; @@ -69,7 +74,8 @@ TrellisCallback buttonCallback(keyEvent evt) { void setup() { Serial.begin(115200); - while (!Serial); // Wait for Serial to be read + while (!Serial) + ; // Wait for Serial to be read if (!trellis.begin()) { Serial.println("Failed to initialize NeoTrellis"); pb_hook_mod_state_write(PB_GS_NOINIT); @@ -77,14 +83,16 @@ void setup() { } void set_game_field() { - if (gamefield == false){ + if (gamefield == false) { // Initialize the matrix with a checkerboard pattern bool toggle = false; for (int i = 0; i < MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE; j++) { neoMatrix[i][j] = toggle; toggle = !toggle; - trellis.setPixelColor(i * MATRIX_SIZE + j, neoMatrix[i][j] ? LED_COLOR_ON : LED_COLOR_OFF); + trellis.setPixelColor(i * MATRIX_SIZE + j, neoMatrix[i][j] + ? LED_COLOR_ON + : LED_COLOR_OFF); } toggle = !toggle; } @@ -100,13 +108,9 @@ void set_game_field() { } } -pb_global_state_t pb_hook_mod_state_read() { - return puzzleState; -} +pb_global_state_t pb_hook_mod_state_read() { return puzzleState; } -void pb_hook_mod_state_write(pb_global_state_t state) { - puzzleState = state; -} +void pb_hook_mod_state_write(pb_global_state_t state) { puzzleState = state; } void flashCorners(uint32_t color) { unsigned long currentMillis = millis(); @@ -120,14 +124,19 @@ void flashCorners(uint32_t color) { int baseIndex = (i * 4 * MATRIX_SIZE) + (j * 4); if (ledState) { trellis.setPixelColor(baseIndex, color); // Top-left corner - trellis.setPixelColor(baseIndex + 3, color); // Top-right corner - trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE, color); // Bottom-left corner - trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE + 3, color); // Bottom-right corner + trellis.setPixelColor(baseIndex + 3, + color); // Top-right corner + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE, + color); // Bottom-left corner + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE + 3, + color); // Bottom-right corner } else { trellis.setPixelColor(baseIndex, LED_COLOR_OFF); trellis.setPixelColor(baseIndex + 3, LED_COLOR_OFF); - trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE, LED_COLOR_OFF); - trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE + 3, LED_COLOR_OFF); + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE, + LED_COLOR_OFF); + trellis.setPixelColor(baseIndex + 3 * MATRIX_SIZE + 3, + LED_COLOR_OFF); } } } @@ -136,7 +145,7 @@ void flashCorners(uint32_t color) { } void loop() { - switch(puzzleState) { + switch (puzzleState) { case PB_GS_PLAYING: set_game_field(); trellis.read(); // Process button events @@ -155,4 +164,3 @@ void loop() { break; } } - diff --git a/puzzle/neo/mod.c b/puzzle/neo/mod.c index 7157d22..32b348a 100644 --- a/puzzle/neo/mod.c +++ b/puzzle/neo/mod.c @@ -1,6 +1,5 @@ -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "neotrellis"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_NEOTRELLIS; - diff --git a/puzzle/smoketest/FreeRTOSConfig.h b/puzzle/smoketest/FreeRTOSConfig.h index 81c487f..44fc902 100644 --- a/puzzle/smoketest/FreeRTOSConfig.h +++ b/puzzle/smoketest/FreeRTOSConfig.h @@ -49,4 +49,3 @@ #define INCLUDE_xTaskGetHandle 1 #define INCLUDE_xTaskResumeFromISR 1 #define INCLUDE_xQueueGetMutexHolder 1 - diff --git a/puzzle/smoketest/main.cpp b/puzzle/smoketest/main.cpp index 3169ed5..35a4ec9 100644 --- a/puzzle/smoketest/main.cpp +++ b/puzzle/smoketest/main.cpp @@ -1,6 +1,6 @@ -#include <Arduino.h> -#include "lib/pbdrv/pb-types.h" #include "lib/pbdrv/pb-mod.h" +#include "lib/pbdrv/pb-types.h" +#include <Arduino.h> //! LED pin (on-board LED on Arduino Uno board) \ingroup puz_smoketest #define EXTERNAL_LED_PIN 13 @@ -9,17 +9,13 @@ pb_global_state_t state = PB_GS_NOINIT; -pb_global_state_t pb_hook_mod_state_read() { - return state; -} +pb_global_state_t pb_hook_mod_state_read() { return state; } -void pb_hook_mod_state_write(pb_global_state_t _state) { - state = _state; -} +void pb_hook_mod_state_write(pb_global_state_t _state) { state = _state; } void setup() { - pinMode(EXTERNAL_LED_PIN, OUTPUT); - pinMode(BUTTON_PIN, INPUT_PULLUP); + pinMode(EXTERNAL_LED_PIN, OUTPUT); + pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { @@ -28,4 +24,3 @@ void loop() { if (!digitalRead(BUTTON_PIN)) // button is inverted state = PB_GS_SOLVED; } - diff --git a/puzzle/smoketest/mod.c b/puzzle/smoketest/mod.c index a1e2843..d9d5824 100644 --- a/puzzle/smoketest/mod.c +++ b/puzzle/smoketest/mod.c @@ -1,6 +1,5 @@ -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "smoketest"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_SMOKETEST; - diff --git a/puzzle/vault/FreeRTOSConfig.h b/puzzle/vault/FreeRTOSConfig.h index c0acc49..d3a5d2f 100644 --- a/puzzle/vault/FreeRTOSConfig.h +++ b/puzzle/vault/FreeRTOSConfig.h @@ -49,4 +49,3 @@ #define INCLUDE_xTaskGetHandle 1 #define INCLUDE_xTaskResumeFromISR 1 #define INCLUDE_xQueueGetMutexHolder 1 - diff --git a/puzzle/vault/main.cpp b/puzzle/vault/main.cpp index 88d5b35..ba65c12 100644 --- a/puzzle/vault/main.cpp +++ b/puzzle/vault/main.cpp @@ -1,7 +1,7 @@ +#include "lib/pbdrv/pb-mod.h" +#include "lib/pbdrv/pb-types.h" #include <Arduino.h> #include <TM1637Display.h> -#include "lib/pbdrv/pb-types.h" -#include "lib/pbdrv/pb-mod.h" #define TOTAL_LEVELS 5 #define ROWS 4 @@ -9,38 +9,39 @@ #define CLK 2 #define DIO 3 #define SOLVED_PIN 53 -#define I2C_MODULE_ADDRESS 0x08 // Address of the puzzle module -#define HANDSHAKE_RECEIVED {0x70, 0x75, 0x7a, 0x62, 0x75, 0x73} // Magic command for the handshake -#define HANDSHAKE_SEND {0x67, 0x61, 0x6d, 0x69, 0x6e, 0x67} // Magic command for the handshake -#define REQUEST_STATE_CMD 0x53 // 'S' to request the game state +#define I2C_MODULE_ADDRESS 0x08 // Address of the puzzle module +#define HANDSHAKE_RECEIVED \ + { 0x70, 0x75, 0x7a, 0x62, 0x75, 0x73 } // Magic command for the handshake +#define HANDSHAKE_SEND \ + { 0x67, 0x61, 0x6d, 0x69, 0x6e, 0x67 } // Magic command for the handshake +#define REQUEST_STATE_CMD 0x53 // 'S' to request the game state const int ROW_PINS[ROWS] = {7, 6, 5, 4}; const int COL_PINS[COLS] = {10, 9, 8}; -const char* validButtons[TOTAL_LEVELS] = {"A2", "B1", "D3", "C2", "C3"}; +const char * validButtons[TOTAL_LEVELS] = {"A2", "B1", "D3", "C2", "C3"}; const char bombCode[] = "1234"; const uint8_t SEGMENT_MAP[] = { - 0b00111111, // 0 - 0b00000110, // 1 - 0b01011011, // 2 - 0b01001111, // 3 - 0b01100110, // 4 - 0b01101101, // 5 - 0b01111101, // 6 - 0b00000111, // 7 - 0b01111111, // 8 - 0b01101111, // 9 - 0b01110111, // A - 0b01111100, // B - 0b00111001, // C - 0b01011110, // D - 0b01111001, // E - 0b01110001 // F - // Add other letters if needed + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b01110111, // A + 0b01111100, // B + 0b00111001, // C + 0b01011110, // D + 0b01111001, // E + 0b01110001 // F + // Add other letters if needed }; // This array of level codes matches the codes you might display per level. -const char* levelCodes[TOTAL_LEVELS] = {"A1", "B2", "D1", "C3", "A2"}; - +const char * levelCodes[TOTAL_LEVELS] = {"A1", "B2", "D1", "C3", "A2"}; // Puzzle state pb_global_state_t puzzleState = PB_GS_NOINIT; @@ -50,142 +51,138 @@ TM1637Display display(CLK, DIO); int currentLevel = 0; void blink_display(int num) { - if (num == 1) { - // Display "1111" with leading zeros shown if necessary - display.showNumberDecEx(1111, 0b11111111, true); - } else if (num == 0) { - // Display "0000" with leading zeros shown if necessary - display.showNumberDecEx(0, 0b11111111, true); - } - delay(500); - display.clear(); - delay(500); + if (num == 1) { + // Display "1111" with leading zeros shown if necessary + display.showNumberDecEx(1111, 0b11111111, true); + } else if (num == 0) { + // Display "0000" with leading zeros shown if necessary + display.showNumberDecEx(0, 0b11111111, true); + } + delay(500); + display.clear(); + delay(500); } +void display_final_code(const char * code) { + uint8_t segs[4] = {0, 0, 0, 0}; + int numDigits = strlen(code); + numDigits = numDigits > 4 ? 4 : numDigits; -void display_final_code(const char* code) { - uint8_t segs[4] = {0, 0, 0, 0}; - int numDigits = strlen(code); - numDigits = numDigits > 4 ? 4 : numDigits; + for (int i = 0; i < numDigits; i++) { + segs[i] = display.encodeDigit(code[i] - '0'); + } - for (int i = 0; i < numDigits; i++) { - segs[i] = display.encodeDigit(code[i] - '0'); - } - - display.setSegments(segs, numDigits, 0); + display.setSegments(segs, numDigits, 0); } void check_button_press() { - for (int col = 0; col < COLS; col++) { - digitalWrite(COL_PINS[col], LOW); - for (int row = 0; row < ROWS; row++) { - if (digitalRead(ROW_PINS[row]) == LOW) { - delay(50); - if (digitalRead(ROW_PINS[row]) == LOW) { - char keyPress[3] = {'A' + row, '1' + col, '\0'}; - Serial.print("Keypress detected: "); - Serial.println(keyPress); - if (strcmp(keyPress, validButtons[currentLevel]) == 0) { - currentLevel++; - if (currentLevel >= TOTAL_LEVELS) { - pb_hook_mod_state_write(PB_GS_SOLVED); - Serial.println("Puzzle solved!"); - display.showNumberDec(currentLevel + 1, true); - digitalWrite(SOLVED_PIN, HIGH); - } - } else { - currentLevel = 0; - } - while (digitalRead(ROW_PINS[row]) == LOW) {} // Ensure button release - } - } - } - digitalWrite(COL_PINS[col], HIGH); - } + for (int col = 0; col < COLS; col++) { + digitalWrite(COL_PINS[col], LOW); + for (int row = 0; row < ROWS; row++) { + if (digitalRead(ROW_PINS[row]) == LOW) { + delay(50); + if (digitalRead(ROW_PINS[row]) == LOW) { + char keyPress[3] = {'A' + row, '1' + col, '\0'}; + Serial.print("Keypress detected: "); + Serial.println(keyPress); + if (strcmp(keyPress, validButtons[currentLevel]) == 0) { + currentLevel++; + if (currentLevel >= TOTAL_LEVELS) { + pb_hook_mod_state_write(PB_GS_SOLVED); + Serial.println("Puzzle solved!"); + display.showNumberDec(currentLevel + 1, true); + digitalWrite(SOLVED_PIN, HIGH); + } + } else { + currentLevel = 0; + } + while (digitalRead(ROW_PINS[row]) == LOW) { + } // Ensure button release + } + } + } + digitalWrite(COL_PINS[col], HIGH); + } } void initialize_system() { - for (int i = 0; i < ROWS; i++) { - pinMode(ROW_PINS[i], INPUT_PULLUP); - } - for (int i = 0; i < COLS; i++) { - pinMode(COL_PINS[i], OUTPUT); - digitalWrite(COL_PINS[i], HIGH); - } - Serial.println("GPIO and display initialized."); + for (int i = 0; i < ROWS; i++) { + pinMode(ROW_PINS[i], INPUT_PULLUP); + } + for (int i = 0; i < COLS; i++) { + pinMode(COL_PINS[i], OUTPUT); + digitalWrite(COL_PINS[i], HIGH); + } + Serial.println("GPIO and display initialized."); } void display_code_for_level(int level) { - char code[3] = {0}; // Temp storage for level code - strncpy(code, levelCodes[level], 2); // Copy the level-specific code - - uint8_t segs[4] = {0}; // Segments to send to the display - - // Check if the first character is a letter and map it - if (isalpha(code[0])) { - if (code[0] >= 'A' && code[0] <= 'F') { - segs[0] = SEGMENT_MAP[code[0] - 'A' + 10]; // Maps A-F to their segment patterns - } else { - // Handle unexpected characters or extend SEGMENT_MAP for more letters - segs[0] = 0; // Display nothing for undefined letters - } - } else { - // Assume it's a number and map directly - segs[0] = SEGMENT_MAP[code[0] - '0']; - } - - // Check if the second character is a digit and map it - if (isdigit(code[1])) { - segs[1] = SEGMENT_MAP[code[1] - '0']; - } else { - // Handle unexpected characters - segs[1] = 0; // Display nothing for undefined digits - } - - // Set only the first two segments, leave others blank - display.setSegments(segs, 2, 0); // Display on leftmost two digits + char code[3] = {0}; // Temp storage for level code + strncpy(code, levelCodes[level], 2); // Copy the level-specific code + + uint8_t segs[4] = {0}; // Segments to send to the display + + // Check if the first character is a letter and map it + if (isalpha(code[0])) { + if (code[0] >= 'A' && code[0] <= 'F') { + segs[0] = SEGMENT_MAP[code[0] - 'A' + + 10]; // Maps A-F to their segment patterns + } else { + // Handle unexpected characters or extend SEGMENT_MAP for more letters + segs[0] = 0; // Display nothing for undefined letters + } + } else { + // Assume it's a number and map directly + segs[0] = SEGMENT_MAP[code[0] - '0']; + } + + // Check if the second character is a digit and map it + if (isdigit(code[1])) { + segs[1] = SEGMENT_MAP[code[1] - '0']; + } else { + // Handle unexpected characters + segs[1] = 0; // Display nothing for undefined digits + } + + // Set only the first two segments, leave others blank + display.setSegments(segs, 2, 0); // Display on leftmost two digits } +pb_global_state_t pb_hook_mod_state_read() { return puzzleState; } -pb_global_state_t pb_hook_mod_state_read() { - return puzzleState; -} - -void pb_hook_mod_state_write(pb_global_state_t state) { - puzzleState = state; -} +void pb_hook_mod_state_write(pb_global_state_t state) { puzzleState = state; } -void pb_hook_ev_main_state_update(pb_global_state_t state){ +void pb_hook_ev_main_state_update(pb_global_state_t state) { Serial.println("WE IN BOISS"); } void setup() { - Serial.begin(115200); - pinMode(SOLVED_PIN, OUTPUT); - digitalWrite(SOLVED_PIN, LOW); - display.setBrightness(0x0f); - initialize_system(); + Serial.begin(115200); + pinMode(SOLVED_PIN, OUTPUT); + digitalWrite(SOLVED_PIN, LOW); + display.setBrightness(0x0f); + initialize_system(); } void loop() { - switch(puzzleState) { - case PB_GS_PLAYING: - display_code_for_level(currentLevel); - check_button_press(); - delay(100); - break; - case PB_GS_SOLVED: - Serial.println("STATE = PB_GS_SOLVED"); - display_final_code(bombCode); - digitalWrite(SOLVED_PIN, HIGH); - break; - case PB_GS_NOINIT: - Serial.println("STATE = PB_GS_NOINIT"); - blink_display(0); - break; - case PB_GS_IDLE: - Serial.println("STATE = PB_GS_IDLE"); - blink_display(1); - break; - } + switch (puzzleState) { + case PB_GS_PLAYING: + display_code_for_level(currentLevel); + check_button_press(); + delay(100); + break; + case PB_GS_SOLVED: + Serial.println("STATE = PB_GS_SOLVED"); + display_final_code(bombCode); + digitalWrite(SOLVED_PIN, HIGH); + break; + case PB_GS_NOINIT: + Serial.println("STATE = PB_GS_NOINIT"); + blink_display(0); + break; + case PB_GS_IDLE: + Serial.println("STATE = PB_GS_IDLE"); + blink_display(1); + break; + } } diff --git a/puzzle/vault/mod.c b/puzzle/vault/mod.c index bae8a3d..250cb66 100644 --- a/puzzle/vault/mod.c +++ b/puzzle/vault/mod.c @@ -1,6 +1,5 @@ -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "vault"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_VAULT; - diff --git a/shared/FSM.h b/shared/FSM.h index 792a44f..db1de07 100644 --- a/shared/FSM.h +++ b/shared/FSM.h @@ -13,38 +13,38 @@ /// class.</typeparam> template <class TState> class FSM { public: - template <class... TPState> FSM(TPState &...args) { - int i = 0; - - ((void)_states.emplace(i++, args), ...); - } - - /// <summary> - /// Implement with FSM::act() - /// </summary> - void act(); - - /// <summary> - /// Used to check current state. - /// </summary> - /// <returns>Current state.</returns> - std::shared_ptr<TState> &get_state() { return _currentState; } - - /// <summary> - /// Used to get all states. - /// </summary> - /// <returns>Current states.</returns> - std::map<int, std::shared_ptr<TState>> get_states(); - - /// <summary> - /// Sets current state, calls appropiate functions. - /// </summary> - /// <param name="">State to transition into.</param> - void set_state(std::shared_ptr<TState>); + template <class... TPState> FSM(TPState &... args) { + int i = 0; + + ((void) _states.emplace(i++, args), ...); + } + + /// <summary> + /// Implement with FSM::act() + /// </summary> + void act(); + + /// <summary> + /// Used to check current state. + /// </summary> + /// <returns>Current state.</returns> + std::shared_ptr<TState> & get_state() { return _currentState; } + + /// <summary> + /// Used to get all states. + /// </summary> + /// <returns>Current states.</returns> + std::map<int, std::shared_ptr<TState>> get_states(); + + /// <summary> + /// Sets current state, calls appropiate functions. + /// </summary> + /// <param name="">State to transition into.</param> + void set_state(std::shared_ptr<TState>); private: - std::map<int, std::shared_ptr<TState>> _states; - std::shared_ptr<TState> _currentState; + std::map<int, std::shared_ptr<TState>> _states; + std::shared_ptr<TState> _currentState; }; #endif // _FSM_H_
\ No newline at end of file diff --git a/test/i2ctcp/main.cpp b/test/i2ctcp/main.cpp index 1f0c3ff..ca857cd 100644 --- a/test/i2ctcp/main.cpp +++ b/test/i2ctcp/main.cpp @@ -5,7 +5,9 @@ using std::min; -const uint8_t data[] = { 0xff, 0x00, 0xde, 0xad, 0xbe, 0xef, }; +const uint8_t data[] = { + 0xff, 0x00, 0xde, 0xad, 0xbe, 0xef, +}; const size_t data_len = sizeof(data); const size_t chunk_size = 6; @@ -33,14 +35,11 @@ TEST(i2ctcp, recv) { int parsed = i2ctcp_read(&recv_msg, send_data + i, expected_size); EXPECT_GE(parsed, 0); - if (i + expected_size == send_size) - EXPECT_EQ(parsed, 0); - else - EXPECT_GT(parsed, 0); + if (i + expected_size == send_size) EXPECT_EQ(parsed, 0); + else EXPECT_GT(parsed, 0); } ASSERT_NE(recv_msg.data, nullptr); ASSERT_EQ(recv_msg.length, data_len); ASSERT_EQ(0, memcmp(recv_msg.data, data, data_len)); } - diff --git a/test/pbdrv/mod.c b/test/pbdrv/mod.c index 5c4c95e..e4c2505 100644 --- a/test/pbdrv/mod.c +++ b/test/pbdrv/mod.c @@ -9,4 +9,3 @@ const i2c_addr_t PB_MOD_ADDR = 0x08; void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) { printf("[0x%02x]: buf[%lu]\n", addr & 0x7f, sz); } - diff --git a/test/pbdrv/msg.cpp b/test/pbdrv/msg.cpp index ca23bc7..3847b5f 100644 --- a/test/pbdrv/msg.cpp +++ b/test/pbdrv/msg.cpp @@ -1,8 +1,8 @@ #include <gtest/gtest.h> +#include "pb-mod.h" #include "pb-msg.h" #include "pb-send.h" -#include "pb-mod.h" TEST(pb_msg_rw, cmd_req_read) { pb_cmd_prop_t cmd = { @@ -53,4 +53,3 @@ TEST(pb_msg_rw, cmd_req_magic) { pb_msg_free(msg_read); } - diff --git a/test/pbdrv/msg.h b/test/pbdrv/msg.h index 52bee37..73c184e 100644 --- a/test/pbdrv/msg.h +++ b/test/pbdrv/msg.h @@ -26,7 +26,6 @@ static const pb_cmd_res_state_t expected_res_state = { .state = PB_GS_IDLE, }; -static const pb_cmd_req_set_state_t expected_req_set_state ={ +static const pb_cmd_req_set_state_t expected_req_set_state = { .state = PB_GS_PLAYING, }; - diff --git a/test/pbdrv/pb-route.c b/test/pbdrv/pb-route.c index 62a4b85..9e0fa30 100644 --- a/test/pbdrv/pb-route.c +++ b/test/pbdrv/pb-route.c @@ -1,2 +1 @@ -void pb_mod_blocking_delay_ms(unsigned long ms) { } - +void pb_mod_blocking_delay_ms(unsigned long ms) {} |