./git_commit_push.sh

This commit is contained in:
Bernhard
2025-12-17 19:19:44 +01:00
parent c393706088
commit dcb267df89
7 changed files with 278 additions and 152 deletions

73
io/io.c
View File

@@ -1,4 +1,5 @@
#include "main.h"
#include "io.h"
#include <can/can_client.h>
@@ -6,29 +7,35 @@ struct GPIO_KEY_DATA gpioKeyStop;
struct GPIO_KEY_DATA gpioKeyPwrUp;
struct GPIO_KEY_DATA gpioKeyPwrDown;
char nInitialized = 0;
int IO_Init()
{
if (wiringPiSetupPinType(WPI_PIN_BCM))
{
printf("IO: Set up wiringPi failed!\n");
mylog(LOG_ERR, "IO: Set up wiringPi failed!");
return 1;
}
nInitialized = 1;
// IO-Pins für Tasten konfigurieren
// config IO-pins for the keys
SetupKeyPin(&gpioKeyStop, GPIO_KEY_STOP);
SetupKeyPin(&gpioKeyPwrUp, GPIO_KEY_PWRUP);
SetupKeyPin(&gpioKeyPwrDown, GPIO_KEY_PWRDOWN);
// IO-Pins für Ausgänge konfigurieren
// config IO-pins for outputs
SetupOutputPin(GPIO_LED_MOTRUN);
SetupOutputPin(GPIO_OUT_PWRON);
printf("IO initialized successfull!\n");
mylog(LOG_INFO, "IO: Initialized successfull!");
return 0;
}
/// @brief Setup a pin for a key input
/// @param pdata
/// @param iKeyPin
void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin)
{
pdata->iKeyPin = iKeyPin;
@@ -40,24 +47,26 @@ void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin)
pdata->iKeyPressedCycleCounter = 0;
pdata->iKeyRepeatCycleCounter = 0;
// Wenn Eingang verwendet wird
if (pdata->iKeyPin > 0)
if ((pdata->iKeyPin > 0) && nInitialized)
{
//mylog(LOG_INFO, "IO: Config Pin %d as input", iOutPin);
pinMode(pdata->iKeyPin, INPUT);
pullUpDnControl(pdata->iKeyPin, PUD_UP);
}
}
/// @brief Read a key input
/// @param pdata
void ReadKey(struct GPIO_KEY_DATA *pdata)
{
if (pdata->iKeyPin > 0)
{
int newval = pdata->iKeyValue;
if (digitalRead(pdata->iKeyPin) == LOW) // invertierte Logik weil wir PullUp-Widerstand bei Betätigung auf low ziehen
if (digitalRead(pdata->iKeyPin) == LOW) // we use pull-up resistors so we have inverted logic
{
// Signal liegt an
// key is pressed
if (pdata->nLowCycleCounter > 0)
{
pdata->nLowCycleCounter--;
@@ -68,14 +77,14 @@ void ReadKey(struct GPIO_KEY_DATA *pdata)
pdata->nHighCycleCounter++;
if (pdata->nHighCycleCounter >= KEY_RISING_FILTERCYCLES)
{
// gewünschte Anzahl Zyklen stabil
// key is stable pressed
newval = 1;
}
}
}
else
{
// Signal liegt nicht an
// key is not pressed
if (pdata->nHighCycleCounter > 0)
{
pdata->nHighCycleCounter--;
@@ -86,7 +95,7 @@ void ReadKey(struct GPIO_KEY_DATA *pdata)
pdata->nLowCycleCounter++;
if (pdata->nLowCycleCounter >= KEY_FALLING_FILTERCYCLES)
{
// gewünschte Anzahl Zyklen stabil
// key is stable not pressed
newval = 0;
}
}
@@ -94,7 +103,7 @@ void ReadKey(struct GPIO_KEY_DATA *pdata)
if (newval && !pdata->iKeyValue)
{
// Taster wurde betätigt
// key was pressed -> rising edge
pdata->iKeyRisingEdge = 1;
pdata->iKeyValue = newval;
pdata->iKeyPressedCycleCounter = 0;
@@ -102,32 +111,32 @@ void ReadKey(struct GPIO_KEY_DATA *pdata)
}
else if (pdata->iKeyValue && !newval)
{
// Taster wurde losgelassen
// key was released -> falling edge
pdata->iKeyFallingEdge = 1;
pdata->iKeyValue = newval;
}
else
{
// Keine Änderung
// no change
pdata->iKeyRisingEdge = 0;
pdata->iKeyFallingEdge = 0;
if (pdata->iKeyValue)
{
// Wenn Taste gedrückt ist
// when key is pressed
if (pdata->iKeyPressedCycleCounter < KEY_START_REPEAT_CYCLECOUNT)
{
// Zyklen zählen
// count cycles
pdata->iKeyPressedCycleCounter++;
}
if (pdata->iKeyPressedCycleCounter >= KEY_START_REPEAT_CYCLECOUNT)
{
// Wenn Taste länger als KEY_START_REPEAT_CYCLECOUNT gedrückt ist
// when key is pressed for more then KEY_START_REPEAT_CYCLECOUNT cycles
pdata->iKeyRepeatCycleCounter++;
if (pdata->iKeyRepeatCycleCounter >= KEY_REPEAT_CYCLECOUNT)
{
// alle KEY_REPEAT_CYCLECOUNT Zyklen Tastendruck signalisieren
// signal key press every KEY_REPEAT_CYCLECOUNT cycles
pdata->iKeyRisingEdge = 1;
pdata->iKeyRepeatCycleCounter = 0;
}
@@ -137,24 +146,31 @@ void ReadKey(struct GPIO_KEY_DATA *pdata)
}
}
/// @brief Config pin for output
/// @param iOutPin
void SetupOutputPin(int iOutPin)
{
if (iOutPin > 0)
if ((iOutPin > 0) && nInitialized)
{
//mylog(LOG_INFO, "IO: Config Pin %d as output", iOutPin);
pinMode(iOutPin, OUTPUT);
digitalWrite(iOutPin, LOW);
}
}
/// @brief Write an output pin to HIGH or LOW
/// @param iOutPin
/// @param iValue
void WriteOutputPin(int iOutPin, int iValue)
{
if (iOutPin > 0)
if ((iOutPin > 0) && nInitialized)
{
digitalWrite(iOutPin, iValue);
}
}
/// @brief look cyclic for the keys
void IO_DoCyclic()
{
ReadKey(&gpioKeyStop);
@@ -163,37 +179,42 @@ void IO_DoCyclic()
if (gpioKeyStop.iKeyValue)
{
// Stop-Taste betätigt -> hat Vorrang vor den anderen Tasten
// stop key is pressed
if (gpioKeyStop.iKeyRisingEdge)
{
mylog(LOG_INFO, "IO: KEY-Stop: Stop motor.");
Can_SetMotorGear(0, 0);
}
}
else
else if (motctrl[0].nDriveReady) // plus and minus keys only when drive is ready
{
if (gpioKeyPwrUp.iKeyRisingEdge)
{
// Leistung erhöhen
// plus key is pressed -> increase power
if (motctrl[0].iMotorGear == MOTOR_GEAR_NEUTRAL)
{
mylog(LOG_INFO, "KEY-Plus: Start motor.");
Can_SetMotorGear(0, 1);
Can_SetMotorPower(0, MOTOR_PWR_MIN_PCT);
}
else
{
mylog(LOG_INFO, "KEY-Plus: Increase power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerPct + MOTOR_PWR_STEP);
}
}
if (gpioKeyPwrDown.iKeyRisingEdge)
{
// Leistung verringern
// minus key is pressed -> decrease power
if (motctrl[0].iMotorPowerPct > MOTOR_PWR_MIN_PCT)
{
mylog(LOG_INFO, "KEY-Minus: Decrease power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerPct - MOTOR_PWR_STEP);
}
else
{
mylog(LOG_INFO, "KEY-Minus: Stop motor.");
Can_SetMotorGear(0, 0);
}
}