wasylm (owner)

Fork Of

Revisions

gist: 114850 Download_button fork
public
Public Clone URL: git://gist.github.com/114850.git
Embed All Files: show embed
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
120
121
#include <stdlib.h>
#include <string.h>
#include <Nixie.h>
#include <MultiDAC.h>
#include "messaging.h"
 
Nixie nixie(NIXIE_PIN_DATA, NIXIE_PIN_CLK, NIXIE_PIN_LATCH);
MultiDAC barGraph(GRAPH_PIN_DATA, GRAPH_PIN_CLK, GRAPH_PIN_LATCH);
 
void setup() {
  Serial.begin(9600);
  Serial.println("ready");
  
  nixie.clear(numDigits);
  barGraph.clear(1);
}
 
void print_err(int err) {
  switch (err) {
    case SERIAL_READ_OK:
      printf("The operation completed successfully\n");
      break;
      
    case SERIAL_READ_ERR_FORMAT:
      printf("The message was improperly formatted or incomplete\n");
      break;
      
    case SERIAL_READ_ERR_EMPTY:
      printf("No message was found\n");
      break;
      
    case default:
      printf("Invalid error id\n");
      break;
  }
}
 
/* Reads and stores a null-terminated message from the serial port
*
* Returns size of message, including null character
*
*/
int serial_read_message(char *msg) {
  char buf[SERIAL_MAX_MSG_LEN], read;
  int read_len = -1, ret;
  
  read = Serial.read();
  
  while (read_len < SERIAL_MAX_MSG_LEN && read != SERIAL_READ_END_CHAR) {
    switch (read) {
      case SERIAL_READ_START_CHAR:
        read_len = 0;
        continue;
        
      case SERIAL_READ_END_CHAR:
        if (read_len > 0) {
          ret = read_len;
        }
        else {
          ret = -SERIAL_READ_ERR_FORMAT;
        }
        break;
        
      case SERIAL_READ_ERR_CHAR:
        ret = -SERIAL_READ_ERR_EMPTY;
        break;
        
      case default:
        if (read_len < 0) {
          ret = -SERIAL_READ_ERR_FORMAT;
        }
        else {
          buf[read_len++] = read;
        }
        break;
        
      read = Serial.read();
    }
  }
  
  return ret;
}
 
/* Using the last known message, fire off whatever action it requested */
void process_message(char *msg) {
  char type_s[3] = {0x00};
  int type;
  
  memcpy(type_s, msg, 2);
  type = atoi(type_s);
  
  switch(type)
  {
    case MSG_TYPE_DEMO:
      run_demo();
      break;
      
    case MSG_TYPE_MESSAGE:
      display_nixie_message(&msg[2]);
      break;
      
    case MSG_TYPE_CLEAR:
      clear_nixies();
      break;
      
    case MSG_TYPE_GRAPH:
      display_bargraph_message(&msg[2]);
      break;
  }
}
 
void loop() {
  char msg[SERIAL_MAX_MSG_LEN];
  int ret;
  
  if ((ret = serial_read_message(msg)) < 0) {
    print_err(ret);
  }
  
  process_message(msg);
}
messaging.h #
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
#ifndef MESSAGING_H
#define MESSAGING_H
 
#define SERIAL_MAX_MSG_LEN 128
 
#define SERIAL_READ_START_CHAR '['
#define SERIAL_READ_END_CHAR ']'
#define SERIAL_READ_ERR_CHAR 0xff
 
#define DEMO_DELAY 175
#define DEMO_DELAY_2 20
 
#define NIXIE_NUM_DIGITS 2
 
#define NIXIE_PIN_DATA 2 /* data line or SER */
#define NIXIE_PIN_CLK 3 /* clock pin or SCK */
#define NIXIE_PIN_LATCH 4 /* latch pin or RCK */
 
#define GRAPH_PIN_DATA 5 /* data line or SER */
#define GRAPH_PIN_CLK 6 /* clock pin or SCK */
#define GRAPH_PIN_LATCH 7 /* latch pin or RCK */
 
enum {
  MSG_TYPE_DEMO,
  MSG_TYPE_MESSAGE,
  MSG_TYPE_CLEAR,
  MSG_TYPE_GRAPH
}
 
enum {
  SERIAL_READ_OK,
  SERIAL_READ_ERR_FORMAT,
  SERIAL_READ_ERR_EMPTY
}
 
#endif