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 <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.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 triggerPump(uint8_t digital_pins, int16_t milliseconds);
void triggerPump(uint8_t digital_pins);
void waterPlant(void);
#endif
+15 -13
View File
@@ -1,30 +1,32 @@
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#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]);
}
}