/*
* The Magic Name to Color Transformer
*
* Type in any name (or string) and see what color correspondence it has.
* Type ~ to toggle debug mode.
*
* Theory and Practise of Tangible User Interfaces
* Lab Assignment 2
*
* Martin Kiechle, Kilian Moser
* 9/13/2011
*/
/*
* variables and allocations
*/
// CRC table
unsigned crctbl[] = {0x0000, 0x1081, 0x2102, 0x3183,
0x4204, 0x5285, 0x6306, 0x7387,
0x8408, 0x9489, 0xa50a, 0xb58b,
0xc60c, 0xd68d, 0xe70e, 0xf78f};
// Output
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
// helper vars
int i = 0;
int DEBUG = 0;
// string and color transformations
char inString[100];
unsigned short checksum;
float hue;
int r,g,b;
int iHue;
// setting it all up
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // ...set up the serial ouput on 0004 style
Serial.println("THE MAGIC NAME-TO-COLOR TRANSFORMER: ");
Serial.println("-------------------------------------- ");
Serial.println("Enter ~ for extended information ");
Serial.println("-------------------------------------- ");
Serial.print("Enter your name: ");
}
// main loop
void loop()
{
memset(inString, 0, 100); // some initializations
readSerialString(inString); // read in some string
if(*inString != 0){
if(*inString == '~'){
DEBUG ^= 1; // debug toggle symbol
Serial.println("debug mode switched");
}
else{
checksum = calc_crc((unsigned char*)inString, getStrLength(inString)); // calc checksum
Serial.println(inString);
hue = checksum * 0.005478; // transform 2 byte checksum to a 0-359 equivalent
HSVtoRGB(&r, &g, &b, hue); // color space conversion
analogWrite(redPin, r); // color output
analogWrite(greenPin, g);
analogWrite(bluePin, b);
// text interface
if(DEBUG){
Serial.print("The CRC checksum of your name is: 0x");
Serial.println(checksum, HEX);
Serial.print("The HSV hue value for the CRC is: ");
iHue = floor(hue);
Serial.println(iHue, DEC);
Serial.print("The resulting RGB values are: Red: ");
Serial.print(r, DEC);
Serial.print(" Green: ");
Serial.print(g, DEC);
Serial.print(" Blue: ");
Serial.println(b, DEC);
}
}
Serial.println("####################################");
Serial.print("Enter your name: ");
}
delay(500); // wait a bit, for serial data
}
// function for reading an input from the serial interface
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
// function for getting the length of a string
unsigned getStrLength (char *strArray) {
int i = 0;
while(*strArray != 0){
strArray++;
i++;
}
return i;
}
// function for calculating the CRC CCITT checksum of a string.
unsigned short calc_crc(unsigned char *ptr, unsigned length)
{
unsigned short crc;
unsigned short i;
unsigned char pos,ch;
crc = 0xffff; /* precondition crc */
for (i = 0; i < length; i++,ptr++) {
ch = *ptr;
pos = (crc ^ ch) & 15;
crc = ((crc >> 4) & 0x0fff) ^ crctbl[pos];
ch >>= 4;
pos = (crc^ch) & 15;
crc = ((crc >> 4) & 0xffff) ^ crctbl[pos];
}
crc = ~crc; /* post condition */
crc = (crc << 8) | (crc >> 8); /* bytewise reverse */
return crc;
}
// color space conversion HSV to RGB --> http://en.wikipedia.org/wiki/HSL_and_HSV
void HSVtoRGB(int *R, int *G, int *B, float h){
float s=1;
float v=1;
float r,g,b;
int i;
float f, p, q, t;
if( s == 0 )
{
r = g = b = v;
return;
}
h /= 60;
i = floor( h );
f = h - i;
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
switch( i ){
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
default: r = v; g = p; b = q; break;
}
*R = r * 255;
*G = g * 255;
*B = b * 255;
}