At my local RadioShack they had some TFT LCD shields for sale, so I snagged one. They come from SeeedStudio I have version 1.0, NOT the one with the SD card slot:
I didn't really have an idea when I got it what I was going to use it for. Maybe Game of Life, or a GUI for some robot I hadn't thought up yet, a panel for some kind of home automation, IRC pms or name mentions, etc... I decided on using it to display my laptops temp and some usage info.
Here is a picture of the final result. Sorry for potato quality and sizing:
Now for the part you want, le code. I used Python for the computer to send the data, this is on Arch Linux, but should work for all *nix distros. I didn't test it on Windows, so IDK if they python will work for you Windows folks. I know the Serial lib wors on windows, but if the other libs don't, you might be able to find an alt.
I put the Python script in my autostart script so it runs at boot.
The code is heavy commented, so I wont explain how it works, the comments explain everything.
Arduino:
// Display sensor data on TFT display from SeeedStudio
// Techb 3/20/14
// Arduino Duemilanove ATmega328p
// should also work with any *Standard* Arduino
// that has shield compatibility. Else wiring it by
// hand would be required. See TFT docs for pinouts
// and more info.
//used by SeeedStudio
#include <Seeed_TFT.h>
//Defines for the shield, comes directly from their TFT examples
#ifdef SEEEDUINO
#define YP A2
#define XM A1
#define YM 14
#define XP 17
#endif
//I'm not using an ArduinoMEGA, so this really isn't needed
//But keeping it for compatiablity
#ifdef MEGA
#define YP A2
#define XM A1
#define YM 54
#define XP 57
#endif
//for the serial in buffer
char serIn;
String data;
//for splitting into an array
String dataArray[4];
char dataBuff[50];
int idx = 0;
void setup()
{
//do a .begin() to keep baud correct, else expect
// random ascii and other crap
Serial.begin(9600);
//used by Seeeds' lib
Tft.init();
//set orintation, note (x,y) values are foobar
// Seeeds' lib is kinda crap
Tft.setDisplayDirect(UP2DOWN);
//Serial.println("Init Done, starting...");
}
void loop()
{
//clear the screen before updating the data
// else the updataed values would overlap
Tft.paintScreenBlack();
//Tft.drawString(string, y, x, size, color)
// decreasing y makes the text go down
//Normaly it would be drawString(string, x, y, size, color)
// but as stated above, the lib kinda sucks and seems like
// pixel coords aren't in concideration, so hard code the shit
Tft.drawString("TempF: ", 230, 0, 4, GREEN);
Tft.drawString("Core1: ", 190, 0, 4, GREEN);
Tft.drawString("Core2: ", 150, 0, 4, GREEN);
Tft.drawString("Mem %: ", 110, 0, 4, GREEN);
//check for data on serial
// NOTE: doing *.available() > 0
// will infinate loop
//The concat idea came from:
// http://stackoverflow.com/questions/5697047/convert-serial-read-into-a-useable-string-using-arduino
//Normaly/you could do it the C way and read it in byte by byte
// to a char[]
//Add the data to an array as we get it, hence why I'm using
// and ending delimiter
while (Serial.available()) {
serIn = Serial.read();
//',' seporates; '!' notes end of data sent
if (serIn != ',' && serIn != '!') {
data.concat(serIn);
}
else {
dataArray[idx] = data;
idx++;
data = "";
}
delay(10); //delay cause sometimes it will do char by char
// instead of the full string, results varried
// without it.
}
idx = 0;
//for testing
// replace this with writing to TFT
unsigned int loc = 230;
if (dataArray[0] != NULL) {
for (int i = 0; i < 4; i++) {
//Serial.println(dataArray[i]);
dataArray[i].toCharArray(dataBuff, 50);
Tft.drawString(dataBuff, loc, 190, 4, WHITE);
loc -= 40;
}
}
//Serial.println("*");
delay(5000);
}
Python 2.7.x
#Requires:
# *PySerial
# http://pyserial.sourceforge.net/
# *PSUtil
# http://psutil.readthedocs.org/en
import serial, os, psutil, time, sys
def getSensorData():
#grep magic to get temp
temp = os.popen("sensors | grep temp1 | grep -o '+[0-9]'*").read()
temp = temp.strip()[1:]
#convert to ferenheit, round to nearest whole number
temp = int(round((int(temp)*1.8)+32))
#per core, loop it nigger
cores = psutil.cpu_percent(percpu=True)
#RAM in %, use it nigger...
mem = psutil.virtual_memory().percent
return (temp, cores[0], cores[1], mem)
try:
#use with so in case of unexpected exit, handle is closed
with serial.Serial('/dev/ttyUSB0', 9600) as s:
while True:
data = getSensorData()
s.write("%d,%s,%s,%s!" % data)
print data
time.sleep(5)
except IOError:
print "Failed to get handle, exiting"
sys.exit()