Commit fdb1b2ceae2646aebed2fcb52a641256690df4d5

Authored by Calvin Bulla
1 parent 921d042d
Exists in master and in 1 other branch dev

It is miraculously working

app/res/devices/_dev_tty.usbmodem411.xml
1 1 <Settings>
2   - <Color>62, 100, 233, 255</Color>
  2 + <Color>62, 161, 51, 255</Color>
3 3 <Offset>0, 0</Offset>
4 4 <Intensity>2</Intensity>
5 5 </Settings>
... ...
app/src/inoChannel.cpp
... ... @@ -98,7 +98,8 @@ void inoChannel::setupArduino() throw(runtime_error) {
98 98  
99 99 void inoChannel::handleInput() throw(runtime_error) {
100 100 if(serial.available() < 1) return;
101   - switch(serial.readByte()) {
  101 + char b;
  102 + switch(b = serial.readByte()) {
102 103 case HW_NEW_FRAME:
103 104 {
104 105 while(serial.available() < 256) {
... ... @@ -117,10 +118,14 @@ void inoChannel::handleInput() throw(runtime_error) {
117 118 printf("Looping %lu\n", time(NULL));
118 119 break;
119 120 case 75:
120   - printf("Im with stupid\n");
  121 + printf("LCD Frame\n");
  122 + break;
  123 + case 76:
  124 + printf("Row pos %d\n", serial.readByte());
121 125 break;
122 126 default:
123   - throw runtime_error("Unrecognized Message");
  127 + printf("Ups %d\n", b);
  128 + throw runtime_error("Upsnrecognized Message");
124 129 break;
125 130 }
126 131 }
... ... @@ -131,6 +136,11 @@ void inoChannel::handleOutput() throw(runtime_error) {
131 136 inoCommand cmd = commands.front();
132 137 if(serial.writeBytes(cmd.data(), cmd.size())) {
133 138 printf("[%d] %lu bytes\n", cmd[0], cmd.size());
  139 + printf(">");
  140 + for(unsigned char c: cmd) {
  141 + printf(" %d", c);
  142 + }
  143 + printf("\n");
134 144 commands.pop_front();
135 145 } else {
136 146 throw runtime_error("Unable to write");
... ... @@ -151,7 +161,8 @@ void inoChannel::threadedFunction() {
151 161 // It should be restarted
152 162 changeStatus("Error");
153 163 cerr << "Arduino [" << device << "]: " << ex.what() << endl;
154   - }
  164 + }
  165 + commands.empty();
155 166 }
156 167  
157 168 void inoChannel::getFrame(unsigned char *frame) {
... ...
app/src/inoControl.cpp
... ... @@ -68,7 +68,7 @@ void inoControl::toggleConnection(bool &amp; isSet) {
68 68 if(statusStr == "Setup" || statusStr == "Error") {
69 69 channel.setup();
70 70 channel.identify();
71   - channel.setTime();
  71 + //channel.setTime();
72 72 }
73 73 //channel.startStream();
74 74 } else {
... ...
ino/lib/LCD/LCD.cpp
... ... @@ -3,8 +3,13 @@
3 3 LCD::LCD() {
4 4 lcd_addr = LCD_DEFAULT_ADDRESS >> 1;
5 5 rowpos = 0;
  6 + set_timeout(1);
6 7 for(uint8_t i = 0; i < LCD_HEIGHT; ++i) {
7 8 for(uint8_t j = 0; j < LCD_ROW_SIZE; ++j) {
  9 + // TODO Only for debugging purposes
  10 + // if((i+j)%2) {
  11 + // rows[i][j] = '*';
  12 + // } else
8 13 rows[i][j] = ' ';
9 14 }
10 15 }
... ... @@ -123,9 +128,52 @@ void LCD::set_row(uint8_t idx, const char *str, uint8_t len, uint8_t pos) {
123 128 memcpy(&rows[idx][pos], str, len);
124 129 }
125 130  
  131 +void LCD::set_timeout(uint8_t ticks) {
  132 + tick_ovf = ticks;
  133 + // Force next update
  134 + tick_cnt = ticks;
  135 +}
  136 +
  137 +/*
126 138 bool LCD::tick() {
  139 + // Serial.write(76);
  140 + // Serial.write((unsigned char)rowpos);
  141 + print_frame();
  142 + if(++rowpos == LCD_ROW_SIZE) {
  143 + rowpos = 0;
  144 + return true;
  145 + }
  146 + return false;
  147 +}
  148 +
  149 +void LCD::print_frame() {
  150 + for(uint8_t i = 0; i < LCD_HEIGHT; ++i) {
  151 + set_cursor(1, i+1);
  152 + Wire.beginTransmission(lcd_addr);
  153 + Wire.write(LCD_COMMAND);
  154 + uint8_t towrite = LCD_ROW_SIZE - rowpos;
  155 + if(towrite < LCD_WIDTH) {
  156 + // Leftovers
  157 + Wire.write(&rows[i][rowpos], towrite);
  158 + Wire.write(rows[i], LCD_WIDTH - towrite);
  159 + } else {
  160 + // No leftovers
  161 + Wire.write(&rows[i][rowpos], LCD_WIDTH);
  162 + }
  163 + Wire.endTransmission();
  164 + }
  165 +}*/
  166 +
  167 +bool LCD::tick() {
  168 + // Check Timeout
  169 + if(++tick_cnt < tick_ovf) {
  170 + return false;
  171 + }
  172 + tick_cnt = 0;
  173 +
127 174 clear_screen();
128 175 print_frame();
  176 +
129 177 if(++rowpos == LCD_ROW_SIZE) {
130 178 rowpos = 0;
131 179 return true;
... ...
ino/lib/LCD/LCD.h
... ... @@ -75,11 +75,15 @@ class LCD {
75 75 void print_frame();
76 76 void set_row(uint8_t idx, const char *str, uint8_t len, uint8_t pos = 0);
77 77 void clean_row(uint8_t idx);
  78 + void set_timeout(uint8_t ticks);
78 79 // true if frame finishes
79 80 bool tick();
80 81 private:
81 82 char rows[LCD_HEIGHT][LCD_ROW_SIZE];
82 83 uint8_t rowpos;
  84 +
  85 + uint8_t tick_cnt;
  86 + uint8_t tick_ovf;
83 87  
84 88 uint8_t lcd_addr;
85 89  
... ...
ino/src/sketch.ino
... ... @@ -4,13 +4,15 @@
4 4  
5 5 #define LEAP_YEAR(Y) (((1970+Y)>0) && !((1970+Y)%4) && (((1970+Y)%100) || !((1970+Y)%400)))
6 6 #define ONE_MS_TCKS 250
  7 +#define ONE_SECOND_TCKS 15625
7 8 #define SIZE 256
8 9  
9 10 // General stuff
10 11  
11 12 volatile uint32_t timestamp = 0;
12   -uint8_t last_second = 0;
  13 +uint32_t last_second = 0;
13 14 Counter<int> ticks(1000);
  15 +// Counter<int> ticks(1);
14 16  
15 17 // LCD stuff
16 18  
... ... @@ -18,6 +20,7 @@ LCD lcd;
18 20 char ino_alias[LCD_ROW_SIZE];
19 21 volatile bool lcd_step = 0;
20 22 Counter<int> lcd_ticks(500);
  23 +// Counter<int> lcd_ticks(1);
21 24  
22 25 // ADC stuff
23 26  
... ... @@ -62,6 +65,16 @@ ISR(TIMER2_COMPA_vect) {
62 65 }
63 66 }
64 67  
  68 +// ISR(TIMER1_COMPA_vect) {
  69 +// TCNT1 = 0; // we won't lose ticks
  70 +// if(ticks.step()) {
  71 +// timestamp++; // inc. 1 sec
  72 +// }
  73 +// if(lcd_ticks.step()) {
  74 +// lcd_step = true;
  75 +// }
  76 +// }
  77 +
65 78 // Procedures && functions
66 79  
67 80 void getTimeFormated(struct time_formated &tf) {
... ... @@ -133,11 +146,11 @@ void update_time() {
133 146 getTimeFormated(tf);
134 147  
135 148 uint8_t len = sprintf(buf,
136   - "%d/%d/%d %s%d:%s%d:%s%d",
137   - tf.day, tf.month, tf.year + 1970,
138   - ((tf.hours < 10)?"0":""), tf.hours,
139   - ((tf.minutes < 10)?"0":""), tf.minutes,
140   - ((tf.seconds < 10)?"0":""), tf.seconds);
  149 + "%d/%d/%d %s%d:%s%d:%s%d",
  150 + tf.day, tf.month, tf.year + 1970,
  151 + ((tf.hours < 10)?"0":""), tf.hours,
  152 + ((tf.minutes < 10)?"0":""), tf.minutes,
  153 + ((tf.seconds < 10)?"0":""), tf.seconds);
141 154  
142 155  
143 156 lcd.set_row(1, buf, len, 4);
... ... @@ -153,15 +166,15 @@ void hardware_setup() {
153 166 ADCSRA |= (1<<ADSC);
154 167  
155 168 // Timer 1
156   -/*
157   - TCCR1A = 0;
158   - TCCR1B = 0; // stop
159   - TCNT1 = 0;
160   - TIFR1 = 0;
161   - OCR1A = ONE_SECOND_TCKS;
162   - TIMSK1 = (1 << OCIE1A) | (1 << TOIE1);
163   - TCCR1B = (1 << CS12) | (1 << CS10);
164   -*/
  169 +
  170 + // TCCR1A = 0;
  171 + // TCCR1B = 0; // stop
  172 + // TCNT1 = 0;
  173 + // TIFR1 = 0;
  174 + // OCR1A = ONE_SECOND_TCKS;
  175 + // TIMSK1 = (1 << OCIE1A) | (1 << TOIE1);
  176 + // TCCR1B = (1 << CS12) | (1 << CS10);
  177 +
165 178 // Timer 2
166 179 // Used for Timing/Unix Time
167 180  
... ... @@ -194,87 +207,87 @@ void setup() {
194 207 Serial.write(HW_INO_VERSION_MINOR);
195 208 }
196 209  
  210 +void handleComm() {
  211 + switch (Serial.read()) {
  212 + case HW_HELO:
  213 + {
  214 + // TODO Buffer overflow
  215 + char toread = Serial.read();
  216 + while (Serial.available() < toread);
  217 + Serial.readBytes(ino_alias, toread);
  218 + lcd.clean_row(0);
  219 + lcd.set_row(0, ino_alias, toread);
  220 + ino_alias[toread] = 0;
  221 + break;
  222 + }
  223 + case HW_SMPL_RATE: {
  224 + // change register
  225 + break;
  226 + }
  227 + case HW_DTIME_GET:
  228 + {
  229 + //TODO Not used
  230 + // Serial.write(HW_ACK);
  231 + // Serial.write(&timestamp, sizeof(uint32_t));
  232 +
  233 + break;
  234 + }
  235 + case HW_DTIME_SET:
  236 + {
  237 + while (Serial.available() < HW_DATE_SIZE);
  238 + uint32_t unix_time = 0;
  239 + Serial.readBytes((char*)&unix_time, 4);
  240 + timestamp = unix_time;
  241 + break;
  242 + }
  243 + case HW_SLEEP:
  244 + {
  245 + SMCR = 0; // (mode) low power reduction
  246 + SMCR |= (1 << SE);
  247 + asm volatile("sleep");
  248 +
  249 + // ZzZzZ
  250 +
  251 + SMCR = 0;
  252 +
  253 + break;
  254 + }
  255 + case HW_START_STREAM:
  256 + {
  257 + transfer_active = true;
  258 + break;
  259 + }
  260 + case HW_STOP_STREAM:
  261 + {
  262 + transfer_active = false;
  263 + break;
  264 + }
  265 + default:
  266 + {
  267 +
  268 + // This should never happen, command ignored
  269 + char *errmsg = "Error!!! Error!!! ";
  270 + lcd.set_row(0, errmsg, 32);
  271 + break;
  272 +
  273 + // This usually happens when we close serial port
  274 + // Reset Arduino
  275 + // TODO Add clean exit command
  276 + //asm volatile("jmp 0");
  277 + }
  278 + }
  279 +}
  280 +
197 281 void loop() {
198   - Serial.write(74);
199 282 process_frame();
200 283  
201   - if(lcd_step) {
  284 + if (lcd_step) {
202 285 update_time();
203   - if(lcd.tick()) {
204   - Serial.write(75);
205   - }
  286 + lcd.tick();
206 287 lcd_step = false;
207 288 }
208 289  
209 290 if (Serial.available() >= 1) {
210   -
211   - switch (Serial.read()) {
212   - case HW_HELO:
213   - {
214   - // TODO Buffer overflow
215   - char toread = Serial.read();
216   - while(Serial.available() < toread);
217   - Serial.readBytes(ino_alias, toread);
218   - lcd.clean_row(0);
219   - lcd.set_row(0, ino_alias, toread);
220   - ino_alias[toread] = 0;
221   - break;
222   - }
223   - case HW_SMPL_RATE: {
224   - // change register
225   - break;
226   - }
227   - case HW_DTIME_GET:
228   - {
229   - //TODO Not used
230   - // Serial.write(HW_ACK);
231   - // Serial.write(&timestamp, sizeof(uint32_t));
232   -
233   - break;
234   - }
235   - case HW_DTIME_SET:
236   - {
237   - while (Serial.available() < HW_DATE_SIZE);
238   - uint32_t unix_time = 0;
239   - Serial.readBytes((char*)&unix_time, 4);
240   - timestamp = unix_time;
241   - break;
242   - }
243   - case HW_SLEEP:
244   - {
245   - SMCR = 0; // (mode) low power reduction
246   - SMCR |= (1 << SE);
247   - asm volatile("sleep");
248   -
249   - // ZzZzZ
250   -
251   - SMCR = 0;
252   -
253   - break;
254   - }
255   - case HW_START_STREAM:
256   - {
257   - transfer_active = true;
258   - break;
259   - }
260   - case HW_STOP_STREAM:
261   - {
262   - transfer_active = false;
263   - break;
264   - }
265   - default:
266   - {
267   -
268   - // This should never happen, command ignored
269   - char *errmsg = "Error!!! Error!!! ";
270   - lcd.set_row(0, errmsg, 32);
271   - break;
272   -
273   - // This usually happens when we close serial port
274   - // Reset Arduino
275   - // TODO Add clean exit command
276   - //asm volatile("jmp 0");
277   - }
278   - }
  291 + handleComm();
279 292 }
280 293 }
... ...