improve readability

This commit is contained in:
Dennis Gunia
2025-09-11 22:02:37 +02:00
parent 1ace5dd9de
commit 405efd2906
7 changed files with 177 additions and 119 deletions

View File

@@ -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"

View File

@@ -0,0 +1,7 @@
9x16M gegenüber 6x12m. Höhe ca 3,0 - 3,5m
Bestuhlt oder unbestuhlt.
Derhstrom
Traversen.

View File

@@ -16,15 +16,15 @@
// I/O Pin definition // I/O Pin definition
#define BUX_RX PD0 #define BUX_RX PD0 // RX Pin (to buffer)
#define BUX_TX PD1 #define BUX_TX PD1 // TX Pin (to buffer)
#define BUX_DIR PD2 #define BUX_DIR PD2 // Buffer direction pin
#define SENSOR_HOME PD3 #define SENSOR_HOME PD3 // Home sensor pin
#define MOTOR_A PC0 #define MOTOR_A PC0 // Motor phase A driver
#define MOTOR_B PC1 #define MOTOR_B PC1 // Motor phase B driver
#define MOTOR_C PC2 #define MOTOR_C PC2 // Motor phase C driver
#define MOTOR_D PC3 #define MOTOR_D PC3 // Motor phase D driver
// EEPROM Addresses // EEPROM Addresses
#define CONF_CONST_OKAY (uint8_t)0xAA #define CONF_CONST_OKAY (uint8_t)0xAA
@@ -35,21 +35,21 @@
// Protocol definitions // Protocol definitions
#define PROTO_MAXPKGLEN 64 // maximum size of package in bytes #define PROTO_MAXPKGLEN 64 // maximum size of package in bytes
// Commands // Command Bytes
#define CMDB_SETVAL (uint8_t)0x10 #define CMDB_SETVAL (uint8_t)0x10 // Set display value
#define CMDB_SETVALR (uint8_t)0x11 #define CMDB_SETVALR (uint8_t)0x11 // Set display value and do a full rotation
#define CMDB_EEPROMR (uint8_t)0xF0 #define CMDB_EEPROMR (uint8_t)0xF0 // Read EEPROM
#define CMDB_EEPROMW (uint8_t)0xF1 #define CMDB_EEPROMW (uint8_t)0xF1 // Write EEPROM
#define CMDB_GSTS (uint8_t)0xF8 #define CMDB_GSTS (uint8_t)0xF8 // Get status
#define CMDB_PING (uint8_t)0xFE #define CMDB_PING (uint8_t)0xFE // Ping
#define CMDB_RESET (uint8_t)0x30 #define CMDB_RESET (uint8_t)0x30 // Reset device
#define CMDB_PWRON (uint8_t)0x21 #define CMDB_PWRON (uint8_t)0x21 // Power motor on
#define CMDB_RPWROFF (uint8_t)0x20 #define CMDB_RPWROFF (uint8_t)0x20 // Poer motor off
// Reply // Command Responses
#define CMDR_ERR_INVALID 0xEE #define CMDR_ERR_INVALID 0xEE // Invalid command
#define CMDR_ACK 0xAA #define CMDR_ACK 0xAA // Acknowledge
#define CMDR_PING 0xFF #define CMDR_PING 0xFF // Ping response
// Utility definitions // Utility definitions
#define SHIFT_0B 0 #define SHIFT_0B 0

View File

@@ -17,7 +17,7 @@ uint8_t motor_steps[4] = {
0b00001000, 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 uint8_t target_flap = 0; // target flap
uint16_t absolute_pos = 0; // absolute position in steps uint16_t absolute_pos = 0; // absolute position in steps
uint16_t steps_since_home = 0; // steps since last home signal 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 // counter for auto powersaving
uint8_t ticksSinceMove = 0; 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; uint8_t afterRotation = STEPS_AFTERROT;
int16_t *delta_err; int16_t *delta_err;
@@ -58,7 +58,7 @@ void mctrl_init()
// setup adc // setup adc
ADMUX = 0x07; // Aref, ADC7 ADMUX = 0x07; // Aref, ADC7
ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADPS1); // Enable ADC, Start first ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADPS1); // Enable ADC, Start first
// reading No frerunning, 8MHz // reading No freerunning, 8MHz
while ((ADCSRA & (1 << ADSC)) > 0) while ((ADCSRA & (1 << ADSC)) > 0)
{ {
}; };
@@ -188,7 +188,7 @@ ISR(TIMER1_COMPA_vect)
} }
else else
{ // if target position is reached { // 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 { // if after rotation is set, apply it as new target
target_flap = afterRotation; target_flap = afterRotation;
afterRotation = STEPS_AFTERROT; afterRotation = STEPS_AFTERROT;

View File

@@ -9,13 +9,11 @@
#pragma once #pragma once
//#define F_CPU 16000000UL //#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 BAUDRATE ((F_CPU) / (UART_BAUD * 16UL) - 1) // set baud rate value for UBRR
#define SFBUS_SOF_BYTE '+' #define SFBUS_SOF_BYTE '+' // Byte marks start of frame
#define SFBUS_EOF_BYTE '$' #define SFBUS_EOF_BYTE '$' // Byte marks end of frame
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@@ -6,10 +6,22 @@
* This file provides an abstraction layer to access many devices * This file provides an abstraction layer to access many devices
* simultaneously. by Dennis Gunia - 2025 - wwwm.dennisgunia.de * simultaneously. by Dennis Gunia - 2025 - wwwm.dennisgunia.de
*/ */
enum SFDEVICE_STATE { NEW, OFFLINE, ONLINE, FAILED }; enum SFDEVICE_STATE
enum SFDEVICE_POWER { DISABLED, ENABLED, UNKNOWN }; {
NEW,
OFFLINE,
ONLINE,
FAILED
};
enum SFDEVICE_POWER
{
DISABLED,
ENABLED,
UNKNOWN
};
struct SFDEVICE { struct SFDEVICE
{
int pos_x; int pos_x;
int pos_y; int pos_y;
u_int16_t address; u_int16_t address;
@@ -22,10 +34,12 @@ struct SFDEVICE {
enum SFDEVICE_POWER powerState; enum SFDEVICE_POWER powerState;
}; };
#define SFDEVICE_MAXDEV 128 enum
{
#define SFDEVICE_MAX_X 20 SFDEVICE_MAXDEV = 128,
#define SFDEVICE_MAX_Y 4 SFDEVICE_MAX_X = 20,
SFDEVICE_MAX_Y = 4
};
// next free slot to register device // next free slot to register device
int nextFreeSlot = -1; int nextFreeSlot = -1;
@@ -33,53 +47,63 @@ int deviceMap[SFDEVICE_MAX_X][SFDEVICE_MAX_Y];
struct SFDEVICE devices[SFDEVICE_MAXDEV]; 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() { void devicemgr_init()
// reserve memory buffer {
for (int y = 0; y<SFDEVICE_MAX_Y; y++){ // reserve memory buffer
for (int x = 0; x<SFDEVICE_MAX_X; x++){ for (int y = 0; y < SFDEVICE_MAX_Y; y++)
deviceMap[x][y] = -1; //all empty slots are -1 {
for (int x = 0; x < SFDEVICE_MAX_X; x++)
{
deviceMap[x][y] = -1; //all empty slots are -1
} }
} }
} }
int devicemgr_readStatus(int device_id) { int devicemgr_readStatus(int device_id)
double _voltage = 0; {
u_int32_t _counter = 0; double _voltage = 0;
u_int8_t _status = u_int32_t _counter = 0;
sfbus_read_status(devices[device_id].rs485_descriptor, u_int8_t _status =
devices[device_id].address, &_voltage, &_counter); sfbus_read_status(devices[device_id].rs485_descriptor, devices[device_id].address, &_voltage, &_counter);
if (_status == 0xFF) { if (_status == 0xFF)
devices[device_id].powerState = UNKNOWN; {
devices[device_id].deviceState = OFFLINE; devices[device_id].powerState = UNKNOWN;
return -1; devices[device_id].deviceState = OFFLINE;
} else { return -1;
}
devices[device_id].reg_voltage = _voltage; devices[device_id].reg_voltage = _voltage;
devices[device_id].reg_counter = _counter; devices[device_id].reg_counter = _counter;
devices[device_id].reg_status = _status; devices[device_id].reg_status = _status;
devices[device_id].powerState = ~((devices[device_id].reg_status >> 4)) & 0x01; devices[device_id].powerState = ~((devices[device_id].reg_status >> 4)) & 0x01;
devices[device_id].deviceState = ONLINE; 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; devices[device_id].deviceState = FAILED;
} }
return 0; return 0;
}
} }
json_object * devicemgr_printMap(){ json_object *devicemgr_printMap()
{
json_object *rows_array = json_object_new_array(); json_object *rows_array = json_object_new_array();
for (int y = 0; y<SFDEVICE_MAX_Y; y++){ for (int y = 0; y < SFDEVICE_MAX_Y; y++)
{
json_object *columns_array = json_object_new_array(); json_object *columns_array = json_object_new_array();
for (int x = 0; x<SFDEVICE_MAX_X; x++){ for (int x = 0; x < SFDEVICE_MAX_X; x++)
{
json_object_array_add(columns_array, json_object_new_int(deviceMap[x][y])); json_object_array_add(columns_array, json_object_new_int(deviceMap[x][y]));
} }
json_object_array_add(rows_array,columns_array); json_object_array_add(rows_array, columns_array);
} }
return rows_array; return rows_array;
} }
json_object * devicemgr_printDetails(int device_id){ json_object *devicemgr_printDetails(int device_id)
{
// generate json object with status // generate json object with status
json_object *root = json_object_new_object(); json_object *root = json_object_new_object();
json_object_object_add(root, "id", json_object_new_int(device_id)); json_object_object_add(root, "id", json_object_new_int(device_id));
@@ -96,42 +120,62 @@ json_object * devicemgr_printDetails(int device_id){
json_object_object_add(status, "rotations", json_object_new_int(devices[device_id].reg_counter)); json_object_object_add(status, "rotations", json_object_new_int(devices[device_id].reg_counter));
json_object_object_add(status, "power", json_object_new_boolean(devices[device_id].powerState)); json_object_object_add(status, "power", json_object_new_boolean(devices[device_id].powerState));
json_object_object_add(status, "raw", json_object_new_uint64(devices[device_id].reg_status)); json_object_object_add(status, "raw", json_object_new_uint64(devices[device_id].reg_status));
switch(devices[device_id].deviceState){ switch (devices[device_id].deviceState)
case ONLINE: {
json_object_object_add(status, "device", json_object_new_string("ONLINE")); case ONLINE:
break; json_object_object_add(status, "device", json_object_new_string("ONLINE"));
case OFFLINE: break;
json_object_object_add(status, "device", json_object_new_string("OFFLINE")); case OFFLINE:
break; json_object_object_add(status, "device", json_object_new_string("OFFLINE"));
case FAILED: break;
json_object_object_add(status, "device", json_object_new_string("FAILED")); case FAILED:
break; json_object_object_add(status, "device", json_object_new_string("FAILED"));
case NEW: break;
json_object_object_add(status, "device", json_object_new_string("NEW")); case NEW:
break; json_object_object_add(status, "device", json_object_new_string("NEW"));
break;
} }
json_object *status_flags = json_object_new_object(); json_object *status_flags = json_object_new_object();
json_object_object_add(status_flags, "errorTooBig", json_object_new_boolean(((devices[device_id].reg_status) >> 0) & 0x01)); json_object_object_add(status_flags,
json_object_object_add(status_flags, "noHome", json_object_new_boolean(((devices[device_id].reg_status) >> 1) & 0x01)); "errorTooBig",
json_object_object_add(status_flags, "fuseBlown", json_object_new_boolean(((devices[device_id].reg_status) >> 2) & 0x01)); json_object_new_boolean(((devices[device_id].reg_status) >> 0) & 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,
json_object_object_add(status_flags, "powerDown", json_object_new_boolean(((devices[device_id].reg_status) >> 4) & 0x01)); "noHome",
json_object_object_add(status_flags, "failSafe", json_object_new_boolean(((devices[device_id].reg_status) >> 5) & 0x01)); json_object_new_boolean(((devices[device_id].reg_status) >> 1) & 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,
"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(status, "flags", status_flags);
json_object_object_add(root, "status", status); json_object_object_add(root, "status", status);
return root; return root;
} }
json_object * devicemgr_printDetailsAll(){ json_object *devicemgr_printDetailsAll()
{
json_object *root = json_object_new_object(); json_object *root = json_object_new_object();
json_object_object_add(root, "devices_all", json_object_new_int(nextFreeSlot + 1)); json_object_object_add(root, "devices_all", json_object_new_int(nextFreeSlot + 1));
json_object *devices_arr = json_object_new_array(); json_object *devices_arr = json_object_new_array();
int devices_online = 0; int devices_online = 0;
for (int i = 0; i< (nextFreeSlot + 1); i++){ for (int i = 0; i < (nextFreeSlot + 1); i++)
{
devicemgr_readStatus(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_array_add(devices_arr, devicemgr_printDetails(i));
} }
json_object_object_add(root, "map", devicemgr_printMap()); json_object_object_add(root, "map", devicemgr_printMap());
@@ -142,40 +186,45 @@ json_object * devicemgr_printDetailsAll(){
return root; return root;
} }
void setSingle(int id, u_int8_t flap){ void setSingle(int id, u_int8_t flap)
sfbus_display_full(devices[id].rs485_descriptor , devices[id].address,flap); {
sfbus_display_full(devices[id].rs485_descriptor, devices[id].address, flap);
devices[nextFreeSlot].current_flap = flap; devices[nextFreeSlot].current_flap = flap;
} }
void printText(char* text,int x,int y){ void printText(char *text, int x, int y)
for (int i = 0; i<strlen(text);i++){ {
int this_id = deviceMap[x+i][y]; for (int i = 0; i < strlen(text); i++)
if (this_id >= 0){ {
setSingle(devices[this_id].address,*(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) { int devicemgr_register(int rs485_descriptor, u_int16_t address, int x, int y)
nextFreeSlot++; {
devices[nextFreeSlot].pos_x = x; nextFreeSlot++;
devices[nextFreeSlot].pos_y = y; devices[nextFreeSlot].pos_x = x;
devices[nextFreeSlot].address = address; devices[nextFreeSlot].pos_y = y;
devices[nextFreeSlot].rs485_descriptor = rs485_descriptor; devices[nextFreeSlot].address = address;
devices[nextFreeSlot].reg_voltage = 0; devices[nextFreeSlot].rs485_descriptor = rs485_descriptor;
devices[nextFreeSlot].reg_counter = 0; devices[nextFreeSlot].reg_voltage = 0;
devices[nextFreeSlot].reg_status = 0; devices[nextFreeSlot].reg_counter = 0;
devices[nextFreeSlot].current_flap = 0; devices[nextFreeSlot].reg_status = 0;
devices[nextFreeSlot].deviceState = NEW; devices[nextFreeSlot].current_flap = 0;
devices[nextFreeSlot].powerState = DISABLED; devices[nextFreeSlot].deviceState = NEW;
// try to reach device devices[nextFreeSlot].powerState = DISABLED;
devicemgr_readStatus(nextFreeSlot); // try to reach device
if (deviceMap[x][y] >= 0){ // rest old ones devicemgr_readStatus(nextFreeSlot);
int old_id = deviceMap[x][y]; if (deviceMap[x][y] >= 0)
devices[old_id].pos_x = -1; { // rest old ones
devices[old_id].pos_y = -1; int old_id = deviceMap[x][y];
} devices[old_id].pos_x = -1;
deviceMap[x][y] = nextFreeSlot; devices[old_id].pos_y = -1;
return nextFreeSlot; }
deviceMap[x][y] = nextFreeSlot;
return nextFreeSlot;
} }

View File

@@ -11,7 +11,6 @@
#include "rs485.h" #include "rs485.h"
#include "sfbus.h" #include "sfbus.h"
#include <unistd.h>
#include "devicemgr.h" #include "devicemgr.h"
void printUsage(char *argv[]) { void printUsage(char *argv[]) {
@@ -20,7 +19,7 @@ void printUsage(char *argv[]) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int opt; int opt = ' ';
u_int16_t addr_int = 0; u_int16_t addr_int = 0;
char *port = malloc(256); char *port = malloc(256);
char *command = malloc(16); char *command = malloc(16);