Commit 30194dfa119cac906220c1fdb95678674aab722b

Authored by Calvin Bulla
1 parent 0f808355
Exists in master and in 1 other branch dev

Minor Interface Changes

app/res/devices/_dev_tty.usbmodem641.xml 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +<Settings>
  2 + <Color>62, 100, 233, 255</Color>
  3 + <Offset>0, 0</Offset>
  4 + <Intensity>2</Intensity>
  5 +</Settings>
... ...
app/res/settings-dev.xml
1 1 <Settings>
2 2 <Color>62, 100, 233, 255</Color>
3 3 <Offset>0, 0</Offset>
  4 + <Intensity>1</Intensity>
4 5 </Settings>
... ...
app/res/settings.xml
1 1 <group>
2   - <_dev_tty.usbmodem411>
3   - <Status>Idle</Status>
  2 + <General>
  3 + <Zoom>2.37372</Zoom>
  4 + </General>
  5 + <_dev_tty.usbmodem641>
  6 + <Status>Setup</Status>
4 7 <Connect>0</Connect>
5 8 <Hide>0</Hide>
6 9 <Settings>
7   - <Color>62, 100, 233, 147</Color>
8   - <Offset>0.0271953, 0.197192</Offset>
  10 + <Color>62, 100, 233, 255</Color>
  11 + <Intensity>2</Intensity>
  12 + <Offset>0, 0</Offset>
9 13 </Settings>
10   - </_dev_tty.usbmodem411>
  14 + </_dev_tty.usbmodem641>
11 15 </group>
... ...
app/src/inoChannel.cpp
1 1 #include "inoChannel.h"
2 2 #include "heisenwave.h"
3 3  
4   -void inoChannel::setup(string device) {
5   - this->device = device;
  4 +inoChannel::inoChannel(const string & device) : device(device) {
  5 +}
  6 +
  7 +inoChannel::~inoChannel() { }
  8 +
  9 +void inoChannel::setup() {
6 10 }
7 11  
8 12 void inoChannel::exit() {
9 13 serial.close();
10 14 }
11 15  
  16 +void inoChannel::restart() {
  17 + exit();
  18 + setup();
  19 +}
  20 +
12 21 void inoChannel::addCommand(const inoCommand & cmd) {
13 22 commandsMtx.lock();
14 23 commands.push(cmd);
15 24 commandsMtx.unlock();
16 25 }
17 26  
  27 +void inoChannel::changeStatus(const string & str) {
  28 + string status(str);
  29 + ofNotifyEvent(statusChange, status, this);
  30 +}
  31 +
18 32 void inoChannel::identify() {
19 33 inoCommand cmd;
20 34  
... ... @@ -38,58 +52,64 @@ void inoChannel::stopStream() {
38 52 addCommand(cmd);
39 53 }
40 54  
41   -bool inoChannel::checkSystem() {
  55 +void inoChannel::waitArduino() throw(runtime_error) {
42 56 while(isThreadRunning() && !(serial.available() >= 4));
43 57 if(serial.available() >= 4) {
44 58 unsigned char buf[4];
45 59 serial.readBytes(buf, 4);
46 60 if(buf[0] != 'H' || buf[1] != 'W') {
47 61 // Serial Port is invalid
48   - string status("Invalid");
49   - ofNotifyEvent(statusChange, status, this);
50   - return false;
  62 + throw runtime_error("Invalid Port");
51 63 }
52   - string status("Connected");
53   - ofNotifyEvent(statusChange, status, this);
54   - return true;
  64 + changeStatus("Connected");
55 65 }
56   - return false;
57 66 }
58 67  
59   -void inoChannel::threadedFunction() {
60   - serial.setup(device, 115200);
61   - serial.flush();
62   - if(!checkSystem()) {
63   - ofLogError() << "Invalid System";
  68 +void inoChannel::handleInput() throw(runtime_error) {
  69 + if(serial.available() < 1) return;
  70 + switch(serial.readByte()) {
  71 + case HW_NEW_FRAME:
  72 + {
  73 + while(serial.available() < 256) {
  74 + if(!isThreadRunning()) return;
  75 + }
  76 + // serial.readBytes(buffer, 256);
  77 + break;
  78 + }
  79 + default:
  80 + throw runtime_error("Unrecognized Message");
  81 + break;
64 82 }
65   - identify();
66   - while(isThreadRunning()) {
67   - if(serial.available() >= 1) {
68   - switch(serial.readByte()) {
69   - case HW_NEW_FRAME:
70   - {
71   - while(serial.available() < 256) {
72   - if(!isThreadRunning()) return;
73   - }
74   -
75   - // serial.readBytes(buffer, 256);
  83 +}
76 84  
77   - break;
78   - }
79   - default:
80   - // no idea what could go wrong
81   - break;
82   - }
83   - }
84   - commandsMtx.lock();
85   - if(commands.size() > 1) {
86   - inoCommand cmd = commands.front();
87   - if(serial.writeBytes(cmd.data(), cmd.size())) {
88   - commands.pop();
89   - } else {
90   - ofLogError() << "Unable to send command";
91   - }
  85 +void inoChannel::handleOutput() throw(runtime_error) {
  86 + commandsMtx.lock();
  87 + if(commands.size() > 1) {
  88 + inoCommand cmd = commands.front();
  89 + if(serial.writeBytes(cmd.data(), cmd.size())) {
  90 + commands.pop();
  91 + } else {
  92 + throw runtime_error("Unable to write");
92 93 }
93   - commandsMtx.unlock();
94 94 }
  95 + commandsMtx.unlock();
  96 +}
  97 +
  98 +void inoChannel::threadedFunction() {
  99 + serial.setup(device, 115200);
  100 + serial.flush();
  101 + try {
  102 + waitArduino();
  103 + identify();
  104 + while(isThreadRunning()) {
  105 + handleInput();
  106 + handleOutput();
  107 + }
  108 + } catch(const exception& ex) {
  109 + // At this point the channel is lost
  110 + // It should be restarted
  111 + changeStatus("Error");
  112 + cerr << "Arduino [" << device << "]: " << ex.what();
  113 + }
  114 + serial.close();
95 115 }
... ...
app/src/inoChannel.h
... ... @@ -6,22 +6,28 @@ typedef vector&lt;unsigned char&gt; inoCommand;
6 6  
7 7 class inoChannel : public ofThread {
8 8 public:
9   - void setup(string device);
  9 + inoChannel(const string & device);
  10 + ~inoChannel();
  11 + void setup();
10 12 void exit();
  13 + void restart();
11 14 void threadedFunction();
12 15  
13 16 void identify();
14 17 void startStream();
15 18 void stopStream();
16 19  
17   - bool checkSystem();
18   -
  20 + //TODO Enum for Status?
19 21 ofEvent<string> statusChange;
20 22  
21 23 private:
22   -
  24 + //TODO C++ Newbie question:
  25 + // Is this the proper use of exceptions?
  26 + void waitArduino() throw(runtime_error);
  27 + void handleInput() throw(runtime_error);
  28 + void handleOutput() throw(runtime_error);
  29 + inline void changeStatus(const string & str);
23 30 inline void addCommand(const inoCommand & cmd);
24   -
25 31 ofSerial serial;
26 32 string device;
27 33 queue<inoCommand> commands;
... ...
app/src/inoControl.cpp
1 1 #include "inoControl.h"
2 2 #include "ofMain.h"
3 3  
  4 +//TODO Make Gui Thread safe
  5 +
4 6 string inoControl::defaultSettings = "settings-dev.xml";
5 7  
6   -inoControl::inoControl(string device) {
  8 +inoControl::inoControl(string device) : device(device), channel(device) {
  9 +
  10 + setupGui();
  11 + setupSettings();
  12 +
  13 + buffer = new unsigned char[BUFFER_SIZE];
  14 +
  15 + ofAddListener(
  16 + channel.statusChange,
  17 + this,
  18 + &inoControl::onStatusChanged);
  19 +}
  20 +
  21 +inoControl::~inoControl() {
  22 + channel.exit();
  23 + channel.waitForThread();
  24 + settings.saveToFile(filePath);
  25 + delete[] buffer;
  26 +}
  27 +
  28 +ofxGuiGroup * inoControl::getGui() {
  29 + return &gui;
  30 +}
  31 +
  32 +void inoControl::setupGui() {
7 33 connect.addListener(this, &inoControl::toggleConnection);
8 34 reset.addListener(this, &inoControl::resetSettings);
9 35 gui.setup(device);
... ... @@ -15,9 +41,12 @@ inoControl::inoControl(string device) {
15 41 settings.add(color.setup("Color",
16 42 ofColor(100, 100, 140),
17 43 ofColor(0, 0), ofColor(255, 255)));
  44 + settings.add(intensity.setup("Intensity", 2, 1, 4));
18 45 settings.add(offset.setup("Offset", ofVec2f(0,0)
19 46 , ofVec2f(-0.5,-0.5), ofVec2f(0.5,0.5)));
  47 +}
20 48  
  49 +void inoControl::setupSettings() {
21 50 filePath = device;
22 51 ofStringReplace(filePath, "/", "_");
23 52 filePath = "devices/" + filePath + ".xml";
... ... @@ -26,42 +55,27 @@ inoControl::inoControl(string device) {
26 55 ofFile::copyFromTo(inoControl::defaultSettings, filePath);
27 56 }
28 57 settings.loadFromFile(filePath);
29   -
30   - buffer = new unsigned char[BUFFER_SIZE];
31   -
32   - channel.setup(device);
33   - ofAddListener(
34   - channel.statusChange,
35   - this,
36   - &inoControl::onStatusChanged);
37   -}
38   -
39   -inoControl::~inoControl() {
40   - channel.exit();
41   - channel.waitForThread();
42   - settings.saveToFile(filePath);
43   - delete[] buffer;
44   -}
45   -
46   -ofxGuiGroup * inoControl::getGui() {
47   - return &gui;
48 58 }
49 59  
50 60 void inoControl::toggleConnection(bool & isSet) {
51 61 string statusStr = status;
52   - cout << "Current Status: " << statusStr << endl;
53 62 if(isSet) {
54   - if(statusStr == "Setup") {
  63 + if(statusStr == "Setup" || statusStr == "Error") {
55 64 channel.startThread();
56 65 }
57 66 channel.startStream();
58 67 } else {
59   - channel.stopStream();
  68 + if(!channel.isThreadRunning()) {
  69 + channel.stopStream();
  70 + }
60 71 }
61 72 }
62 73  
63 74 void inoControl::onStatusChanged(string & str) {
64 75 status = str;
  76 + if(str == "Error") {
  77 + connect = false;
  78 + }
65 79 }
66 80  
67 81 void inoControl::drawWave(float zoom) {
... ... @@ -79,7 +93,7 @@ void inoControl::drawWave(float zoom) {
79 93 ofSetColor(color);
80 94 ofSetPolyMode(OF_POLY_WINDING_ODD);
81 95 ofNoFill();
82   - ofSetLineWidth(1.5);
  96 + ofSetLineWidth(intensity);
83 97 ofBeginShape();
84 98 for(int i = 0; i <= BUFFER_SIZE; i++) {
85 99 ofVertex((float)i/BUFFER_SIZE * (float)width,
... ...
app/src/inoControl.h
... ... @@ -20,16 +20,24 @@ public:
20 20  
21 21 private:
22 22 static string defaultSettings;
  23 +
  24 + void setupGui();
  25 + void setupSettings();
  26 +
23 27 string filePath;
24 28 string device;
  29 +
25 30 ofxGuiGroup gui;
26 31 ofxToggle connect;
27 32 ofxToggle hide;
28 33 ofxLabel status;
  34 +
29 35 ofxGuiGroup settings;
30 36 ofxButton reset;
31 37 ofxColorSlider color;
  38 + ofxIntSlider intensity;
32 39 ofxVec2Slider offset;
  40 +
33 41 inoChannel channel;
34 42 unsigned char *buffer;
35 43 };
... ...
app/src/ofApp.cpp
... ... @@ -10,8 +10,11 @@ void ofApp::setup(){
10 10  
11 11 discoverBtn.addListener(this, &ofApp::discoverArduinos);
12 12 gui.setup();
13   - discoverBtn.setup("Discover");
14   - zoom.setup("Zoom", 1.0f, 0.25f, 4.0f);
  13 +
  14 + general.setup("General");
  15 + general.add(discoverBtn.setup("Discover"));
  16 + general.add(zoom.setup("Zoom", 1.0f, 0.25f, 4.0f));
  17 + gui.add(&general);
15 18 discoverArduinos();
16 19 }
17 20  
... ... @@ -66,8 +69,7 @@ void ofApp::discoverArduinos() {
66 69  
67 70 void ofApp::rebuildGui() {
68 71 gui.clear();
69   - gui.add(&discoverBtn);
70   - gui.add(&zoom);
  72 + gui.add(&general);
71 73 for(auto ctrl : controls) {
72 74 gui.add(ctrl.second->getGui());
73 75 }
... ...
app/src/ofApp.h
... ... @@ -28,6 +28,7 @@ private:
28 28 void rebuildGui();
29 29 map<string, inoControl *> controls;
30 30 ofxPanel gui;
  31 + ofxGuiGroup general;
31 32 ofxButton discoverBtn;
32 33 ofxFloatSlider zoom;
33 34 bool showGui = true;
... ...