From d6bd1932d3f8e57f3cbf04cfcd4ee54d6d7574af Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sat, 17 May 2025 13:01:33 +0200 Subject: [PATCH] Make pumps initialization and actuation more dynamic by checking which one are declared in config.h --- firmware/atmega/include/pump.h | 32 +++++++++++++++++++++++++++++++- firmware/atmega/source/pump.c | 28 +++++++++++++++------------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/firmware/atmega/include/pump.h b/firmware/atmega/include/pump.h index 65aef45..4559b9f 100644 --- a/firmware/atmega/include/pump.h +++ b/firmware/atmega/include/pump.h @@ -1,9 +1,39 @@ +#ifndef PUMP_H +#define PUMP_H + #include #include #include #include #include "config.h" + +static const int8_t actuatorPins[] = { + #ifdef PUMP_D3 + PUMP_D3, + #endif + + #ifdef PUMP_D4 + PUMP_D4, + #endif + + #ifdef PUMP_D5 + PUMP_D5, + #endif + + #ifdef PUMP_D6 + PUMP_D6, + #endif + + #ifdef PUMP_D7 + PUMP_D7, + #endif + }; + +#define ACTUATORS_NUM (sizeof(actuatorPins) / sizeof(actuatorPins[0])) + void pumpsInit(void); -void triggerPump(uint8_t digital_pins, int16_t milliseconds); +void triggerPump(uint8_t digital_pins); void waterPlant(void); + +#endif diff --git a/firmware/atmega/source/pump.c b/firmware/atmega/source/pump.c index 9cca39d..0e2d5f3 100644 --- a/firmware/atmega/source/pump.c +++ b/firmware/atmega/source/pump.c @@ -1,30 +1,32 @@ #include #include #include +#include "pump.h" #include "config.h" void pumpsInit(void) { - // Set as input - DDRD &= ~((1 << PUMP_D7) | - (1 << PUMP_D6) | - (1 << PUMP_D5) | - (1 << PUMP_D4) | - (1 << PUMP_D3) | - 0 | 0 | 0 ); + uint8_t actuatorMask = 0; + for (uint8_t i = 0; i < ACTUATORS_NUM; i++) { + actuatorMask |= (1 << actuatorPins[i]); + } + DDRD |= actuatorMask; } -void triggerPump(int8_t pump_pin, int16_t milliseconds) { +void triggerPump(int8_t pump_pin) { // Turn on pump PORTD |= (1 << pump_pin); - // timer for n milliseconds - // NOTE: watchout for overflows!! create a safety net. + // timer for a fixed n milliseconds + // hardcoded for now // Turn off pump - //PORTD &= ~(1 << pump_pin); + PORTD &= ~(1 << pump_pin); } void waterPlant(void) { - //for each pump --> turn em on - //better to have it here as function instead of doing it in main + for (int i=0; i < ACTUATORS_NUM; i++) { + triggerPump(actuatorPins[i]); + } + + }