From 405efd29062450f9e367d2fdba879d9b02cea95d Mon Sep 17 00:00:00 2001 From: Dennis Gunia Date: Thu, 11 Sep 2025 22:02:37 +0200 Subject: [PATCH] improve readability --- software/firmware_module/.clangd | 5 + .../module_rev0/src/TEchnik OF26.txt | 7 + .../firmware_module/module_rev0/src/global.h | 46 ++-- .../firmware_module/module_rev0/src/mctrl.c | 8 +- .../firmware_module/module_rev0/src/rs485.h | 8 +- software/pc_client/src/devicemgr.c | 219 +++++++++++------- software/pc_client/src/main.c | 3 +- 7 files changed, 177 insertions(+), 119 deletions(-) create mode 100644 software/firmware_module/.clangd create mode 100644 software/firmware_module/module_rev0/src/TEchnik OF26.txt diff --git a/software/firmware_module/.clangd b/software/firmware_module/.clangd new file mode 100644 index 0000000..0216f2c --- /dev/null +++ b/software/firmware_module/.clangd @@ -0,0 +1,5 @@ +--- +CompileFlags: + Add: + - "-I/home/dennisgunia/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/" + - "-I/home/dennisgunia/.config/VSCodium/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/19.1.2/clangd_19.1.2/lib/clang/19/include/stddef.h" diff --git a/software/firmware_module/module_rev0/src/TEchnik OF26.txt b/software/firmware_module/module_rev0/src/TEchnik OF26.txt new file mode 100644 index 0000000..ed01087 --- /dev/null +++ b/software/firmware_module/module_rev0/src/TEchnik OF26.txt @@ -0,0 +1,7 @@ +9x16M gegenüber 6x12m. Höhe ca 3,0 - 3,5m + +Bestuhlt oder unbestuhlt. +Derhstrom +Traversen. + + diff --git a/software/firmware_module/module_rev0/src/global.h b/software/firmware_module/module_rev0/src/global.h index fd94703..d7711be 100644 --- a/software/firmware_module/module_rev0/src/global.h +++ b/software/firmware_module/module_rev0/src/global.h @@ -16,18 +16,18 @@ // I/O Pin definition -#define BUX_RX PD0 -#define BUX_TX PD1 -#define BUX_DIR PD2 -#define SENSOR_HOME PD3 +#define BUX_RX PD0 // RX Pin (to buffer) +#define BUX_TX PD1 // TX Pin (to buffer) +#define BUX_DIR PD2 // Buffer direction pin +#define SENSOR_HOME PD3 // Home sensor pin -#define MOTOR_A PC0 -#define MOTOR_B PC1 -#define MOTOR_C PC2 -#define MOTOR_D PC3 +#define MOTOR_A PC0 // Motor phase A driver +#define MOTOR_B PC1 // Motor phase B driver +#define MOTOR_C PC2 // Motor phase C driver +#define MOTOR_D PC3 // Motor phase D driver // EEPROM Addresses -#define CONF_CONST_OKAY (uint8_t)0xAA +#define CONF_CONST_OKAY (uint8_t)0xAA #define CONF_ADDR_OKAY 0x0004 #define CONF_ADDR_ADDR 0x0000 #define CONF_ADDR_OFFSET 0x0002 @@ -35,21 +35,21 @@ // Protocol definitions #define PROTO_MAXPKGLEN 64 // maximum size of package in bytes -// Commands -#define CMDB_SETVAL (uint8_t)0x10 -#define CMDB_SETVALR (uint8_t)0x11 -#define CMDB_EEPROMR (uint8_t)0xF0 -#define CMDB_EEPROMW (uint8_t)0xF1 -#define CMDB_GSTS (uint8_t)0xF8 -#define CMDB_PING (uint8_t)0xFE -#define CMDB_RESET (uint8_t)0x30 -#define CMDB_PWRON (uint8_t)0x21 -#define CMDB_RPWROFF (uint8_t)0x20 +// Command Bytes +#define CMDB_SETVAL (uint8_t)0x10 // Set display value +#define CMDB_SETVALR (uint8_t)0x11 // Set display value and do a full rotation +#define CMDB_EEPROMR (uint8_t)0xF0 // Read EEPROM +#define CMDB_EEPROMW (uint8_t)0xF1 // Write EEPROM +#define CMDB_GSTS (uint8_t)0xF8 // Get status +#define CMDB_PING (uint8_t)0xFE // Ping +#define CMDB_RESET (uint8_t)0x30 // Reset device +#define CMDB_PWRON (uint8_t)0x21 // Power motor on +#define CMDB_RPWROFF (uint8_t)0x20 // Poer motor off -// Reply -#define CMDR_ERR_INVALID 0xEE -#define CMDR_ACK 0xAA -#define CMDR_PING 0xFF +// Command Responses +#define CMDR_ERR_INVALID 0xEE // Invalid command +#define CMDR_ACK 0xAA // Acknowledge +#define CMDR_PING 0xFF // Ping response // Utility definitions #define SHIFT_0B 0 diff --git a/software/firmware_module/module_rev0/src/mctrl.c b/software/firmware_module/module_rev0/src/mctrl.c index 74b0dc7..e4749c3 100644 --- a/software/firmware_module/module_rev0/src/mctrl.c +++ b/software/firmware_module/module_rev0/src/mctrl.c @@ -17,7 +17,7 @@ uint8_t motor_steps[4] = { 0b00001000, }; -uint8_t step_index = 0; // cuurent index in motor_steps +uint8_t step_index = 0; // current index in motor_steps uint8_t target_flap = 0; // target flap uint16_t absolute_pos = 0; // absolute position in steps uint16_t steps_since_home = 0; // steps since last home signal @@ -29,7 +29,7 @@ uint8_t lastSens = 0; // home sonsor signal from last tick // counter for auto powersaving uint8_t ticksSinceMove = 0; -// value to goto after the current target_flap is reached. 255 = NONE. +// value to goto after the current is reached. 255 = NONE. uint8_t afterRotation = STEPS_AFTERROT; int16_t *delta_err; @@ -58,7 +58,7 @@ void mctrl_init() // setup adc ADMUX = 0x07; // Aref, ADC7 ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADPS1); // Enable ADC, Start first - // reading No frerunning, 8MHz + // reading No freerunning, 8MHz while ((ADCSRA & (1 << ADSC)) > 0) { }; @@ -188,7 +188,7 @@ ISR(TIMER1_COMPA_vect) } else { // if target position is reached - if (afterRotation < (STEPS_PER_FLAP + 5)) + if (afterRotation < (AMOUNTFLAPS + 5)) { // if after rotation is set, apply it as new target target_flap = afterRotation; afterRotation = STEPS_AFTERROT; diff --git a/software/firmware_module/module_rev0/src/rs485.h b/software/firmware_module/module_rev0/src/rs485.h index f787c9f..3c10cbd 100644 --- a/software/firmware_module/module_rev0/src/rs485.h +++ b/software/firmware_module/module_rev0/src/rs485.h @@ -9,13 +9,11 @@ #pragma once //#define F_CPU 16000000UL -#define UART_BAUD 19200 +#define UART_BAUD 19200 // RS485 baud rate #define BAUDRATE ((F_CPU) / (UART_BAUD * 16UL) - 1) // set baud rate value for UBRR -#define SFBUS_SOF_BYTE '+' -#define SFBUS_EOF_BYTE '$' - - +#define SFBUS_SOF_BYTE '+' // Byte marks start of frame +#define SFBUS_EOF_BYTE '$' // Byte marks end of frame #ifdef __cplusplus extern "C" { diff --git a/software/pc_client/src/devicemgr.c b/software/pc_client/src/devicemgr.c index cfc6562..371c724 100644 --- a/software/pc_client/src/devicemgr.c +++ b/software/pc_client/src/devicemgr.c @@ -6,10 +6,22 @@ * This file provides an abstraction layer to access many devices * simultaneously. by Dennis Gunia - 2025 - wwwm.dennisgunia.de */ -enum SFDEVICE_STATE { NEW, OFFLINE, ONLINE, FAILED }; -enum SFDEVICE_POWER { DISABLED, ENABLED, UNKNOWN }; +enum SFDEVICE_STATE +{ + NEW, + OFFLINE, + ONLINE, + FAILED +}; +enum SFDEVICE_POWER +{ + DISABLED, + ENABLED, + UNKNOWN +}; -struct SFDEVICE { +struct SFDEVICE +{ int pos_x; int pos_y; u_int16_t address; @@ -22,10 +34,12 @@ struct SFDEVICE { enum SFDEVICE_POWER powerState; }; -#define SFDEVICE_MAXDEV 128 - -#define SFDEVICE_MAX_X 20 -#define SFDEVICE_MAX_Y 4 +enum +{ + SFDEVICE_MAXDEV = 128, + SFDEVICE_MAX_X = 20, + SFDEVICE_MAX_Y = 4 +}; // next free slot to register device int nextFreeSlot = -1; @@ -33,53 +47,63 @@ int deviceMap[SFDEVICE_MAX_X][SFDEVICE_MAX_Y]; struct SFDEVICE devices[SFDEVICE_MAXDEV]; -const char* symbols[] = {" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ä", "Ö", "Ü", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ".", "-", "?", "!"}; +const char *symbols[] = {" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", + "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ä", "Ö", "Ü", + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ".", "-", "?", "!"}; -void devicemgr_init() { - // reserve memory buffer - for (int y = 0; y> 4)) & 0x01; devices[device_id].deviceState = ONLINE; - if ((((devices[device_id].reg_status) >> 5) & 0x01) > 0){ + if ((((devices[device_id].reg_status) >> 5) & 0x01) > 0) + { devices[device_id].deviceState = FAILED; } return 0; - } } -json_object * devicemgr_printMap(){ +json_object *devicemgr_printMap() +{ json_object *rows_array = json_object_new_array(); - for (int y = 0; y> 0) & 0x01)); - json_object_object_add(status_flags, "noHome", json_object_new_boolean(((devices[device_id].reg_status) >> 1) & 0x01)); - json_object_object_add(status_flags, "fuseBlown", json_object_new_boolean(((devices[device_id].reg_status) >> 2) & 0x01)); - json_object_object_add(status_flags, "homeSense", json_object_new_boolean(((devices[device_id].reg_status) >> 3) & 0x01)); - json_object_object_add(status_flags, "powerDown", json_object_new_boolean(((devices[device_id].reg_status) >> 4) & 0x01)); - json_object_object_add(status_flags, "failSafe", json_object_new_boolean(((devices[device_id].reg_status) >> 5) & 0x01)); - json_object_object_add(status_flags, "busy", json_object_new_boolean(((devices[device_id].reg_status) >> 6) & 0x01)); + json_object_object_add(status_flags, + "errorTooBig", + json_object_new_boolean(((devices[device_id].reg_status) >> 0) & 0x01)); + json_object_object_add(status_flags, + "noHome", + json_object_new_boolean(((devices[device_id].reg_status) >> 1) & 0x01)); + json_object_object_add(status_flags, + "fuseBlown", + json_object_new_boolean(((devices[device_id].reg_status) >> 2) & 0x01)); + json_object_object_add(status_flags, + "homeSense", + json_object_new_boolean(((devices[device_id].reg_status) >> 3) & 0x01)); + json_object_object_add(status_flags, + "powerDown", + json_object_new_boolean(((devices[device_id].reg_status) >> 4) & 0x01)); + json_object_object_add(status_flags, + "failSafe", + json_object_new_boolean(((devices[device_id].reg_status) >> 5) & 0x01)); + json_object_object_add(status_flags, + "busy", + json_object_new_boolean(((devices[device_id].reg_status) >> 6) & 0x01)); json_object_object_add(status, "flags", status_flags); json_object_object_add(root, "status", status); - + return root; } -json_object * devicemgr_printDetailsAll(){ +json_object *devicemgr_printDetailsAll() +{ json_object *root = json_object_new_object(); json_object_object_add(root, "devices_all", json_object_new_int(nextFreeSlot + 1)); json_object *devices_arr = json_object_new_array(); int devices_online = 0; - for (int i = 0; i< (nextFreeSlot + 1); i++){ + for (int i = 0; i < (nextFreeSlot + 1); i++) + { devicemgr_readStatus(i); - if ( devices[i].deviceState == ONLINE ){devices_online++;} + if (devices[i].deviceState == ONLINE) + { + devices_online++; + } json_object_array_add(devices_arr, devicemgr_printDetails(i)); } json_object_object_add(root, "map", devicemgr_printMap()); json_object_object_add(root, "devices", devices_arr); json_object_object_add(root, "devices_online", json_object_new_int(devices_online)); - + printf("The json representation:\n\n%s\n\n", json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY)); return root; } -void setSingle(int id, u_int8_t flap){ - sfbus_display_full(devices[id].rs485_descriptor , devices[id].address,flap); +void setSingle(int id, u_int8_t flap) +{ + sfbus_display_full(devices[id].rs485_descriptor, devices[id].address, flap); devices[nextFreeSlot].current_flap = flap; } -void printText(char* text,int x,int y){ - for (int i = 0; i= 0){ - setSingle(devices[this_id].address,*(text+i)); +void printText(char *text, int x, int y) +{ + for (int i = 0; i < strlen(text); i++) + { + int this_id = deviceMap[x + i][y]; + if (this_id >= 0) + { + setSingle(devices[this_id].address, *(text + i)); } } } -int devicemgr_register(int rs485_descriptor, u_int16_t address, int x,int y) { - nextFreeSlot++; - devices[nextFreeSlot].pos_x = x; - devices[nextFreeSlot].pos_y = y; - devices[nextFreeSlot].address = address; - devices[nextFreeSlot].rs485_descriptor = rs485_descriptor; - devices[nextFreeSlot].reg_voltage = 0; - devices[nextFreeSlot].reg_counter = 0; - devices[nextFreeSlot].reg_status = 0; - devices[nextFreeSlot].current_flap = 0; - devices[nextFreeSlot].deviceState = NEW; - devices[nextFreeSlot].powerState = DISABLED; - // try to reach device - devicemgr_readStatus(nextFreeSlot); - if (deviceMap[x][y] >= 0){ // rest old ones - int old_id = deviceMap[x][y]; - devices[old_id].pos_x = -1; - devices[old_id].pos_y = -1; - } - deviceMap[x][y] = nextFreeSlot; - return nextFreeSlot; +int devicemgr_register(int rs485_descriptor, u_int16_t address, int x, int y) +{ + nextFreeSlot++; + devices[nextFreeSlot].pos_x = x; + devices[nextFreeSlot].pos_y = y; + devices[nextFreeSlot].address = address; + devices[nextFreeSlot].rs485_descriptor = rs485_descriptor; + devices[nextFreeSlot].reg_voltage = 0; + devices[nextFreeSlot].reg_counter = 0; + devices[nextFreeSlot].reg_status = 0; + devices[nextFreeSlot].current_flap = 0; + devices[nextFreeSlot].deviceState = NEW; + devices[nextFreeSlot].powerState = DISABLED; + // try to reach device + devicemgr_readStatus(nextFreeSlot); + if (deviceMap[x][y] >= 0) + { // rest old ones + int old_id = deviceMap[x][y]; + devices[old_id].pos_x = -1; + devices[old_id].pos_y = -1; + } + deviceMap[x][y] = nextFreeSlot; + return nextFreeSlot; } - diff --git a/software/pc_client/src/main.c b/software/pc_client/src/main.c index c9a66bb..8530607 100644 --- a/software/pc_client/src/main.c +++ b/software/pc_client/src/main.c @@ -11,7 +11,6 @@ #include "rs485.h" #include "sfbus.h" -#include #include "devicemgr.h" void printUsage(char *argv[]) { @@ -20,7 +19,7 @@ void printUsage(char *argv[]) { } int main(int argc, char *argv[]) { - int opt; + int opt = ' '; u_int16_t addr_int = 0; char *port = malloc(256); char *command = malloc(16);