Plant Watering Sensor
Logic Namespace Reference

This module contains the logic of the firmware. More...

Enumerations

enum  UserAction : uint8_t { UA_None, UA_NewSetPoint, UA_DisableFlash }
 The action the user would like to do. More...
 

Functions

UserAction getUserAction ()
 Wait for the button release to get the selected user action. More...
 
void configurationMode ()
 The configuration mode. More...
 
void testMode ()
 The test mode. More...
 
void main ()
 The main loop of the firmware. More...
 

Variables

const char cFirmwareSignature [] PROGMEM = "FW" LR_FIRMWARE_VERSION ":LuckyResistor"
 A message for the release version written into the image. More...
 

Detailed Description

This module contains the logic of the firmware.

Enumeration Type Documentation

◆ UserAction

enum Logic::UserAction : uint8_t

The action the user would like to do.

Enumerator
UA_None 

No action, the user accidentally pressed the button.

UA_NewSetPoint 

Write a new set-point with the current value.

UA_DisableFlash 

Disable the annoying flashing until a new set-point is written.

Definition at line 58 of file Logic.cpp.

58  : uint8_t {
59  UA_None,
62 };
Write a new set-point with the current value.
Definition: Logic.cpp:60
Disable the annoying flashing until a new set-point is written.
Definition: Logic.cpp:61
No action, the user accidentally pressed the button.
Definition: Logic.cpp:59

Function Documentation

◆ configurationMode()

void Logic::configurationMode ( )

The configuration mode.

A sub logic entered if the button is pressed on the interface.

Definition at line 97 of file Logic.cpp.

98 {
99  // Check the requested user action.
100  const UserAction userAction = getUserAction();
101  if (userAction == UA_NewSetPoint) {
102  // Write the current oscillator frequency as new set-point to the settings.
104  Settings::write();
105  } else if (userAction == UA_DisableFlash) {
107  Settings::write();
108  }
109  // Just ignore the action otherwise.
110 }
UserAction
The action the user would like to do.
Definition: Logic.cpp:58
UserAction getUserAction()
Wait for the button release to get the selected user action.
Definition: Logic.cpp:69
Write a new set-point with the current value.
Definition: Logic.cpp:60
const uint8_t cSetPointCompensation
Set-point compensation for human touch of the button.
Definition: Configuration.h:89
Disable the annoying flashing until a new set-point is written.
Definition: Logic.cpp:61
volatile uint8_t currentOscillatorFrequency
The current oscillator frequency.
Definition: Logic.cpp:45
const uint8_t cDisabledSetPointFrequency
The set-point value if the user disabled it.
Definition: Configuration.h:85
void write()
Write all settings back to EEPROM.
Definition: Settings.cpp:103
void setSetPointFrequency(uint8_t frequency)
Write a new set-point value.
Definition: Settings.cpp:118

◆ getUserAction()

UserAction Logic::getUserAction ( )

Wait for the button release to get the selected user action.

Returns
The selected user action.

Definition at line 69 of file Logic.cpp.

70 {
71  // Pulse green for ~3 seconds while the button is pressed.
72  for (uint8_t i = 0; i < Configuration::cLogicPulsesForUserAction; ++i) {
75  // Abort if the button is released in this state.
76  return UA_None;
77  }
78  }
79  // After green, pulse for ~3 seconds in red.
80  for (uint8_t i = 0; i < Configuration::cLogicPulsesForUserAction; ++i) {
83  // A new set point shall be selected.
85  return UA_NewSetPoint;
86  }
87  }
88  // The flash should be disabled until a new set-point is written.
90  return UA_DisableFlash;
91 }
void flash(Color color, uint8_t flashCount)
Display a number of flashes.
Definition: Display.cpp:31
void pulse(Color color)
Smooth Pulse one time.
Definition: Display.cpp:58
bool isButtonPressed()
Get the status of the button.
Definition: Hardware.cpp:99
Write a new set-point with the current value.
Definition: Logic.cpp:60
Display a red color.
Definition: Display.h:35
const uint8_t cLogicPulsesForUserAction
The number of pulses for the selection of the user action.
Definition: Configuration.h:77
Display a green color.
Definition: Display.h:36
Disable the annoying flashing until a new set-point is written.
Definition: Logic.cpp:61
No action, the user accidentally pressed the button.
Definition: Logic.cpp:59

◆ main()

void Logic::main ( )

The main loop of the firmware.

Definition at line 139 of file Logic.cpp.

140 {
141  // Setup everything.
142  Hardware::setup();
143 
144  // Test loop (disabled in production)
145  do {
146  // Time elapsed since at this point, after power level on MCU reached 2V:
147  // BOD off SUT = 0b10: ~60ms
148  // BOD on (1.8V) SUT = 0b00: ~4ms
149 
150  // Oscillator is working after 1ms, no delay required.
151 
152  // Step 1: Check the battery level
155  // Signal low battery using two short flashes.
157  // Wait until the button is released
158  while (Hardware::isButtonPressed()) {}
159  goto lDone;
161  testMode();
162  }
165  }
166 
167  // Step 2: Measure the current frequency of the oscillator.
171  }
172 
173  // Step 3: Read the reference frequency from the EEPROM.
174  Settings::read();
175  const uint8_t setpointOscillatorFrequency = Settings::getSetPointFrequency();
176 
177  // Step 4: Check if the button is pressed.
179  // Go to the configuration mode.
181  } else {
182  // Just check the status and give a signal if we are below set-point.
183  if (currentOscillatorFrequency >= setpointOscillatorFrequency) {
184  // Send a single flash signal.
186  }
187  }
188 
189  // Wait 5s if the test loop is enabled.
191  Tool::delayMs(2000);
192  }
193 
195  // On "use" the firmware identifier to keep it in memory.
196  pgm_read_byte(cFirmwareSignature);
197  }
198 
200 
201 lDone:
202  // Log the current values if the logger is activated.
205  }
206 
207  // Send the done signal and repeat until the system timer turns off the power.
208  while (1) {
210  }
211 }
uint8_t getSetPointFrequency()
Read the set-point value.
Definition: Settings.cpp:112
const bool cTestLoopEnabled
This flag enables the test loop, which never stops but runs the logic in a loop.
void read()
Read all settings into SRAM.
Definition: Settings.cpp:85
uint8_t getOscillatorFrequency()
Try to determine the frequency.
Definition: Hardware.cpp:136
volatile uint16_t currentVoltage
The current voltage value.
Definition: Logic.cpp:41
const bool cTestModeEnabled
This flag enables the test mode which is activated with a 4V VCC voltage.
void flash(Color color, uint8_t flashCount)
Display a number of flashes.
Definition: Display.cpp:31
const bool cConfigurationModeEnabled
This flag enables the configuration mode.
const bool cLoggingEnabled
This flag enables logging of the last 10 values into SRAM.
void setup()
Setup the MCU hardware to the initial values.
Definition: Hardware.cpp:35
void sendDoneSignal()
Send the done signal.
Definition: Hardware.cpp:89
const uint16_t cMinimumVoltageValue
The minimum voltage value.
Definition: Configuration.h:35
bool isButtonPressed()
Get the status of the button.
Definition: Hardware.cpp:99
void shutdownVoltageMeasurement()
Shutdown voltage measurement to save power.
Definition: Hardware.cpp:128
Display a red color.
Definition: Display.h:35
const bool cPowerSave
This flag controls if the times, etc.
Definition: Configuration.h:98
const uint16_t cTestVoltageValue
The test voltage value.
Definition: Configuration.h:41
Display a green color.
Definition: Display.h:36
uint16_t getVoltageValue()
Read the current voltage value.
Definition: Hardware.cpp:105
void configurationMode()
The configuration mode.
Definition: Logic.cpp:97
void testMode()
The test mode.
Definition: Logic.cpp:118
void shutdownOscillatorCounter()
Shutdown the counter to save power.
Definition: Hardware.cpp:156
volatile uint8_t currentOscillatorFrequency
The current oscillator frequency.
Definition: Logic.cpp:45
const bool cFirmwareSignatureEnabled
This flag enables the firmware signature text.
void log(uint8_t oscillatorFrequency, uint16_t voltageValue, bool isButtonPressed)
Log one entry into EEPROM.
void delayMs(uint16_t delay)
Wait approximate the given number of milliseconds.
Definition: Tool.cpp:31

◆ testMode()

void Logic::testMode ( )

The test mode.

A sub logic entered if the supply voltage is >4V.

Definition at line 118 of file Logic.cpp.

119 {
120  while (1) {
121  for (uint8_t i = 0; i < 4; i++) {
124  }
125  for (uint8_t i = 0; i < 100; ++i) {
131  } else {
132  Tool::delayMs(100);
133  }
134  }
135  }
136 }
uint8_t getOscillatorFrequency()
Try to determine the frequency.
Definition: Hardware.cpp:136
void dimSignal(Color color)
Very short dim signal.
Definition: Display.cpp:73
Display a red color.
Definition: Display.h:35
Display a green color.
Definition: Display.h:36
const uint16_t cTestLowOscillator
Test value for glass of water.
Definition: Configuration.h:49
volatile uint8_t currentOscillatorFrequency
The current oscillator frequency.
Definition: Logic.cpp:45
const uint16_t cTestHighOscillator
Test value for air.
Definition: Configuration.h:45
void delayMs(uint16_t delay)
Wait approximate the given number of milliseconds.
Definition: Tool.cpp:31

Variable Documentation

◆ PROGMEM

const char cFirmwareSignature [] Logic::PROGMEM = "FW" LR_FIRMWARE_VERSION ":LuckyResistor"

A message for the release version written into the image.

Definition at line 53 of file Logic.cpp.