abloom (owner)

Forks

Revisions

gist: 114841 Download_button fork
public
Public Clone URL: git://gist.github.com/114841.git
arduino.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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();
}
messaging.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef MESSAGING_H
#define MESSAGING_H
 
typedef struct
{
  bool present;
  int type;
  int size;
  char* body;
} MessageData;
 
#endif