Make pumps initialization and actuation more dynamic by checking which one are declared in config.h

This commit is contained in:
2025-05-17 13:01:33 +02:00
parent 8ad766c377
commit d6bd1932d3
2 changed files with 46 additions and 14 deletions
+31 -1
View File
@@ -1,9 +1,39 @@
#ifndef PUMP_H
#define PUMP_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "config.h" #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 pumpsInit(void);
void triggerPump(uint8_t digital_pins, int16_t milliseconds); void triggerPump(uint8_t digital_pins);
void waterPlant(void); void waterPlant(void);
#endif
+15 -13
View File
@@ -1,30 +1,32 @@
#include <stdint.h> #include <stdint.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "pump.h"
#include "config.h" #include "config.h"
void pumpsInit(void) { void pumpsInit(void) {
// Set as input uint8_t actuatorMask = 0;
DDRD &= ~((1 << PUMP_D7) | for (uint8_t i = 0; i < ACTUATORS_NUM; i++) {
(1 << PUMP_D6) | actuatorMask |= (1 << actuatorPins[i]);
(1 << PUMP_D5) | }
(1 << PUMP_D4) | DDRD |= actuatorMask;
(1 << PUMP_D3) |
0 | 0 | 0 );
} }
void triggerPump(int8_t pump_pin, int16_t milliseconds) { void triggerPump(int8_t pump_pin) {
// Turn on pump // Turn on pump
PORTD |= (1 << pump_pin); PORTD |= (1 << pump_pin);
// timer for n milliseconds // timer for a fixed n milliseconds
// NOTE: watchout for overflows!! create a safety net. // hardcoded for now
// Turn off pump // Turn off pump
//PORTD &= ~(1 << pump_pin); PORTD &= ~(1 << pump_pin);
} }
void waterPlant(void) { void waterPlant(void) {
//for each pump --> turn em on for (int i=0; i < ACTUATORS_NUM; i++) {
//better to have it here as function instead of doing it in main triggerPump(actuatorPins[i]);
}
} }