Viking Skull Lamp  V1.0.1
Loading...
Searching...
No Matches
configuration.cpp
Go to the documentation of this file.
1/*
2 * Created on May 28 2022
3 *
4 * Copyright (c) 2022 - Daniel Hajnal
5 * hajnal.daniel96@gmail.com
6 * This file is part of the Viking Skull Lamp project.
7 * Modified 2022.06.27
8*/
9
10/*
11MIT License
12Copyright (c) 2022 Daniel Hajnal
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28*/
29
30#include "configuration.hpp"
31
32configurationManager::configurationManager( configurationData_t *configTree_p, uint8_t configTreeSize_p ){
33
34 // Save the pointer and the size to internal variable.
35 configTree = configTree_p;
36 configTreeSize = configTreeSize_p;
37
38}
39
41
42 // Generic counter variables.
43 uint8_t i;
44 uint8_t j;
45
46 // Index of the next byte that should be written.
47 // It also shows how many bytes have been written.
48 uint16_t index = 0;
49
50 // Points to the next byte that should be written.
51 uint8_t *dataPtr;
52
53 // Reset CRC to default.
54 crc = 0x00;
55
56 Serial.println( F( "Saving bytes:" ) );
57 for( i = 0; i < configTreeSize; i++ ){
58
59 dataPtr = (uint8_t*)configTree[ i ].dataPointer;
60
61 for( j = 0; j < configTree[ i ].dataSize; j++ ){
62
63 Serial.print( F( "\tElement: " ) );
64 Serial.print( i );
65 Serial.print( F( "\t" ) );
66 Serial.print( index );
67 Serial.print( F( "\t: " ) );
68 Serial.print( *dataPtr );
69 Serial.print( F( "\tHEX: " ) );
70 Serial.println( *dataPtr, HEX );
71
72 EEPROM.update( index, *dataPtr );
73 crcFeed( *dataPtr );
74
75 dataPtr++;
76 index++;
77
78 }
79
80 }
81
82 // Write the CRC as well.
83 EEPROM.update( index, crc );
84
85 Serial.print( F( "\tCalculated CRC: " ) );
86 Serial.println( crc );
87
88}
89
91
92 // Generic counter variables.
93 uint8_t i;
94 uint8_t j;
95
96 // Index of the next byte that should be read.
97 // It also shows how many bytes have been read.
98 uint16_t index = 0;
99
100 // Points to the next byte that should be read.
101 uint8_t *dataPtr;
102
103 // We will read the stored CRC from the EEPROM to this variable.
104 uint8_t crcRead = 0;
105
106 // Reset CRC to default.
107 crc = 0x00;
108
109 Serial.println( F( "Loading bytes:" ) );
110 for( i = 0; i < configTreeSize; i++ ){
111
112 dataPtr = (uint8_t*)configTree[ i ].dataPointer;
113
114 for( j = 0; j < configTree[ i ].dataSize; j++ ){
115
116 *dataPtr = EEPROM.read( index );
117
118 Serial.print( F( "\tElement: " ) );
119 Serial.print( i );
120 Serial.print( F( "\t" ) );
121 Serial.print( index );
122 Serial.print( F( "\t: " ) );
123 Serial.print( *dataPtr );
124 Serial.print( F( "\tHEX: " ) );
125 Serial.println( *dataPtr, HEX );
126
127 crcFeed( *dataPtr );
128
129 dataPtr++;
130 index++;
131
132 }
133
134 }
135
136 // Read the stored CRC.
137 crcRead = EEPROM.read( index );
138
139 // Compare the two CRC
140 if( crcRead != crc ){
141
142 Serial.print( F( "\tCRC error! Calculated: " ) );
143 Serial.print( crc );
144 Serial.print( F( ", read: " ) );
145 Serial.println( crcRead );
146 return false;
147
148 }
149
150 Serial.print( F( "\tCRC OK! Calculated: " ) );
151 Serial.print( crc );
152 Serial.print( F( ", read: " ) );
153 Serial.println( crcRead );
154
155 return true;
156
157}
158
159void configurationManager::crcFeed( uint8_t data ){
160
161 uint8_t j;
162
163 crc ^= data;
164
165 for( j = 0; j < 8; j++ ){
166
167 if( ( crc & 0x80 ) != 0 ){
168
169 crc = (uint8_t)( ( crc << 1 ) ^ 0x31 );
170
171 }
172
173 else{
174
175 crc <<= 1;
176
177 }
178
179 }
180
181}
configurationData_t * configTree
Pointer to configTree.
uint8_t crc
Calculated CRC.
void crcFeed(uint8_t data)
Maxim like crc-8.
bool loadConfig()
Load configuration from EEPROM.
configurationManager(configurationData_t *configTree_p, uint8_t configTreeSize_p)
Constructor.
void saveConfig()
Save configuration to EEPROM.
uint8_t configTreeSize
configTree size.
uint8_t dataSize
The size of the data, that has to be stored or loaded. Specified in bytes.