Skip to content

Instantly share code, notes, and snippets.

@micolous
Created November 19, 2012 14:05
Show Gist options
  • Save micolous/4110819 to your computer and use it in GitHub Desktop.
Save micolous/4110819 to your computer and use it in GitHub Desktop.
// Laptop control software
#include <SoftwareSerial.h>
#include <SerialLCD.h>
//#include <NewSoftSerial.h>
#include <math.h>
const int laptopRelayPin = 7;
const int tempPin = 0;
int pcConnected = 0;
int incomingByte = 0;
//char buf[17];
int b2 = 0;
char b3 = 0;
char tempCycle = 0;
SerialLCD slcd(11, 12);
// for temperature sensor
int a, b = 3975;
float resistance, temperature;
float getTemperature() {
a = analogRead(tempPin);
resistance = (float)(1023 - a) * 10000 / a;
temperature = 1 / (log(resistance / 10000) / b + 1 / 298.15) - 273.15;
return temperature;
}
void powerUpLaptop() {
Serial.println("LAPTOP INIT");
// ensure relay is open
digitalWrite(laptopRelayPin, LOW);
// wait a moment
delay(500);
// press the button for a second
digitalWrite(laptopRelayPin, HIGH);
delay(1000);
// let go
digitalWrite(laptopRelayPin, LOW);
Serial.println("LAPTOP WAITING");
}
void setup() {
Serial.begin(9600);
Serial.println("CARDUINO INIT");
slcd.begin();
slcd.print("Booting PC");
slcd.setCursor(0, 0);
//memset(&buf, 0, sizeof(&buf));
pinMode(laptopRelayPin, OUTPUT);
// report we're about to start.
powerUpLaptop();
slcd.print("Waiting for PC ");
slcd.setCursor(0, 0);
}
void loop() {
if (pcConnected == 0 || tempCycle == 0) {
Serial.write("TEMP ");
Serial.println(getTemperature());
}
tempCycle++;
while (Serial.available() > 0) {
// there is data waiting, handle it.
if (pcConnected == 0) {
switch (Serial.read()) {
case '\\':
pcConnected = 1;
Serial.println("HELO");
slcd.clear();
slcd.setCursor(0, 0);
slcd.print("Connected.");
slcd.setCursor(0, 0);
break;
default:
Serial.println("UNINITIALISED");
break;
}
} else if (pcConnected == 1) {
// pc is connected, lets accept some commands
incomingByte = Serial.read();
// we have a data to read, handle it.
// upper 3 bits are the command.
switch (incomingByte & 0xE0) {
case 0x00:
// 0 0 0 == write characters to screen from current pos
// lower 5 bits are length - 1 (1 - 16 byte)
// clear buffer
//memset(&buf, 0, sizeof(&buf));
b3 = (incomingByte & 0x1F) + 1;
for (int x=0; x<b3; x++) {
b2 = Serial.read();
// no data available, retry
if (b2 == -1) {
x--;
continue;
}
// there is a character, push to buffer
//buf[x] = (char)b2;
slcd.print((char)b2);
}
// now all the bytes are read, push it to the screen
//slcd.print(buf);
Serial.println("OK");
//Serial.println(buf);
break;
case 0x20:
// 0 0 1 -- set cursor position
// bit 5 == y
// bit 1 - 4 == x
slcd.setCursor(incomingByte & 0x0F, (incomingByte & 0x10) >> 4);
Serial.print("CURSOR ");
Serial.print(incomingByte & 0x0F, DEC);
Serial.print(" ");
Serial.println((incomingByte & 0x10) >> 4, DEC);
break;
case 0x40:
// 0 1 0 -- management commands
// strip out high bits in case we change protocol
// and to make sure we're not relying on those high bits for params
switch (incomingByte & 0x1F) {
case 0x00:
// 0 0 0 0 0 (0x40) @ : clear display
slcd.clear();
Serial.println("CLEAR");
break;
case 0x01:
// 0 0 0 0 1 (0x41) A : set cursor to home (0, 0)
slcd.home();
Serial.println("HOME");
break;
case 0x02:
// 0 0 0 1 0 (0x42) B : switch off display
slcd.noDisplay();
Serial.println("DISPLAY OFF");
break;
case 0x03:
// 0 0 0 1 1 (0x43) C : switch on display
slcd.display();
Serial.println("DISPLAY ON");
break;
case 0x04:
// 0 0 1 0 0 (0x44) D : switch off blink cursor
slcd.noBlink();
Serial.println("BLINK OFF");
break;
case 0x05:
// 0 0 1 0 1 (0x45) E : switch on blink cursor
slcd.blink();
Serial.println("BLINK ON");
break;
case 0x06:
// 0 0 1 1 0 (0x46) F : switch off cursor
slcd.noCursor();
Serial.println("CURSOR OFF");
break;
case 0x07:
// 0 0 1 1 1 (0x47) G : switch on cursor
slcd.cursor();
Serial.println("CURSOR ON");
break;
case 0x08:
// 0 1 0 0 0 (0x48) H : switch off backlight
slcd.noBacklight();
Serial.println("BACKLIGHT OFF");
break;
case 0x09:
// 0 1 0 0 1 (0x49) I : switch on backlight
slcd.backlight();
Serial.println("BACKLIGHT ON");
break;
/* // these commands can get display controller stuck
case 0x0A:
// 0 1 0 1 0 (0x4A) J : switch off LCD power and backlight
slcd.noPower();
Serial.println("POWER OFF");
break;
case 0x0B:
// 0 1 0 1 1 (0x4B) K : switch on LCD power
slcd.Power();
Serial.println("POWER ON");
break;
*/
case 0x0C:
// 0 1 1 0 0 (0x4C) L : scroll one place to left
slcd.scrollDisplayLeft();
Serial.println("SCROLL LEFT");
break;
case 0x0D:
// 0 1 1 0 1 (0x4D) M : scroll one place to right
slcd.scrollDisplayRight();
Serial.println("SCROLL RIGHT");
break;
case 0x0E:
// 0 1 1 1 0 (0x4E) N : left to right text mode
slcd.leftToRight();
Serial.println("DIR LTR");
break;
case 0x0F:
// 0 1 1 1 1 (0x4F) O : right to left text mode
slcd.rightToLeft();
Serial.println("DIR RTL");
break;
case 0x10:
// 1 0 0 0 0 (0x50) P : disable autoscroll
slcd.noAutoscroll();
Serial.println("AUTOSCROLL OFF");
break;
case 0x11:
// 1 0 0 0 1 (0x51) Q : enable autoscroll
slcd.autoscroll();
Serial.println("AUTOSCROLL ON");
break;
case 0x1C:
// 5C ( \ ) : command that normally switches to PC mode
// pretend to reinit
Serial.println("HELO");
break;
default:
Serial.println("WHAT");
break;
}
break;
default:
Serial.println("WHAT");
break;
}
} else {
// undefined state!
pcConnected = 0;
}
}
if (pcConnected == 0) {
slcd.setCursor(0, 1);
slcd.print((getTemperature()), DEC);
slcd.print(" C "); //\0xDFc ");
Serial.println("WAITING");
delay(1000);
} else {
// run faster when PC connected
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment