Unit 10 – Brief 1: Rest in Pixels – Thermo Printer Arduino & Processing Code

Ardunio;

/*************************************************************************
This is an Arduino library for the Adafruit Thermal Printer.
Pick one up at –> http://www.adafruit.com/products/597
These printers use TTL serial to communicate, 2 pins are required.

Adafruit invests time and resources providing this open source code.
Please support Adafruit and open-source hardware by purchasing products
from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution.
*************************************************************************/

// If you’re using Arduino 1.0 uncomment the next line:
#include “SoftwareSerial.h”
// If you’re using Arduino 23 or earlier, uncomment the next line:
//#include “NewSoftSerial.h”

#include “Adafruit_Thermal.h”
#include “adalogo.h”
#include “adaqrcode.h”
#include <avr/pgmspace.h>

int printer_RX_Pin = 5; // This is the green wire
int printer_TX_Pin = 6; // This is the yellow wire

Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);

String inputString = “”; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete

void setup(){
Serial.begin(9600);
pinMode(7, OUTPUT); digitalWrite(7, LOW); // To also work w/IoTP printer
printer.begin();
// reserve 200 bytes for the inputString:
inputString.reserve(200);

/*
// Test inverse on & off
printer.inverseOn();
printer.println(“Inverse ON”);
printer.inverseOff();

// Test character double-height on & off
printer.doubleHeightOn();
printer.println(“Double Height ON”);
printer.doubleHeightOff();

// Set text justification (right, center, left) — accepts ‘L’, ‘C’, ‘R’
printer.justify(‘R’);
printer.println(“Right justified”);
printer.justify(‘C’);
printer.println(“Center justified”);
printer.justify(‘L’);
printer.println(“Left justified”);

// Test more styles
printer.boldOn();
printer.println(“Bold text”);
printer.boldOff();

printer.underlineOn();
printer.println(“Underlined text “);
printer.underlineOff();

printer.setSize(‘L’); // Set type size, accepts ‘S’, ‘M’, ‘L’
printer.println(“Large”); // Print line
printer.setSize(‘M’);
printer.println(“Medium”);
printer.setSize(‘S’);
printer.println(“Small”);

printer.justify(‘C’);
printer.println(“normal\nline\nspacing”);
printer.setLineHeight(50);
printer.println(“Taller\nline\nspacing”);
printer.setLineHeight(); // Reset to default
printer.justify(‘L’);

// Barcode examples
printer.feed(1);
// CODE39 is the most common alphanumeric barcode
printer.printBarcode(“ADAFRUT”, CODE39);
printer.setBarcodeHeight(100);
// Print UPC line on product barcodes
printer.printBarcode(“123456789123”, UPC_A);

// Print the 75×75 pixel logo in adalogo.h
printer.printBitmap(adalogo_width, adalogo_height, adalogo_data);

// Print the 135×135 pixel QR code in adaqrcode.h
printer.printBitmap(adaqrcode_width, adaqrcode_height, adaqrcode_data);
printer.println(“Adafruit!”);
printer.feed(1);

printer.sleep(); // Tell printer to sleep
printer.wake(); // MUST call wake() before printing again, even if reset
printer.setDefault(); // Restore printer to defaults
*/
}

// The following function calls are in setup(), but do not need to be.
// Use them anywhere! They’re just here so they’re run only one time
// and not printed over and over.
// Some functions will feed a line when called to ‘solidify’ setting.
// This is normal.

void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
printer.println(inputString);
printer.println(“\n——————————–“);
printer.println(“”);
// clear the string:
inputString = “”;
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == ‘\t’) {
stringComplete = true;
}
}
}

Processing;

//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList<String> words = new ArrayList();
Twitter twitter;
import processing.serial.*;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port

int lineWidth = 32;

void setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);

//Credentials
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(“dn4t7UY1vX3KZNTNOyjIw”);
cb.setOAuthConsumerSecret(“tWVT9FeRAbj4Bas5Vx2wxp01yGkfQabPej8Rv3MNyRo”);
cb.setOAuthAccessToken(“63141740-EM1IOlusFq2b6zNKexbEaQDQJ4dYwDex7Mod38”);
cb.setOAuthAccessTokenSecret(“08KgJInek97tzoslwKmYGvVJCjZfnORm7Ytxam6HKA”);

//Make the twitter object and prepare the query
twitter = new TwitterFactory(cb.build()).getInstance();

}

void draw() {

Query query = new Query(“#R.I.P”);
query.setRpp(1);

//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();

for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println(“Tweet by ” + user + ” at ” + d + “: ” + msg);
msg = msg.replace(“\n”,” “);
msg = msg.replace(“\t”,” “);
String msgToSend = “”;
String curLine = “”;
// Split lines
String[] wordList = msg.split(” “);
for (int j = 0; j < wordList.length; j++) {

if(curLine.length() + wordList[j].length() < lineWidth )
{
curLine += (wordList[j] + ” “);
} else
{
curLine += “\n”;
msgToSend += curLine;
curLine = wordList[j]+” “;
}
}

msgToSend += curLine;
myPort.write(msgToSend+”\t”);

/*
//Break the tweet into words
String[] input = msg.split(” “);
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
*/
};
}
catch (TwitterException te) {
println(“Couldn’t connect: ” + te);
};
println(“——————————————————“);
delay(10000);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: