Restructure directory organization

This commit is contained in:
2025-05-13 11:28:18 +02:00
parent fff7c84206
commit 4ab01eb3a4
6 changed files with 18 additions and 11 deletions
+18
View File
@@ -0,0 +1,18 @@
FROM ubuntu:22.04
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y \
build-essential \
gcc-avr \
avrdude \
arduino-core-avr \
make \
&& apt-get clean
WORKDIR /usr/src/
COPY . .
RUN make all
CMD ["./main"]
+88
View File
@@ -0,0 +1,88 @@
PROJECT = plant-manager
MCU = atmega328p
F_CPU = 16000000UL
PROGRAMMER = usbasp
ARDUINO_INSTALL_PATH = /usr/share/arduino
ARDUINO_CORE_PATH = $(ARDUINO_INSTALL_PATH)/hardware/arduino/avr/cores/arduino
ARDUINO_VARIANT_PATH = $(ARDUINO_INSTALL_PATH)/hardware/arduino/avr/variants/standard
CC = avr-gcc
CXX = avr-g++
LD = $(CXX)
OC = avr-objcopy
AD = avrdude
AR = avr-ar
RM = rm -f
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)/obj
CORE_DIR = $(BUILD_DIR)/core
CFLAGS = -Wall -Os -DF_CPU=$(F_CPU) -mmcu=$(MCU) -I$(ARDUINO_CORE_PATH) -I$(ARDUINO_VARIANT_PATH) -I.
CXXFLAGS = $(CFLAGS) -fno-exceptions -fpermissive -std=gnu++11
LDFLAGS = -Wall -Os -mmcu=$(MCU) -Wl,--gc-sections
LIBS = -lm
SRCS_CPP = main.cpp moisture.cpp
HDRS = moisture.h config.h
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS_CPP:.cpp=.o))
CORE_C_SRCS = \
wiring.c \
wiring_analog.c \
wiring_digital.c \
hooks.c
CORE_CPP_SRCS = \
abi.cpp \
new.cpp \
Print.cpp \
Stream.cpp \
HardwareSerial.cpp \
HardwareSerial0.cpp \
main.cpp
CORE_SRCS = $(addprefix $(ARDUINO_CORE_PATH)/, $(CORE_C_SRCS) $(CORE_CPP_SRCS))
CORE_OBJS = $(patsubst $(ARDUINO_CORE_PATH)/%.c,$(CORE_DIR)/%.o,$(filter %.c,$(CORE_SRCS))) \
$(patsubst $(ARDUINO_CORE_PATH)/%.cpp,$(CORE_DIR)/%.o,$(filter %.cpp,$(CORE_SRCS)))
CORE_LIB = $(BUILD_DIR)/libcore.a
EXEC = moisture
ELF_TARGET = $(BUILD_DIR)/$(EXEC).elf
TARGET = $(BUILD_DIR)/$(EXEC).hex
all: $(TARGET)
$(shell mkdir -p $(OBJ_DIR) $(CORE_DIR))
$(ELF_TARGET): $(OBJS) $(CORE_LIB)
$(LD) $(LDFLAGS) $(OBJS) -L$(BUILD_DIR) -lcore $(LIBS) -o $@
$(CORE_LIB): $(CORE_OBJS)
$(AR) rcs $@ $^
# Compile your C++ files
$(OBJ_DIR)/%.o: %.cpp $(HDRS) Makefile
$(CXX) $(CXXFLAGS) -c $< -o $@
$(CORE_DIR)/%.o: $(ARDUINO_CORE_PATH)/%.c Makefile
$(CC) $(CFLAGS) -c $< -o $@
$(CORE_DIR)/%.o: $(ARDUINO_CORE_PATH)/%.cpp Makefile
$(CXX) $(CXXFLAGS) -c $< -o $@
$(TARGET): $(ELF_TARGET)
$(OC) -O ihex -R .eeprom $< $@
flash: $(TARGET)
$(AD) -v -p $(MCU) -c $(PROGRAMMER) -D -U flash:w:$<:i
clean:
$(RM) $(TARGET) $(ELF_TARGET) $(OBJS) $(CORE_OBJS) $(CORE_LIB)
$(RM) -r $(BUILD_DIR)
.PHONY: all flash clean
+7
View File
@@ -0,0 +1,7 @@
#ifndef CONFIG_H
#define CONFIG_H
#define MOISTURE_SENSOR_A0 A0
#define SENSOR_READINGS 3
#endif
+44
View File
@@ -0,0 +1,44 @@
#include <Arduino.h>
#include <stdint.h>
#include <stdbool.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include "moisture.h"
ISR(WDT_vect) {
readSensors = true;
}
void watchdogs(void) {
cli();
MCUSR &= ~(1 << WDRF);
WDTCSR |= (1 << WDCE) | (1 << WDE);
WDTCSR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0);
sei();
}
void enterSleep(void) {
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_cpu();
sleep_disable();
}
void setup(void) {
Serial.begin(9600);
initMoistureSensor();
watchdogs();
}
void loop(void) {
if (readSensors) {
int16_t result = readMoisture();
if (result != -1) {
Serial.println(result);
readSensors = false;
}
}
enterSleep();
}
+40
View File
@@ -0,0 +1,40 @@
#include <Arduino.h>
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "moisture.h"
#include "config.h"
static int16_t moistureReadingSum = 0;
static int8_t moistureReadingCount = 0;
volatile bool readSensors = false;
void initMoistureSensor(void) {
pinMode(MOISTURE_SENSOR_A0, INPUT);
}
void triggerSensorRead(void) {
readSensors = true;
}
int16_t readMoisture(void) {
if (!readSensors)
return -1;
moistureReadingSum += analogRead(MOISTURE_SENSOR_A0);
moistureReadingCount++;
//TODO: implement variance check (if over a certain variance discard measurements for this cicle)
if (moistureReadingCount >= SENSOR_READINGS) {
int16_t avg = moistureReadingSum / SENSOR_READINGS;
//Serial.println(avg);
moistureReadingSum = 0;
moistureReadingCount = 0;
readSensors = false;
return avg;
} else readSensors = true;
return -1;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef MOISTURE_H
#define MOISTURE_H
#include <stdint.h>
#include <stdbool.h>
void initMoistureSensor(void);
int16_t readMoisture(void);
void triggerSensorRead(void);
extern volatile bool readSensors;
#endif