Commit 9ccdf30b83fe0b06f4c7dcd01eb89c83c27949c7

Authored by Calvin Bulla
1 parent 23d072ff
Exists in master and in 1 other branch dev

Working Commit

app/res/devices/_dev_tty.usbmodem411.xml
1 1 <Settings>
2   - <Color>62, 161, 51, 255</Color>
3   - <Offset>0, 0</Offset>
4   -<Intensity>2</Intensity>
  2 + <Color>62, 100, 233, 255</Color>
  3 + <Position>0, 0</Position>
  4 + <Intensity>1</Intensity>
5 5 </Settings>
... ...
app/res/devices/_dev_tty.usbmodem641.xml
... ... @@ -2,4 +2,5 @@
2 2 <Color>164, 100, 110, 255</Color>
3 3 <Offset>0, 0</Offset>
4 4 <Intensity>2</Intensity>
  5 +<Position>0.0225161, 0.181888</Position>
5 6 </Settings>
... ...
app/res/settings-dev.xml
1 1 <Settings>
2 2 <Color>62, 100, 233, 255</Color>
3   - <Offset>0, 0</Offset>
  3 + <Position>0, 0</Position>
4 4 <Intensity>1</Intensity>
5 5 </Settings>
... ...
app/res/settings.xml
1 1 <group>
2 2 <General>
3   - <Zoom>2.37372</Zoom>
  3 + <Zoom>1, 1</Zoom>
4 4 </General>
5   - <_dev_tty.usbmodem641>
  5 + <_dev_tty.usbmodem411>
6 6 <Status>Setup</Status>
7 7 <Connect>0</Connect>
8 8 <Hide>0</Hide>
  9 + <Sample__kHz_>1</Sample__kHz_>
9 10 <Settings>
10 11 <Color>62, 100, 233, 255</Color>
11   - <Intensity>2</Intensity>
12   - <Offset>0, 0</Offset>
  12 + <Intensity>1</Intensity>
  13 + <Position>0, 0</Position>
13 14 </Settings>
14   - </_dev_tty.usbmodem641>
  15 + </_dev_tty.usbmodem411>
15 16 </group>
... ...
app/src/heisenwave.h
... ... @@ -13,6 +13,7 @@
13 13 #define HW_SLEEP 8
14 14 #define HW_STOP_DATA 9
15 15 #define HW_NEW_FRAME 10
  16 +#define HW_SAVE_CFG 11
16 17  
17 18 // COMMANDS SIZE
18 19 #define HW_DATE_SIZE 4
... ...
app/src/inoChannel.cpp
... ... @@ -115,6 +115,12 @@ void inoChannel::setupArduino() throw(runtime_error) {
115 115 }
116 116 }
117 117  
  118 +void inoChannel::saveConfig() {
  119 + inoCommand cmd;
  120 + cmd.push_back(HW_SAVE_CFG);
  121 + addCommand(cmd);
  122 +}
  123 +
118 124 void inoChannel::handleInput() throw(runtime_error) {
119 125 if(serial.available() < 1) return;
120 126 char b;
... ... @@ -126,25 +132,13 @@ void inoChannel::handleInput() throw(runtime_error) {
126 132 }
127 133 frameMtx.lock();
128 134 serial.readBytes(buffer, 256);
129   - for(int i = 0; i < 256; ++i) {
130   - // printf("%d => %d\n", i, buffer[i]);
131   - }
132 135 frameMtx.unlock();
133 136 ofNotifyEvent(receiveFrame, this);
134 137 break;
135 138 }
136   - case 74:
137   - printf("Looping %lu\n", time(NULL));
138   - break;
139   - case 75:
140   - printf("LCD Frame\n");
141   - break;
142   - case 76:
143   - printf("Row pos %d\n", serial.readByte());
144   - break;
145 139 default:
146   - printf("Ups %d\n", b);
147   - throw runtime_error("Upsnrecognized Message");
  140 + printf("Received %d\n", b);
  141 + throw runtime_error("Unrecognized Message");
148 142 break;
149 143 }
150 144 }
... ... @@ -186,6 +180,7 @@ void inoChannel::threadedFunction() {
186 180  
187 181 void inoChannel::getFrame(unsigned char *frame) {
188 182 frameMtx.lock();
189   - memcpy(frame, buffer, 256);
  183 + for(int i=0; i < 256; i++) frame[i] = buffer[i];
  184 + //memcpy(frame, buffer, 256); // not working despite being in a thread safe block
190 185 frameMtx.unlock();
191 186 }
... ...
app/src/inoChannel.h
... ... @@ -25,6 +25,7 @@ public:
25 25 void setTime();
26 26 void sleepArduino();
27 27 void setSampleRate(float new_rate);
  28 + void saveConfig();
28 29 void flush();
29 30  
30 31 bool isActive();
... ...
app/src/inoControl.cpp
... ... @@ -51,8 +51,8 @@ void inoControl::setupGui() {
51 51 ofColor(100, 100, 140),
52 52 ofColor(0, 0), ofColor(255, 255)));
53 53 settings.add(intensity.setup("Intensity", 2, 1, 4));
54   - settings.add(offset.setup("Offset", ofVec2f(0,0)
55   - , ofVec2f(-0.5,-0.5), ofVec2f(0.5,0.5)));
  54 + settings.add(position.setup("Position", ofVec2f(0,0)
  55 + , ofVec2f(-1,-1), ofVec2f(1,1)));
56 56 }
57 57  
58 58 void inoControl::setupSettings() {
... ... @@ -78,6 +78,7 @@ void inoControl::toggleConnection(bool &amp; isSet) {
78 78 channel.startStream();
79 79 } else {
80 80 channel.stopStream();
  81 + channel.saveConfig();
81 82 channel.sleepArduino();
82 83 }
83 84 }
... ... @@ -94,26 +95,31 @@ void inoControl::onStatusChanged(string &amp; str) {
94 95 }
95 96 }
96 97  
97   -void inoControl::drawWave(float zoom) {
  98 +void inoControl::drawWave(float zx, float zy) {
98 99 if(hide) return;
99 100  
100   - int width = ofGetViewportWidth();
101   - int height = ofGetViewportHeight();
  101 + double width = ofGetViewportWidth();
  102 + double height = ofGetViewportHeight();
102 103  
103 104 ofPushMatrix();
104 105  
105   - ofTranslate(offset->x * (float)width,
106   - offset->y * (float)width);
107   - ofScale(zoom, 1);
  106 + ofTranslate(
  107 + position->x * width,
  108 + position->y * height
  109 + );
  110 +
  111 + ofScale(zx, zy);
108 112  
109 113 ofSetColor(color);
110 114 ofSetPolyMode(OF_POLY_WINDING_ODD);
111 115 ofNoFill();
112 116 ofSetLineWidth(intensity);
113 117 ofBeginShape();
114   - for(int i = 0; i <= BUF_TOTAL; i++) {
115   - ofVertex((float)i/BUF_TOTAL * (float)width,
116   - (float)height - ((float)buffer[i] / 256 * (float)height));
  118 + for(int i = 0; i < BUF_TOTAL; i++) {
  119 + ofVertex(
  120 + (double)i/BUF_TOTAL * (double)width, // X
  121 + (1.0 - ((double)buffer[i])/256.0) * height // Y
  122 + );
117 123 }
118 124 ofEndShape(false);
119 125  
... ... @@ -125,9 +131,8 @@ void inoControl::resetSettings() {
125 131 }
126 132  
127 133 void inoControl::onReceiveFrame() {
128   - printf("[%lu] New Frame\n", time(NULL));
129   - // channel.getFrame(&buffer[bufferIdx]);
130   - // if(++bufferIdx == BUF_COUNT) {
131   - // bufferIdx = 0;
132   - // }
  134 + channel.getFrame(&buffer[bufferIdx*256]);
  135 + if(++bufferIdx >= BUF_COUNT) {
  136 + bufferIdx = 0;
  137 + }
133 138 }
... ...
app/src/inoControl.h
1 1 #pragma once
2 2  
3   -#define BUF_FRAME 256
4 3 #define BUF_COUNT 4
5   -#define BUF_TOTAL (BUF_FRAME * BUF_COUNT)
  4 +#define BUF_TOTAL (256 * BUF_COUNT)
6 5  
7 6 #include "ofMain.h"
8 7 #include "ofxGui.h"
... ... @@ -21,7 +20,7 @@ public:
21 20 void toggleConnection(bool & status);
22 21 void changeSampleRate(float & new_rate);
23 22  
24   - void drawWave(float zoom);
  23 + void drawWave(float zx, float zy);
25 24  
26 25 private:
27 26 static string defaultSettings;
... ... @@ -42,7 +41,7 @@ private:
42 41 ofxButton reset;
43 42 ofxColorSlider color;
44 43 ofxIntSlider intensity;
45   - ofxVec2Slider offset;
  44 + ofxVec2Slider position;
46 45  
47 46 inoChannel channel;
48 47 unsigned char *buffer;
... ...
app/src/ofApp.cpp
... ... @@ -13,7 +13,8 @@ void ofApp::setup(){
13 13  
14 14 general.setup("General");
15 15 general.add(discoverBtn.setup("Discover"));
16   - general.add(zoom.setup("Zoom", 1.0f, 0.25f, 4.0f));
  16 + general.add(zoom.setup("Zoom", ofVec2f(1,1)
  17 + , ofVec2f(0.5,0.5), ofVec2f(2,2)));
17 18 gui.add(&general);
18 19 discoverArduinos();
19 20 }
... ... @@ -30,7 +31,7 @@ void ofApp::update() {
30 31 void ofApp::draw() {
31 32 ofPushMatrix();
32 33 for(auto p : controls) {
33   - p.second->drawWave(zoom);
  34 + p.second->drawWave(zoom->x, zoom->y);
34 35 }
35 36 ofPopMatrix();
36 37 if(showGui) {
... ...
app/src/ofApp.h
... ... @@ -30,7 +30,7 @@ private:
30 30 ofxPanel gui;
31 31 ofxGuiGroup general;
32 32 ofxButton discoverBtn;
33   - ofxFloatSlider zoom;
  33 + ofxVec2Slider zoom;
34 34 bool showGui = true;
35 35 };
36 36  
... ...
ino/lib/heisenwave/heisenwave.h
... ... @@ -19,6 +19,7 @@
19 19 #define HW_SLEEP 8
20 20 #define HW_STOP_DATA 9
21 21 #define HW_NEW_FRAME 10
  22 +#define HW_SAVE_CFG 11
22 23  
23 24 // COMMANDS SIZE
24 25  
... ...
ino/src/sketch.ino
1 1 #include <heisenwave.h>
2 2 #include <counter.h>
3 3 #include <LCD.h>
  4 +#include <EEPROM.h>
4 5  
5 6 #define LEAP_YEAR(Y) (((1970+Y)>0) && !((1970+Y)%4) && (((1970+Y)%100) || !((1970+Y)%400)))
6 7 #define ONE_MS_TCKS 250
... ... @@ -12,7 +13,6 @@
12 13  
13 14 // General stuff
14 15  
15   -volatile uint32_t timestamp = 0;
16 16 uint32_t last_second = 0;
17 17 Counter<int> ticks(1000);
18 18 // Counter<int> ticks(1);
... ... @@ -36,16 +36,20 @@ volatile uint8_t pc_index = 0;
36 36 volatile bool transfer_active = false;
37 37 volatile bool transfer_ready = false;
38 38  
  39 +struct config {
  40 + volatile uint32_t timestamp;
  41 + uint16_t sample_rate;
  42 +} config;
  43 +
39 44 struct time_formated {
40 45 uint8_t seconds, minutes, hours, day, month, year;
41 46 };
42 47  
43   -uint16_t sample_rate;
44   -#define T1_MS 0.016
  48 +#define T1_SECONDS 0.000016
45 49  
46 50 void set_sample_rate(uint16_t new_rate) {
47   - sample_rate = new_rate; // Hz
48   - int cnt = round((1.0 / (double)new_rate)/T1_MS);
  51 + config.sample_rate = new_rate; // Hz
  52 + int cnt = round((1.0 / (double)new_rate)/ T1_SECONDS);
49 53 OCR1B = cnt;
50 54 OCR1A = cnt;
51 55 }
... ... @@ -68,13 +72,14 @@ ISR(ADC_vect) {
68 72 }
69 73  
70 74 ISR(TIMER1_COMPB_vect) {
  75 + // TCNT1 = 0;
71 76 }
72 77  
73 78 // Each ms
74 79 ISR(TIMER2_COMPA_vect) {
75 80 TCNT2 = 0; // we won't lose ticks
76 81 if(ticks.step()) {
77   - timestamp++; // inc. 1 sec
  82 + config.timestamp++; // inc. 1 sec
78 83 }
79 84 if(lcd_ticks.step()) {
80 85 lcd_step = true;
... ... @@ -98,7 +103,7 @@ void getTimeFormated(struct time_formated &amp;tf) {
98 103 unsigned long days = 0;
99 104 uint8_t monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
100 105  
101   - time = (uint32_t) timestamp;
  106 + time = (uint32_t) config.timestamp;
102 107 tf.seconds = time % 60;
103 108 time /= 60; // minutes
104 109 tf.minutes = time % 60;
... ... @@ -172,6 +177,7 @@ void hardware_setup() {
172 177 ADMUX |= (1<<REFS0) | (1<<ADLAR);
173 178 ADMUX &= ~(1<<REFS1);
174 179 ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) | (1<<ADTS0));
  180 + ADCSRB |= (1<<ADTS2) | (1<<ADTS0);
175 181 ADCSRA |= (1<<ADSC);
176 182  
177 183 // Timer 1
... ... @@ -196,13 +202,18 @@ void hardware_setup() {
196 202 TCCR2B = (1 << CS22);
197 203 }
198 204  
  205 +void load_config() {
  206 + uint8_t *buf = (uint8_t *) &config;
  207 + for(int i = 0; i < sizeof(struct config); ++i) {
  208 + buf[i] = EEPROM.read(i);
  209 + }
  210 +}
  211 +
199 212 // Main
200 213  
201 214 void setup() {
202 215 hardware_setup();
203 216  
204   - timestamp = 1431447151; // testing purposes
205   -
206 217 lcd.clear_screen();
207 218 lcd.backlight(true);
208 219 lcd.hide_cursor();
... ... @@ -210,6 +221,8 @@ void setup() {
210 221 char *wait = "Waiting Waiting";
211 222 lcd.set_row(0, wait, strlen(wait));
212 223  
  224 + load_config();
  225 +
213 226 Serial.begin(115200);
214 227 Serial.write("HW");
215 228 Serial.write(HW_INO_VERSION_MAJOR);
... ... @@ -244,7 +257,7 @@ void handleComm() {
244 257 {
245 258 //TODO Not used
246 259 // Serial.write(HW_ACK);
247   - // Serial.write(&timestamp, sizeof(uint32_t));
  260 + // Serial.write(&config.timestamp, sizeof(uint32_t));
248 261  
249 262 break;
250 263 }
... ... @@ -253,13 +266,14 @@ void handleComm() {
253 266 while (Serial.available() < HW_DATE_SIZE);
254 267 uint32_t unix_time = 0;
255 268 Serial.readBytes((char*)&unix_time, 4);
256   - timestamp = unix_time;
  269 + config.timestamp = unix_time;
257 270 break;
258 271 }
259 272 case HW_SLEEP:
260 273 {
261 274 DEBUG_16("SLEEP")
262   - SMCR = 0; // (mode) low power reduction
  275 + PRR |= (1<<PRTWI) | (1<<PRTIM2) | (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRADC);
  276 + SMCR = 0; // Idle
263 277 SMCR |= (1 << SE);
264 278 asm volatile("sleep");
265 279  
... ... @@ -267,6 +281,8 @@ void handleComm() {
267 281  
268 282 SMCR = 0;
269 283  
  284 + DEBUG_16("WAKEUP")
  285 +
270 286 break;
271 287 }
272 288 case HW_START_STREAM:
... ... @@ -279,6 +295,13 @@ void handleComm() {
279 295 transfer_active = false;
280 296 break;
281 297 }
  298 + case HW_SAVE_CFG:
  299 + {
  300 + uint8_t *buf = (uint8_t *) &config;
  301 + for(int i = 0; i < sizeof(struct config); ++i) {
  302 + EEPROM.write(i, buf[i]);
  303 + }
  304 + }
282 305 default:
283 306 {
284 307  
... ...