Created
May 20, 2009 14:33
-
-
Save abloom/114841 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <Nixie.h> | |
#include <MultiDAC.h> | |
#include "messaging.h" | |
// message types | |
#define DEMO_ID 1 | |
#define MESSAGE_ID 2 | |
#define CLEAR_ID 3 | |
#define BAR_GRAPH_ID 4 | |
#define MESSAGE_SIZE 2 | |
#define DEMO_DELAY 175 | |
#define DEMO_DELAY_2 20 | |
#define numDigits 2 | |
#define nixieDataPin 2 // data line or SER | |
#define nixieClockPin 3 // clock pin or SCK | |
#define nixieLatchPin 4 // latch pin or RCK | |
#define barGraphDataPin 5 // data line or SER | |
#define barGraphClockPin 6 // clock pin or SCK | |
#define barGraphLatchPin 7 // latch pin or RCK | |
Nixie nixie(nixieDataPin, nixieClockPin, nixieLatchPin); | |
MultiDAC barGraph(barGraphDataPin, barGraphClockPin, barGraphLatchPin); | |
boolean message_processed = true; | |
MessageData last_message; | |
int demo_count = 0; | |
int demo_direction = 1; | |
unsigned long demo_timer = 0; | |
int demo_count_2 = 0; | |
int demo_direction_2 = 1; | |
unsigned long demo_timer_2 = 0; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("ready"); | |
nixie.clear(numDigits); | |
barGraph.clear(1); | |
last_message.body = (char*)malloc(0); | |
last_message.type = DEMO_ID; | |
} | |
/* Check if a message is ready in the buffer (at least the header) | |
returns a MessageData struct with the 'present' field set (possibly size and type) */ | |
MessageData serial_message_ready() | |
{ | |
MessageData data; | |
data.present = false; | |
// 4 char headers (TypeTypeSizeSize) | |
if (Serial.available() >= 4) { | |
char type[2], size[2]; | |
type[0] = Serial.read(); | |
type[1] = Serial.read(); | |
size[0] = Serial.read(); | |
size[1] = Serial.read(); | |
// we only care about the integer version | |
data.type = atoi(type); | |
data.size = atoi(size); | |
} | |
return data; | |
} | |
/* Using the MessageData size, this function will return a complete MessageData struct | |
with a char* body of serial_data.size */ | |
MessageData serial_read_message(MessageData serial_data) | |
{ | |
serial_data.body = (char*)malloc(sizeof(char) * serial_data.size); | |
for(int i = 0; i < serial_data.size; i++) | |
serial_data.body[i] = Serial.read(); | |
return serial_data; | |
} | |
/* Using the last known message, fire off whatever action it requested */ | |
void run() | |
{ | |
switch(last_message.type) | |
{ | |
case DEMO_ID: | |
run_demo(); | |
break; | |
case MESSAGE_ID: | |
display_nixie_message(last_message.body, last_message.size); | |
break; | |
case CLEAR_ID: | |
clear_nixies(); | |
break; | |
case BAR_GRAPH_ID: | |
display_bargraph_message(last_message.body, last_message.size); | |
break; | |
} | |
} | |
void loop() { | |
MessageData serial_data = serial_message_ready(); | |
if (serial_data.present) | |
{ | |
free(last_message.body); | |
last_message = serial_read_message(serial_data); | |
} | |
run(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MESSAGING_H | |
#define MESSAGING_H | |
typedef struct | |
{ | |
bool present; | |
int type; | |
int size; | |
char* body; | |
} MessageData; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment