Commit 497fec6ffeb06fe04c4d09e184eb8fa18798a3ab

Authored by Calvin Bulla
1 parent 053265a4
Exists in master and in 1 other branch dev

Minor Tweaks and Bugfixes

app/res/devices/_dev_tty.usbmodem411.xml
1 1 <Settings>
2 2 <Color>62, 100, 233, 255</Color>
3   - <Offset>0.5, 0</Offset>
  3 + <Offset>0, 0</Offset>
4 4 </Settings>
... ...
app/src/inoChannel.cpp
... ... @@ -6,74 +6,83 @@ void inoChannel::setup(string device) {
6 6 }
7 7  
8 8 void inoChannel::exit() {
9   - // there is no memory to free (now)
  9 + serial.close();
10 10 }
11 11  
12 12 void inoChannel::identify() {
13   - string cmd;
14   -
15   - cmd.append(device);
16   - cmd.push_back(HW_HELO);
17   - cmd.push_back(device.size());
18   - commands.push(command);
  13 + inoCommand cmd;
  14 +
  15 + cmd.resize(17, 0);
  16 +
  17 + cmd[0] = HW_HELO;
  18 + for(int i = 0; i < 16 && i < device.size(); i++) {
  19 + cmd[i+1] = device[i];
  20 + }
  21 + commands.push(cmd);
19 22 }
20 23  
21 24 void inoChannel::startStream() {
22   - string cmd;
  25 + inoCommand cmd;
23 26 cmd.push_back(HW_START_STREAM);
24 27 commands.push(cmd);
25 28 }
26 29  
27 30 void inoChannel::stopStream() {
28   - string cmd;
  31 + inoCommand cmd;
29 32 cmd.push_back(HW_STOP_STREAM);
30 33 commands.push(cmd);
31 34 }
32 35  
33   -void inoChannel::checkSystem() {
  36 +bool inoChannel::checkSystem() {
34 37 while(isThreadRunning() && !(serial.available() >= 4));
35 38 if(serial.available() >= 4) {
36   - char buf[4];
37   - serial.readBytes((unsigned char*)buf, 4);
  39 + unsigned char buf[4];
  40 + serial.readBytes(buf, 4);
38 41 if(buf[0] != 'H' || buf[1] != 'W') {
39 42 // Serial Port is invalid
40 43 string status("Invalid");
41 44 ofNotifyEvent(statusChange, status, this);
42   - serial.close();
43   - return;
  45 + return false;
44 46 }
45 47 string status("Connected");
46 48 ofNotifyEvent(statusChange, status, this);
  49 + return true;
47 50 }
  51 + return false;
48 52 }
49 53  
50 54 void inoChannel::threadedFunction() {
51 55 serial.setup(device, 115200);
52 56 serial.flush();
53   - checkSystem();
  57 + if(!checkSystem()) {
  58 + ofLogError() << "Invalid System";
  59 + }
  60 + identify();
54 61 while(isThreadRunning()) {
55   - while(serial.available() < 1);
56   - switch(serial.read()) {
57   - case HW_NEW_FRAME:
58   - {
59   - while(serial.avilable() < 256);
60   -
61   - // serial.readBytes(buffer, 256);
62   -
63   - break;
  62 + if(serial.available() >= 1) {
  63 + switch(serial.readByte()) {
  64 + case HW_NEW_FRAME:
  65 + {
  66 + while(serial.available() < 256) {
  67 + if(!isThreadRunning()) return;
  68 + }
  69 +
  70 + // serial.readBytes(buffer, 256);
  71 +
  72 + break;
  73 + }
  74 + default:
  75 + // no idea what could go wrong
  76 + break;
64 77 }
65   - default:
66   - // no idea what could go wrong
67   - break;
68 78 }
69 79 if(commands.size() > 1) {
70   - string cmd = commands.front();
71   - if(serial.writeBytes(cmd.c_str(), cmd.size())) {
  80 + inoCommand cmd = commands.front();
  81 + if(serial.writeBytes(cmd.data(), cmd.size())) {
72 82 commands.pop();
  83 + } else {
  84 + ofLogError() << "Unable to send command";
73 85 }
74 86 }
75 87 }
76   - string status("Closed");
77   - ofNotifyEvent(statusChange, status, this);
78   - serial.close();
79 88 }
... ...
app/src/inoChannel.h
... ... @@ -2,6 +2,8 @@
2 2  
3 3 #include "ofMain.h"
4 4  
  5 +typedef vector<unsigned char> inoCommand;
  6 +
5 7 class inoChannel : public ofThread {
6 8 public:
7 9 void setup(string device);
... ... @@ -12,12 +14,12 @@ public:
12 14 void startStream();
13 15 void stopStream();
14 16  
15   - void checkSystem();
  17 + bool checkSystem();
16 18  
17 19 ofEvent<string> statusChange;
18 20  
19 21 private:
20 22 ofSerial serial;
21 23 string device;
22   - queue<string> commands;
  24 + queue<inoCommand> commands;
23 25 };
... ...
app/src/inoControl.cpp
... ... @@ -7,7 +7,7 @@ inoControl::inoControl(string device) {
7 7 connect.addListener(this, &inoControl::toggleConnection);
8 8 reset.addListener(this, &inoControl::resetSettings);
9 9 gui.setup(device);
10   - gui.add(status.setup("Status", "Setup..."));
  10 + gui.add(status.setup("Status", "Setup"));
11 11 gui.add(connect.setup("Connect", false));
12 12 gui.add(hide.setup("Hide", false));
13 13 gui.add(settings.setup("Settings"));
... ... @@ -26,7 +26,6 @@ inoControl::inoControl(string device) {
26 26 ofFile::copyFromTo(inoControl::defaultSettings, filePath);
27 27 }
28 28 settings.loadFromFile(filePath);
29   - status = "Idle";
30 29  
31 30 buffer = new unsigned char[BUFFER_SIZE];
32 31  
... ... @@ -48,11 +47,16 @@ ofxGuiGroup * inoControl::getGui() {
48 47 return &gui;
49 48 }
50 49  
51   -void inoControl::toggleConnection(bool & status) {
52   - if(status) {
53   - channel.startThread();
  50 +void inoControl::toggleConnection(bool & isSet) {
  51 + string statusStr = status;
  52 + cout << "Current Status: " << statusStr << endl;
  53 + if(isSet) {
  54 + if(statusStr == "Setup") {
  55 + channel.startThread();
  56 + }
  57 + channel.startStream();
54 58 } else {
55   - channel.stopThread();
  59 + channel.stopStream();
56 60 }
57 61 }
58 62  
... ...
app/src/inoUtils.cpp
... ... @@ -3,15 +3,19 @@
3 3 #include "ofMain.h"
4 4  
5 5 #if defined(__linux__)
6   -const std::string patterns[] = {
  6 +const string patterns[] = {
7 7 "/dev/ttyACM",
8 8 "/dev/ttyUSB"
9 9 };
10 10 #elif defined(__APPLE__)
11   -const std::string patterns[] = {
  11 +const string patterns[] = {
12 12 "/dev/tty.usbmodem",
13 13 "/dev/tty.usbserial"
14 14 };
  15 +#else
  16 +const string patterns[] = {
  17 + "COM"
  18 +};
15 19 #endif
16 20  
17 21 bool isArduinoPort(const string & path) {
... ...
ino/lib/heisenwave/heisenwave.h
... ... @@ -4,18 +4,21 @@
4 4 #define HW_INO_VERSION_MINOR 5
5 5 #define HW_INO_VERSION_MAJOR 0
6 6  
7   -// COMMANDS
8   -
  7 +// TOKENS
9 8 #define HW_ACK 1
10 9 #define HW_NACK 2
11   -#define HW_HELO 3
12   -#define HW_SMPL_RATE 4
13   -#define HW_GET_DATA 5
14   -#define HW_DTIME_SET 6
15   -#define HW_DTIME_GET 7
16   -#define HW_SLEEP 8
17   -#define HW_STOP_DATA 9
18   -#define HW_NEW_FRAME 10
  10 +
  11 +// COMMANDS
  12 +#define HW_HELO 1
  13 +#define HW_START_STREAM 2
  14 +#define HW_STOP_STREAM 3
  15 +#define HW_SMPL_RATE 4
  16 +#define HW_GET_DATA 5
  17 +#define HW_DTIME_SET 6
  18 +#define HW_DTIME_GET 7
  19 +#define HW_SLEEP 8
  20 +#define HW_STOP_DATA 9
  21 +#define HW_NEW_FRAME 10
19 22  
20 23 // COMMANDS SIZE
21 24  
... ...
ino/src/sketch.ino
... ... @@ -19,174 +19,174 @@ volatile uint8_t *buffer = frame_1;
19 19 volatile uint8_t *pc_buffer = frame_1;
20 20 volatile uint8_t index = 0;
21 21 volatile uint8_t pc_index = 0;
22   -volatile boolean transfer_active = false;
23   -volatile boolean transfer_ready = false;
  22 +volatile bool transfer_active = false;
  23 +volatile bool transfer_ready = false;
24 24  
25 25 // ISR routines
26 26  
27 27 ISR(ADC_vect) {
28   - buffer[index++] = ADCH;
29   -
30   - if (index == 0) {
31   - buffer = (buffer == frame_1)? frame_2 : frame_1;
32   - pc_buffer = (buffer == frame_1)? frame_2 : frame_1;
33   -
34   - // will this double buffer system be enough?...
35   - // if transfer = true => not enough!...
36   - // we could disable interrupts while send_data...?
37   -
38   - transfer_ready = true;
39   - }
  28 + buffer[index++] = ADCH;
  29 +
  30 + if (index == 0) {
  31 + buffer = (buffer == frame_1)? frame_2 : frame_1;
  32 + pc_buffer = (buffer == frame_1)? frame_2 : frame_1;
  33 +
  34 + // will this double buffer system be enough?...
  35 + // if transfer = true => not enough!...
  36 + // we could disable interrupts while send_data...?
  37 +
  38 + transfer_ready = true;
  39 + }
40 40 }
41 41  
42 42 // Procedures && functions
43 43  
44 44 void send_data() {
45   - if (transfer_active && transfer_ready) {
46   - Serial.write(HW_NEW_FRAME);
47   -
48   - for (int i = 0; i < SIZE; i++) {
49   - Serial.write(pc_buffer[i]);
50   - }
51   -
52   - transfer_ready = false;
  45 + if (transfer_active && transfer_ready) {
  46 + Serial.write(HW_NEW_FRAME);
  47 +
  48 + for (int i = 0; i < SIZE; i++) {
  49 + Serial.write(pc_buffer[i]);
53 50 }
  51 +
  52 + transfer_ready = false;
  53 + }
54 54 }
55 55  
56 56 void print_time_lcd() {
57   - bool alias_turn;
58   - char buffer[4];
59   - lcd.clear_screen();
60   -
61   - if (++lcd_swap >= 10) {
62   - lcd_swap = 0;
63   - }
64   -
65   - if (lcd_swap < 5) {
66   - lcd.print(ino_alias);
67   - } else {
68   - lcd.set_cursor(3,1);
69   - lcd.print(dayShortStr(day()));
70   - lcd.print("/");
71   - lcd.print(monthShortStr(month()));
72   - lcd.print("/");
73   - sprintf(buffer, "%d", year());
74   - lcd.print(buffer);
75   - }
76   -
77   - lcd.set_cursor(5,2);
78   -
79   - sprintf(buffer, "%d", hour());
80   - if (strlen(buffer) < 2) lcd.print("0");
81   - lcd.print(buffer);lcd.print(":");
82   - sprintf(buffer, "%d", minute());
83   - if (strlen(buffer) < 2) lcd.print("0");
84   - lcd.print(buffer);lcd.print(":");
85   - sprintf(buffer, "%d", second());
86   - if (strlen(buffer) < 2) lcd.print("0");
  57 + bool alias_turn;
  58 + char buffer[4];
  59 + lcd.clear_screen();
  60 +
  61 + if (++lcd_swap >= 10) {
  62 + lcd_swap = 0;
  63 + }
  64 +
  65 + if (lcd_swap < 5) {
  66 + lcd.print(ino_alias);
  67 + } else {
  68 + lcd.set_cursor(3,1);
  69 + lcd.print(dayShortStr(day()));
  70 + lcd.print("/");
  71 + lcd.print(monthShortStr(month()));
  72 + lcd.print("/");
  73 + sprintf(buffer, "%d", year());
87 74 lcd.print(buffer);
  75 + }
  76 +
  77 + lcd.set_cursor(5,2);
  78 +
  79 + sprintf(buffer, "%d", hour());
  80 + if (strlen(buffer) < 2) lcd.print("0");
  81 + lcd.print(buffer);lcd.print(":");
  82 + sprintf(buffer, "%d", minute());
  83 + if (strlen(buffer) < 2) lcd.print("0");
  84 + lcd.print(buffer);lcd.print(":");
  85 + sprintf(buffer, "%d", second());
  86 + if (strlen(buffer) < 2) lcd.print("0");
  87 + lcd.print(buffer);
88 88 }
89 89  
90 90 void hardware_setup() {
91 91 // Max Prescaler
92   - ADCSRA |= (0<<ADPS2) | (0<<ADPS1) | (1<<ADPS0) | (1<<ADATE) | (1<<ADEN) | (1<<ADIE);
93   - ADMUX |= (1<<REFS0) | (1<<ADLAR);
94   - ADMUX &= ~(1<<REFS1);
95   - ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) | (1<<ADTS0));
96   - ADCSRA |= (1<<ADSC);
  92 + ADCSRA |= (0<<ADPS2) | (0<<ADPS1) | (1<<ADPS0) | (1<<ADATE) | (1<<ADEN) | (1<<ADIE);
  93 + ADMUX |= (1<<REFS0) | (1<<ADLAR);
  94 + ADMUX &= ~(1<<REFS1);
  95 + ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) | (1<<ADTS0));
  96 + ADCSRA |= (1<<ADSC);
97 97 }
98 98  
99 99 // Main
100 100  
101 101 void setup() {
102   - hardware_setup();
103   - Serial.begin(115200);
104   -
105   - lcd.clear_screen();
106   - lcd.backlight(true);
107   -
108   - Serial.write(HW_ACK);
109   - Serial.write("HW");
110   - Serial.write(HW_INO_VERSION_MAJOR);
111   - Serial.write(HW_INO_VERSION_MINOR);
  102 + hardware_setup();
  103 + Serial.begin(115200);
  104 +
  105 + lcd.clear_screen();
  106 + lcd.backlight(true);
  107 +
  108 + //Serial.write(HW_ACK);
  109 + Serial.write("HW");
  110 + Serial.write(HW_INO_VERSION_MAJOR);
  111 + Serial.write(HW_INO_VERSION_MINOR);
112 112 }
113 113  
114 114 void loop() {
115   - send_data();
116   -
117   - uint8_t current;
118   - if (current = second(), last_second != current) {
119   - print_time_lcd();
120   - last_second = current;
121   - }
  115 + send_data();
  116 +
  117 + uint8_t current;
  118 + if (current = second(), last_second != current) {
  119 + print_time_lcd();
  120 + last_second = current;
  121 + }
  122 +
  123 + if (Serial.available() < 1) {
  124 + switch (Serial.read()) {
  125 + case HW_HELO:
  126 + {
  127 + while (Serial.available() < HW_HELO_SIZE);
  128 +
  129 + for (byte i = 0; i < HW_HELO_SIZE; i++) {
  130 + ino_alias[i] = Serial.read();
  131 + }
  132 +
  133 + Serial.write(HW_ACK);
  134 + }
  135 + case HW_SMPL_RATE:
  136 + {
  137 + // change register
  138 +
  139 + break;
  140 + }
  141 + case HW_DTIME_GET:
  142 + {
  143 + Serial.write(HW_ACK);
  144 + Serial.write((byte *) now(), sizeof(uint32_t));
  145 +
  146 + break;
  147 + }
  148 + case HW_DTIME_SET:
  149 + {
  150 + while (Serial.available() < HW_DATE_SIZE);
  151 +
  152 + uint32_t unix_time = 0;
  153 + for (uint8_t i = HW_DATE_SIZE-1; i >= 0; i--) {
  154 + unix_time += Serial.read() << (8*i);
  155 + }
  156 + setTime(unix_time);
  157 +
  158 + Serial.write(HW_ACK);
  159 +
  160 + break;
  161 + }
  162 + case HW_SLEEP:
  163 + {
  164 + SMCR = 0; // (mode) low power reduction
  165 + SMCR |= (1 << SE);
  166 + asm volatile("sleep");
  167 +
  168 + // ZzZzZ
  169 +
  170 + SMCR = 0;
  171 +
  172 + break;
  173 + }
  174 + case HW_GET_DATA:
  175 + {
  176 + transfer_active = true;
  177 +
  178 + break;
  179 + }
  180 + case HW_STOP_DATA:
  181 + {
  182 + // Stop sending chunks?
  183 + transfer_active = false;
122 184  
123   - if (Serial.available() < 1) {
124   - switch (Serial.read()) {
125   - case HW_HELO:
126   - {
127   - while (Serial.available() < HW_HELO_SIZE);
128   -
129   - for (byte i = 0; i < HW_HELO_SIZE; i++) {
130   - ino_alias[i] = Serial.read();
131   - }
132   -
133   - Serial.write(HW_ACK);
134   - }
135   - case HW_SMPL_RATE:
136   - {
137   - // change register
138   -
139   - break;
140   - }
141   - case HW_DTIME_GET:
142   - {
143   - Serial.write(HW_ACK);
144   - Serial.write((byte *) now(), sizeof(uint32_t));
145   -
146   - break;
147   - }
148   - case HW_DTIME_SET:
149   - {
150   - while (Serial.available() < HW_DATE_SIZE);
151   -
152   - uint32_t unix_time = 0;
153   - for (uint8_t i = HW_DATE_SIZE-1; i >= 0; i--) {
154   - unix_time += Serial.read() << (8*i);
155   - }
156   - setTime(unix_time);
157   -
158   - Serial.write(HW_ACK);
159   -
160   - break;
161   - }
162   - case HW_SLEEP:
163   - {
164   - SMCR = 0; // (mode) low power reduction
165   - SMCR |= (1 << SE);
166   - asm volatile("sleep");
167   -
168   - // ZzZzZ
169   -
170   - SMCR = 0;
171   -
172   - break;
173   - }
174   - case HW_GET_DATA:
175   - {
176   - transfer_active = true;
177   -
178   - break;
179   - }
180   - case HW_STOP_DATA:
181   - {
182   - // Stop sending chunks?
183   - transfer_active = false;
184   -
185   - break;
186   - }
187   - default:
188   - // This should never happen, command ignored
189   - break;
  185 + break;
190 186 }
  187 + default:
  188 + // This should never happen, command ignored
  189 + break;
191 190 }
  191 + }
192 192 }
... ...