Plant Watering Sensor
Settings Namespace Reference

This module contains access to the permanent settings of the device. More...

Classes

struct  Data
 The internal structure of the data. More...
 

Functions

uint16_t calculateCrc ()
 Calculate the CRC of the current SRAM data. More...
 
void read ()
 Read all settings into SRAM. More...
 
void write ()
 Write all settings back to EEPROM. More...
 
uint8_t getSetPointFrequency ()
 Read the set-point value. More...
 
void setSetPointFrequency (uint8_t frequency)
 Write a new set-point value. More...
 

Variables

const uint16_t cCrcStartMagic = 0xa004u
 The CRC start magic. More...
 
const uint8_t cMagicSize = 4
 The size of the magic. More...
 
EEMEM Data eData
 Allocate the data in the EEPROM memory. More...
 
Data gData
 Allocate the data in SRAM. More...
 
PROGMEM const Data cDefaultData
 Allocate default data in Flash memory. More...
 

Detailed Description

This module contains access to the permanent settings of the device.

Function Documentation

◆ calculateCrc()

uint16_t Settings::calculateCrc ( )

Calculate the CRC of the current SRAM data.

The CRC is calculated using the cCrcStartMagic as start point over the whole data structure with the CRC in the structure set to zero.

Definition at line 73 of file Settings.cpp.

74 {
75  uint16_t crc = cCrcStartMagic;
76  uint16_t originalCrc = gData.crc;
77  for (uint8_t i = 0; i < (sizeof(Data)-2); ++i) {
78  crc = _crc16_update(crc, reinterpret_cast<uint8_t*>(&gData)[i]);
79  }
80  gData.crc = originalCrc;
81  return crc;
82 }
const uint16_t cCrcStartMagic
The CRC start magic.
Definition: Settings.cpp:37
uint16_t crc
Definition: Settings.cpp:48
Data gData
Allocate the data in SRAM.
Definition: Settings.cpp:57

◆ getSetPointFrequency()

uint8_t Settings::getSetPointFrequency ( )

Read the set-point value.

Returns
The set-point value as oscillator frequency.

Definition at line 112 of file Settings.cpp.

113 {
114  return static_cast<uint8_t>(gData.setPoint);
115 }
Data gData
Allocate the data in SRAM.
Definition: Settings.cpp:57
uint16_t setPoint
Definition: Settings.cpp:47

◆ read()

void Settings::read ( )

Read all settings into SRAM.

Definition at line 85 of file Settings.cpp.

86 {
87  // Read Data structure from EEPROM.
88  eeprom_read_block(&gData, &eData, sizeof(Data));
89  // Check the magic in the structure.
90  if (memcmp_P(gData.magic, cDefaultData.magic, cMagicSize) == 0) {
91  // Check the CRC of the buffer.
92  const uint16_t calculatedCrc = calculateCrc();
93  // Check if the CRC of the buffer is correct.
94  if (gData.crc == calculatedCrc) {
95  return;
96  }
97  }
98  // Copy the default settings into SRAM.
99  memcpy_P(&gData, &cDefaultData, sizeof(Data));
100 }
const uint8_t cMagicSize
The size of the magic.
Definition: Settings.cpp:41
EEMEM Data eData
Allocate the data in the EEPROM memory.
Definition: Settings.cpp:53
uint16_t crc
Definition: Settings.cpp:48
uint16_t calculateCrc()
Calculate the CRC of the current SRAM data.
Definition: Settings.cpp:73
PROGMEM const Data cDefaultData
Allocate default data in Flash memory.
Definition: Settings.cpp:61
uint8_t magic[cMagicSize]
Definition: Settings.cpp:46
Data gData
Allocate the data in SRAM.
Definition: Settings.cpp:57

◆ setSetPointFrequency()

void Settings::setSetPointFrequency ( uint8_t  frequency)

Write a new set-point value.

Parameters
frequencyThe new oscillator frequency.

Definition at line 118 of file Settings.cpp.

119 {
120  gData.setPoint = frequency;
121 }
Data gData
Allocate the data in SRAM.
Definition: Settings.cpp:57
uint16_t setPoint
Definition: Settings.cpp:47

◆ write()

void Settings::write ( )

Write all settings back to EEPROM.

Definition at line 103 of file Settings.cpp.

104 {
105  // Calculate the CRC of the current data values.
106  gData.crc = calculateCrc();
107  // Write the whole data structure into the EEPROM.
108  eeprom_update_block(&gData, &eData, sizeof(Data));
109 }
EEMEM Data eData
Allocate the data in the EEPROM memory.
Definition: Settings.cpp:53
uint16_t crc
Definition: Settings.cpp:48
uint16_t calculateCrc()
Calculate the CRC of the current SRAM data.
Definition: Settings.cpp:73
Data gData
Allocate the data in SRAM.
Definition: Settings.cpp:57

Variable Documentation

◆ cCrcStartMagic

const uint16_t Settings::cCrcStartMagic = 0xa004u

The CRC start magic.

Definition at line 37 of file Settings.cpp.

◆ cDefaultData

PROGMEM const Data Settings::cDefaultData
Initial value:
= {
{'L','R','p','s'},
0,
}
const uint8_t cDefaultSetPointFrequency
The default set-point value if none is set.
Definition: Configuration.h:81

Allocate default data in Flash memory.

Definition at line 61 of file Settings.cpp.

◆ cMagicSize

const uint8_t Settings::cMagicSize = 4

The size of the magic.

Definition at line 41 of file Settings.cpp.

◆ eData

EEMEM Data Settings::eData

Allocate the data in the EEPROM memory.

Definition at line 53 of file Settings.cpp.

◆ gData

Data Settings::gData

Allocate the data in SRAM.

Definition at line 57 of file Settings.cpp.