Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active December 25, 2015 23:49
Show Gist options
  • Save ElectricCoffee/7059260 to your computer and use it in GitHub Desktop.
Save ElectricCoffee/7059260 to your computer and use it in GitHub Desktop.
Simple program that uses the adafruit thermal printer to print out whatever messages you send it, currently it supports bold and underlined text, this sketch requires the Adafruit_Thermal library, which can be found here on GitHub, anything the program prints to serial is purely for debug purposes
#include <Adafruit_Thermal.h>
#include <SoftwareSerial.h>
#define PRINTER_RX 12
#define PRINTER_TX 13
#define SERIAL_BAUD 9600
Adafruit_Thermal printer(PRINTER_RX, PRINTER_TX);
boolean stringComplete = false;
String inputString = "";
void setup() {
Serial.begin(SERIAL_BAUD);
printer.begin();
printer.println("I am ready!");
printer.feed(3);
Serial.println("I am ready too!");
}
void loop() {
if(stringComplete) {
Serial.println("received: " + inputString);
matchString(inputString);
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while(Serial.available()) {
char in = (char) Serial.read();
inputString += in;
if(in == '\n') {
stringComplete = true;
resetPrinter();
break;
}
}
}
void matchString(String input) {
if(containsString(input, "bold")) {
Serial.println("match bold");
printer.boldOn();
}
if(containsString(input, "underlined")) {
Serial.println("match underline");
printer.underlineOn();
}
if(containsString(input, "dprint")) {
printer.println("message: " + extractString(input));
printer.feed(3);
}
else if(containsString(input, "print")) {
printer.println(extractString(input));
printer.feed(3);
}
else delay(100);
}
boolean containsString(String input, String search) {
return input.indexOf(search) > -1; // returns true if the word we search for is first
}
String extractString(String input) {
int
firstquote = input.indexOf('"') + 1,
lastQuote = input.lastIndexOf('"');
return input.substring(firstquote, lastQuote);
}
void resetPrinter() {
printer.sleep();
printer.wake();
printer.setDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment