-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplimentPrinter.ino
298 lines (229 loc) · 5.89 KB
/
ComplimentPrinter.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#include <EEPROM.h>
#include <math.h>
#include "compliments.h"
// Instantiate a Bounce object
#include <Bounce2.h>
Bounce debouncer = Bounce();
int buttonState;
unsigned long buttonPressTimeStamp;
#define BUTTON_PIN 2
#define LED_PIN 7
#define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
boolean printing;
int last = 0;
void EEPROMWritelong(int address, long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
long amountOfPrints = 0;
void setup() {
Serial.begin(115200);
Serial.println("starting..");
pinMode(BUTTON_PIN,INPUT_PULLUP);
printing = false;
debouncer.attach(BUTTON_PIN);
debouncer.interval(5); // interval in ms
pinMode(LED_PIN,OUTPUT);
pinMode(7, OUTPUT); digitalWrite(7, LOW);
randomSeed(analogRead(A0));
// NOTE: SOME PRINTERS NEED 9600 BAUD instead of 19200, check test page.
mySerial.begin(19200); // Initialize SoftwareSerial
printer.begin(100); // Init printer (same regardless of serial type)
digitalWrite(LED_PIN, HIGH );
long val = EEPROMReadlong(0);
Serial.print("Starting; current EEPROM value is ");
Serial.println(val);
if(val == -1)
{
Serial.println("EEPROM NOT set, setting to 1");
EEPROMWritelong(0,1);
val = EEPROMReadlong(0);
Serial.print("Current EEPROM value is ");
Serial.println(val);
}
amountOfPrints = val;
}
char comp[150];
String padWithSpaces(String theString)
{
int StrLen = 30;
int textLen = theString.length();
int leftPad = (StrLen-textLen)/2;
int rightPad = leftPad;
if((StrLen-textLen) % 2 != 0 || (StrLen-textLen) == 1)
{
leftPad++;
}
String fin = "";
for(int x=0;x<leftPad;x++)
{
fin = fin + " ";
}
fin = fin + theString;
for(int x=0;x<rightPad;x++)
{
fin = fin + " ";
}
return fin;
}
void showMem()
{
Serial.print("Free Memory:");Serial.println(freeRam());
}
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void niceCompliment()
{
printer.wake();
amountOfPrints = amountOfPrints +1;
if(amountOfPrints % 10 == 0)
{
EEPROMWritelong(0,amountOfPrints);
}
int i = random(1,36);
if(i == last)
{
i = random(1,36);
}
last = i;
strcpy_P(comp, (char*)pgm_read_word(&(compliments_table[i])));
printer.setLineHeight(24);
/* Top Line */
printer.write(201);
for(int i=0;i<30;i++)
{
printer.write(205);
}
printer.write(187);
printer.println("");
/* Compliment lines */
printer.write(186);
String compHead = "Compliment #";
compHead = compHead + String(amountOfPrints);
printer.print(padWithSpaces(compHead));
printer.write(186);
printer.println("");
printer.write(204);
for(int i=0;i<30;i++)
{
printer.write(205);
}
printer.write(185);
printer.println("");
printer.write(186);
for(int i=0;i<30;i++)
{
printer.print(" ");
}
printer.write(186);
printer.println("");
/* compliment */
String currentLine = "";
int lineLength = 28;
int lastSpace = 0;
int currentPos = 0;
for(int z=0;z<strlen(comp);z++)
{
currentLine = currentLine + String(comp[z]);
currentPos++;
if(comp[z] == ' ' || currentLine.length() == lineLength)
{
if(currentLine.length() >= lineLength)
{
if(comp[z] == ' ')
{
lastSpace = z;
}
String printableLine = currentLine.substring(0,lastSpace);
printableLine.trim();
printer.justify('L');
printer.write(186);
printer.print(padWithSpaces(printableLine));
printer.write(186);
printer.println();
currentLine = currentLine.substring(lastSpace);
currentLine.trim();
lastSpace = 0;
currentPos = currentLine.length();
}
else
{
lastSpace = currentPos;
}
}
}
if(currentLine.length() > 0)
{
currentLine.trim();
printer.justify('L');
printer.write(186);
printer.print(padWithSpaces(currentLine));
printer.write(186);
printer.println();
}
/* Bottom Lines */
printer.write(186);
for(int i=0;i<30;i++)
{
printer.print(" ");
}
printer.write(186);
printer.println("");
printer.write(200);
for(int i=0;i<30;i++)
{
printer.write(205);
}
printer.write(188);
printer.println("");
printer.justify('C');
printer.println("www.andrewmohawk.com/compliment/");
printer.feed(10);
printer.justify('L');
printer.sleep();
}
void loop() {
// Update the Bounce instance :
debouncer.update();
if ( debouncer.fell() )
{
digitalWrite(LED_PIN, LOW );
printing = true;
Serial.println("[+] Printing Compliment");
niceCompliment();
Serial.println("[+] ...Done!");
digitalWrite(LED_PIN, HIGH );
printing = false;
delay(300);
}
delay(50);
}