-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrushping.ino
More file actions
127 lines (108 loc) · 4 KB
/
Copy pathcrushping.ino
File metadata and controls
127 lines (108 loc) · 4 KB
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
// This #include statement was automatically added by the Particle IDE.
#include "Debounce.h"
// Prereq for thermal library?
#include <Particle.h>
#include "Adafruit_Thermal.h"
// Define static variables
#define LOW_DURATION 15000 // milliseconds
#define HIGH_DURATION 250 // milliseconds
#define PRINTER_RATE 9600 // hertz/baud, Adafruit nano printer
// Declare global variables
Adafruit_Thermal printer;
String lastMessage;
bool hasMessage;
unsigned long duration;
unsigned long startTime;
unsigned long currentTime;
Debounce debouncer = Debounce();
int LED_PIN = D7;
int BUTTON_PIN = D0;
//-------------------------------------------------------------
// Clear the internal message buffer
void clearMessage() {
lastMessage = "";
hasMessage = false;
}
void setMessage(String msg) {
// Write message (truncated for buffer size) to internal buffer
lastMessage = msg.substring(0,144);
hasMessage = true;
}
// This function will print whatever message is sent to it
int receiveMessage(String msg) {
// debug: send an event saying we received a message
Particle.publish("message-received", msg);
setMessage(msg);
// When the function is done, return 1 so the sender knows it worked
return 1;
}
//Print a message
void printMessage(String msg) {
// Apparently, we must wake the printer from low power mode before interacting with it
printer.wake();
printer.setDefault();
// Print the message with some ascii borders (32 characters wide)
printer.println("XOXOXOXOXOXOXOXOXOXOXOXOXOXOXOX");
printer.println(msg);
printer.println("XOXOXOXOXOXOXOXOXOXOXOXOXOXOXOX");
// Done printing, feed it some paper to move above the tear line
printer.feed(2);
// Put printer in low power mode between prints (this happens automatically after certain time)
printer.sleep();
//delay(1000L);
// debug: send an event saying we printed a message
Particle.publish("message-printed", msg);
}
// If we have a message in our buffer, print it and then clear the buffer
void printIfMessage() {
if (hasMessage) {
printMessage(lastMessage);
clearMessage();
}
}
void setup() {
// Expose Particle cloud variables and functions
Particle.function("message", receiveMessage);
Particle.variable("hasMessage", hasMessage);
Particle.variable("lastMessage", lastMessage);
// Serial1 is configured to the RX and TX pins on the Photon
Serial1.begin(PRINTER_RATE);
// Start communicating with the printer via the Serial1 pins
printer.begin(&Serial1);
// Preallocate up to 145 characters (bytes) for lastMessage
lastMessage.reserve(145);
lastMessage = "empty";
//Set up [button] LED
pinMode(LED_PIN, OUTPUT); // set LED pin to output
duration = LOW_DURATION;
startTime = millis();
//Set up button
debouncer.attach(BUTTON_PIN, INPUT_PULLUP);
debouncer.interval(80); // minimum push interval in ms
}
void loop() {
currentTime = millis();
// check button status
debouncer.update();
// If we have a message, just keep the LED on
if (hasMessage) {
digitalWrite(LED_PIN, HIGH);
// If we don't have a message, flash the LED every [5] seconds so we know we're alive
} else {
if (currentTime - startTime > duration) {
if (duration == LOW_DURATION) {
digitalWrite(LED_PIN, HIGH);
duration = HIGH_DURATION; // next period is HIGH, so set correct duration
} else {
digitalWrite(LED_PIN, LOW);
duration = LOW_DURATION;
}
startTime = currentTime; // pin has just changed state, so start timing again
}
}
// if button is pressed, print the currently buffered message if we have one
if (debouncer.rose()) {
Particle.publish("button-pressed", "true"); // debug: send an event saying button was pressed
printIfMessage();
}
}