forked from oliverk71/Coffeemaker-Payment-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEPROM_tool.ino
242 lines (224 loc) · 6.86 KB
/
EEPROM_tool.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <SoftwareSerial.h>
#include <EEPROM.h>
SoftwareSerial myCoffeemaker(4,5); // RX, TX
long int cardNr;
union{
byte cardByte[4];
long int cardNr;
}
cardConvert;
union{
byte creditByte[2];
int creditInt;
}
creditConvert;
void setup() {
Serial.begin(9600);
myCoffeemaker.begin(9600);
Serial.println("(1) read data in Arduino EEPROM and display HEX values ");
Serial.println("(2) read data in Arduino EEPROM and display card numbers, credits and price list.");
Serial.println("(3) erase all data in Arduino EEPROM (overwrite with zeros)");
Serial.println("(4) initialize for CoffeemakerPM (erases EEPROM and activates Inkasso mode.)");
Serial.println("(5) deactivate incasso mode.");
Serial.println("(6) read EEPROM of the coffeemaker and display the HEX values.");
}
void loop() {
if (Serial.available()){
char c = Serial.read();
if (c == '1'){
for (int a = 0; a < 1024; a+=16){
for (int a2 = 0; a2 < 16; a2++){
byte b = EEPROM.read(a+a2);
if (b < 16){
Serial.print('0');
}
Serial.print(b, HEX);
Serial.print("\t");
}
Serial.println();
}
}
if (c == '2'){
Serial.println();
Serial.println("Registered cards");
Serial.println("================");
for (int i = 0; i < 40; i++){ // read card numbers and referring credit from EEPROM
cardConvert.cardByte[0] = EEPROM.read(i*6);
cardConvert.cardByte[1] = EEPROM.read(i*6+1);
cardConvert.cardByte[2] = EEPROM.read(i*6+2);
cardConvert.cardByte[3] = EEPROM.read(i*6+3);
Serial.print(i);
Serial.print("\t");
Serial.print(print10digits(cardConvert.cardNr));
Serial.print("\t");
creditConvert.creditByte[0] = EEPROM.read(i*6+4);
creditConvert.creditByte[1] = EEPROM.read(i*6+5);
Serial.println(printCredit(creditConvert.creditInt));
}
Serial.println();
Serial.println("Price list");
Serial.println("==========");
for (int i = 0; i < 10; i++){ // read price list products 1 to 10 and start value for new cards
creditConvert.creditByte[0] = EEPROM.read(i*2+1000);
creditConvert.creditByte[1] = EEPROM.read(i*2+1001);
Serial.print("product ");
Serial.print(i+1);
Serial.print("\t");
Serial.println(printCredit(creditConvert.creditInt));
}
Serial.println();
Serial.print("Standard value for newly registered cards: ");
creditConvert.creditByte[0] = EEPROM.read(10*2+1000);
creditConvert.creditByte[1] = EEPROM.read(10*2+1001);
Serial.println(printCredit(creditConvert.creditInt));
}
if (c == '3' || c == '4'){
for (int a = 0; a < 1023; a++ ){
EEPROM.write(a, 0);
}
Serial.println("EEPROM deleted");
}
if (c == '4'){
toCoffeemaker("?M3\r\n");
delay(10);
if (fromCoffeemaker() == "?ok\r\n"){
Serial.println("Inkasso mode activated");
} else {
Serial.print("error: coffeemaker not responding");
}
}
if (c == '5'){
toCoffeemaker("?M1\r\n");
delay(10);
if (fromCoffeemaker() == "?ok\r\n"){
Serial.println("Inkasso mode deactivated");
} else {
Serial.print("error: coffeemaker not responding");
}
}
if (c == '6'){
Serial.println();
for (int i = 0; i <= 0x7F; i++ ){ // 100 nur testweise
String outputString = "RE:";
// if (a < 10){
// outputString += "000";
// }
// else if (a < 100) {
// outputString += "00";
// }
// else if (a < 1000){
// // outputString="";
// outputString += "0";
// }
if(i <= 0xF){
outputString += "0";
}
outputString += String(i, HEX);
outputString.toUpperCase();
outputString += "\r\n";
toCoffeemaker(outputString);
// Serial.print(outputString);
delay(50); // wait for answer?
String inputString = fromCoffeemaker();
inputString.remove(0,3);
inputString.remove(4);
// String Hex = inputString.charAt(2)+""+inputString.charAt(3);
// Hex += inputString.charAt(0)+""+inputString.charAt(1);
// byte hex = byte(Hex);
if (((i+8)%8) == 0){
Serial.print("00");
if(i<=0xF) Serial.print("0");
Serial.print(i, HEX);
Serial.print(" ");
}
Serial.print(inputString);
Serial.print(" ");
if (((i+1)%8) == 0){
Serial.println();
}
delay(100);
}
}
}
}
String fromCoffeemaker(){
String inputString = "";
char d4 = 255;
while (myCoffeemaker.available()){ // if data is available to read
byte d0 = myCoffeemaker.read();
delay (1);
byte d1 = myCoffeemaker.read();
delay (1);
byte d2 = myCoffeemaker.read();
delay (1);
byte d3 = myCoffeemaker.read();
delay (7);
bitWrite(d4, 0, bitRead(d0,2));
bitWrite(d4, 1, bitRead(d0,5));
bitWrite(d4, 2, bitRead(d1,2));
bitWrite(d4, 3, bitRead(d1,5));
bitWrite(d4, 4, bitRead(d2,2));
bitWrite(d4, 5, bitRead(d2,5));
bitWrite(d4, 6, bitRead(d3,2));
bitWrite(d4, 7, bitRead(d3,5));
if (d4 != 10){
inputString += d4;
}
else {
inputString += d4;
return(inputString);
}
}
}
void toCoffeemaker(String outputString)
{
// Serial.println();
// Serial.print(outputString);
// Serial.println(outputString.length());
for (byte a = 0; a < outputString.length(); a++){
// Serial.print(outputString.charAt(a));
byte d0 = 255;
byte d1 = 255;
byte d2 = 255;
byte d3 = 255;
bitWrite(d0, 2, bitRead(outputString.charAt(a),0));
bitWrite(d0, 5, bitRead(outputString.charAt(a),1));
bitWrite(d1, 2, bitRead(outputString.charAt(a),2));
bitWrite(d1, 5, bitRead(outputString.charAt(a),3));
bitWrite(d2, 2, bitRead(outputString.charAt(a),4));
bitWrite(d2, 5, bitRead(outputString.charAt(a),5));
bitWrite(d3, 2, bitRead(outputString.charAt(a),6));
bitWrite(d3, 5, bitRead(outputString.charAt(a),7));
myCoffeemaker.write(d0);
delay(1);
myCoffeemaker.write(d1);
delay(1);
myCoffeemaker.write(d2);
delay(1);
myCoffeemaker.write(d3);
delay(7);
}
}
String printCredit(int credit){
int euro = ((credit)/100); // int euro = ((credit*10)/100);
int cent = ((credit)%100); // int cent = ((credit*10)%100);
String(output);
output = String(euro);
output += ',';
output += String(cent);
if (cent < 10){
output += '0';
}
output += F(" EUR");
return output;
}
String print10digits(long int number) {
String(tempString) = String(number);
String(newString) = "";
int i = 10-tempString.length();
for (int a = 0; a < (10-tempString.length()); a++){
newString += "0";
}
newString += number;
return newString;
}