1. iorodeo
  2. colorimeter

Commits

iorodeo  committed 93a27d1

Added required libraries to project - so users don't need to deal with this.

  • Participants
  • Parent commits 2295176
  • Branches default

Comments (0)

Files changed (6)

File firmware/LICENSE_Streaming.txt

View file
+GNU LESSER GENERAL PUBLIC LICENSE
+
+Version 3, 29 June 2007
+
+Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
+
+Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
+
+This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
+
+0. Additional Definitions.
+As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
+
+“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
+
+An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
+
+A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
+
+The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
+
+The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
+
+1. Exception to Section 3 of the GNU GPL.
+You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
+
+2. Conveying Modified Versions.
+If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
+
+a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
+b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
+3. Object Code Incorporating Material from Library Header Files.
+The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
+
+a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
+b) Accompany the object code with a copy of the GNU GPL and this license document.
+4. Combined Works.
+You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
+
+a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
+b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
+c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
+d) Do one of the following:
+0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
+1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
+e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
+5. Combined Libraries.
+You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
+
+a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
+b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
+6. Revised Versions of the GNU Lesser General Public License.
+The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
+
+If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

File firmware/SerialHandler.h

View file
 #include "SerialReceiver.h"
 #include "Colorimeter.h"
 
-
-
 const int CMD_CALIBRATE=0;
 const int CMD_GET_MEASUREMENT=1;
 const int CMD_SET_NUM_SAMPLES=2;

File firmware/SerialReceiver.cpp

View file
+#include "Streaming.h"
+#include "SerialReceiver.h"
+
+SerialReceiver::SerialReceiver() {
+    error = SR_ERR_NONE;
+    resetState();
+    startChar = SR_DFLT_START_CHAR;
+    stopChar = SR_DFLT_STOP_CHAR;
+    sepChar = SR_DFLT_SEP_CHAR;
+    resetState();
+}
+
+bool SerialReceiver::messageReady() {
+    if (state == SR_STATE_MESSAGE) {
+        return true;
+    }
+    else {
+        return false;
+    }
+}
+
+uint8_t SerialReceiver::numberOfItems() {
+    if (state == SR_STATE_MESSAGE) {
+        return itemCnt;
+    }
+    else {
+        return 0;
+    }
+}
+
+char SerialReceiver::readChar(uint8_t itemNum, uint8_t ind) {
+    char rval;
+    if (checkItemRange(itemNum)) {
+        if (ind < itemLenBuffer[itemNum]) {
+            rval = itemBuffer[itemNum][ind];
+        }
+        else {
+            rval = 0;
+        }
+    }
+    else {
+        rval = 0;
+    }
+    return rval;
+}
+
+int SerialReceiver::readInt(uint8_t itemNum) {
+    if (checkItemRange(itemNum)) {
+        return atoi(itemBuffer[itemNum]);
+    }
+    else {
+        return 0;
+    }
+}
+
+long SerialReceiver::readLong(uint8_t itemNum) {
+    if (checkItemRange(itemNum)) {
+        return atol(itemBuffer[itemNum]);
+    }
+    else {
+        return 0;
+    }
+}
+
+double SerialReceiver::readDouble(uint8_t itemNum) {
+    if(checkItemRange(itemNum)) {
+        return atof(itemBuffer[itemNum]);
+    }
+    else {
+        return 0;
+    }
+}
+
+float SerialReceiver::readFloat(uint8_t itemNum) {
+    return (float) readDouble(itemNum);
+}
+
+void SerialReceiver::copyString(uint8_t itemNum, char* string, uint8_t size) {
+    if (checkItemRange(itemNum)) {
+        strlcpy(string,itemBuffer[itemNum],size);
+    }
+    else {
+        if (size >= 0) {
+            string[0] = '\0';
+        }
+    }
+}
+
+
+bool SerialReceiver::checkItemRange(uint8_t itemNum) {
+    if ((state==SR_STATE_MESSAGE) && (itemNum >=0) && (itemNum < itemCnt)) {
+        return true;
+    }
+    else {
+        return false;
+    }
+}
+
+void SerialReceiver::process(int serialByte) {
+    if (serialByte > 0){
+        switch (state) {
+            case SR_STATE_IDLE:
+                processNewMsg(serialByte);
+                break;
+            case SR_STATE_RECEIVING:
+                processCurMsg(serialByte);
+                break;
+            case SR_STATE_MESSAGE:
+                break;
+            default:
+                break;
+
+        }
+    }
+}
+
+void SerialReceiver::processNewMsg(int serialByte) {
+    if (serialByte == startChar) {
+        resetItems();
+        state = SR_STATE_RECEIVING;
+        error = SR_ERR_NONE;
+    }
+    else if ((serialByte == '\n') || (serialByte == ' ')) {
+        return;
+    }
+    else {
+        error = SR_ERR_ILLEGAL_CHAR;
+        resetState();
+    }
+}
+
+void SerialReceiver::processCurMsg(int serialByte) {
+    if (serialByte == startChar) {
+        handleStartChar(serialByte);
+    }
+    else if (serialByte == stopChar) {
+        handleStopChar(serialByte);
+    }
+
+    else if (serialByte == sepChar) {
+        handleSepChar(serialByte);
+    }
+
+    else {
+        handleNewChar(serialByte);
+    }
+}
+
+void SerialReceiver::handleStartChar(int serialByte) {
+    resetState();
+    error = SR_ERR_ILLEGAL_CHAR;
+}
+
+void SerialReceiver::handleStopChar(int serialByte) {
+    if (itemPos > 0) {
+        itemBuffer[itemCnt][itemPos] = '\0';
+        itemLenBuffer[itemCnt] = itemPos;
+        itemCnt++;
+    }
+    // Check message checksum here .... if not OK reset.
+    state = SR_STATE_MESSAGE;
+}
+
+void SerialReceiver::handleSepChar(int serialByte) {
+    if (itemPos > 0) {
+        if (itemCnt == SR_MAX_ITEMS-1) {
+            resetState();
+            error = SR_ERR_MESSAGE_LENGTH;
+        }
+        else {
+            itemBuffer[itemCnt][itemPos] = '\0';
+            itemLenBuffer[itemCnt] = itemPos;
+            itemCnt++;
+            itemPos = 0;
+        }
+    }
+    else {
+        resetState();
+        error = SR_ERR_ITEM_LENGTH;
+    }
+}
+
+void SerialReceiver::handleNewChar(int serialByte) {
+    if (itemPos == SR_MAX_ITEM_SZ) {
+        resetState();
+        error = SR_ERR_ITEM_LENGTH;
+    }
+    else {
+        if ((serialByte == '\n') || (serialByte == ' ')) {
+            return;
+        }
+        itemBuffer[itemCnt][itemPos] = serialByte;
+        itemPos++;
+    }
+}
+
+void SerialReceiver::reset() {
+    resetState();
+    error = SR_ERR_NONE;
+}
+
+void SerialReceiver::resetState() {
+    resetItems();
+    state = SR_STATE_IDLE;
+}
+
+void SerialReceiver::resetItems() {
+    itemCnt = 0;
+    itemPos = 0;
+    for (int i=0; i<SR_MAX_ITEMS;i++) {
+        itemBuffer[i][0] = '\0';
+        itemLenBuffer[i] = 0;
+    }
+}
+
+void SerialReceiver::printInfo() {
+    Serial << "Current Message Info" << endl;
+    Serial << "--------------------" << endl;
+    Serial << "state:   " << _DEC(state) << endl;
+    Serial << "error:   " << _DEC(error) << endl;
+    Serial << "itemCnt: " << _DEC(itemCnt) << endl;
+    Serial << "itemPos: " << _DEC(itemPos) << endl;
+    for (int i=0; i<itemCnt; i++) {
+        Serial << "buf[" << _DEC(i) << "] = " << itemBuffer[i] << endl;
+    }
+    Serial << endl;
+}
+
+void SerialReceiver::printMessageInfo() {
+    Serial << "Message = " << endl;
+    for (int i=0; i<itemCnt; i++) {
+        Serial << "buf[" << _DEC(i) << "] = " << itemBuffer[i] << ", len = " << _DEC(itemLenBuffer[i]) << endl;
+    }
+    Serial << endl;
+}
+
+void SerialReceiver::printMessage() {
+    for (int i=0; i<itemCnt; i++) {
+        if (i < itemCnt-1) {
+            Serial << itemBuffer[i] << " ";
+        }
+        else {
+            Serial << itemBuffer[i];
+        }
+    }
+    Serial << endl;
+}

File firmware/SerialReceiver.h

View file
+#if defined(ARDUINO) && ARDUINO >= 100
+#include "Arduino.h"
+#else
+#include "WProgram.h"
+#endif
+
+#ifndef SerialReceiver_h
+#define SerialReceiver_h
+
+enum {SR_MAX_ITEM_SZ = 25};
+enum {SR_MAX_ITEMS = 4};
+
+const char SR_DFLT_START_CHAR = '[';
+const char SR_DFLT_STOP_CHAR = ']';
+const char SR_DFLT_SEP_CHAR = ',';
+
+const uint8_t SR_STATE_IDLE = 0;
+const uint8_t SR_STATE_RECEIVING = 1;
+const uint8_t SR_STATE_MESSAGE = 2;
+
+const uint8_t SR_ERR_NONE = 0;
+const uint8_t SR_ERR_ILLEGAL_CHAR = 1;
+const uint8_t SR_ERR_ITEM_LENGTH = 2;
+const uint8_t SR_ERR_MESSAGE_LENGTH = 3;
+
+class SerialReceiver {
+
+    public:
+        SerialReceiver();
+        void process(int serialByte);
+        bool messageReady();
+        void reset();
+        uint8_t numberOfItems();
+        uint8_t itemLength(uint8_t itemNum);
+        char readChar(uint8_t itemNum, uint8_t ind); 
+        int readInt(uint8_t itemNum);
+        long readLong(uint8_t itemNum);
+        float readFloat(uint8_t itemNum);
+        double readDouble(uint8_t itemNum);
+        void copyString(uint8_t itemNum, char *string, uint8_t size);
+        void printInfo();
+        void printMessageInfo();
+        void printMessage();
+        
+    private:
+        uint8_t state;
+        uint8_t error;
+        char itemBuffer[SR_MAX_ITEMS][SR_MAX_ITEM_SZ+1];
+        uint8_t itemLenBuffer[SR_MAX_ITEMS];
+        uint8_t itemCnt;
+        uint8_t itemPos; 
+        char startChar;
+        char stopChar; 
+        char sepChar;
+        void resetItems();
+        void resetState();
+        void processNewMsg(int serialByte);
+        void processCurMsg(int serialByte);
+        void handleNewChar(int serialByte);
+        void handleSepChar(int serialByte);
+        void handleStopChar(int serialByte);
+        void handleStartChar(int serialByte);
+        bool checkItemRange(uint8_t itemNum);
+};
+
+
+#endif

File firmware/Streaming.h

View file
+/*
+Streaming.h - Arduino library for supporting the << streaming operator
+Copyright (c) 2010-2012 Mikal Hart.  All rights reserved.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef ARDUINO_STREAMING
+#define ARDUINO_STREAMING
+
+#if defined(ARDUINO) && ARDUINO >= 100
+#include "Arduino.h"
+#else
+#include "WProgram.h"
+#endif
+
+#define STREAMING_LIBRARY_VERSION 5
+
+// Generic template
+template<class T> 
+inline Print &operator <<(Print &stream, T arg) 
+{ stream.print(arg); return stream; }
+
+struct _BASED 
+{ 
+  long val; 
+  int base;
+  _BASED(long v, int b): val(v), base(b) 
+  {}
+};
+
+#if ARDUINO >= 100
+
+struct _BYTE_CODE
+{
+	byte val;
+	_BYTE_CODE(byte v) : val(v)
+	{}
+};
+#define _BYTE(a)    _BYTE_CODE(a)
+
+inline Print &operator <<(Print &obj, const _BYTE_CODE &arg)
+{ obj.write(arg.val); return obj; } 
+
+#else
+
+#define _BYTE(a)    _BASED(a, BYTE)
+
+#endif
+
+#define _HEX(a)     _BASED(a, HEX)
+#define _DEC(a)     _BASED(a, DEC)
+#define _OCT(a)     _BASED(a, OCT)
+#define _BIN(a)     _BASED(a, BIN)
+
+// Specialization for class _BASED
+// Thanks to Arduino forum user Ben Combee who suggested this 
+// clever technique to allow for expressions like
+//   Serial << _HEX(a);
+
+inline Print &operator <<(Print &obj, const _BASED &arg)
+{ obj.print(arg.val, arg.base); return obj; } 
+
+#if ARDUINO >= 18
+// Specialization for class _FLOAT
+// Thanks to Michael Margolis for suggesting a way
+// to accommodate Arduino 0018's floating point precision
+// feature like this:
+//   Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision
+
+struct _FLOAT
+{
+  float val;
+  int digits;
+  _FLOAT(double v, int d): val(v), digits(d)
+  {}
+};
+
+inline Print &operator <<(Print &obj, const _FLOAT &arg)
+{ obj.print(arg.val, arg.digits); return obj; }
+#endif
+
+// Specialization for enum _EndLineCode
+// Thanks to Arduino forum user Paul V. who suggested this
+// clever technique to allow for expressions like
+//   Serial << "Hello!" << endl;
+
+enum _EndLineCode { endl };
+
+inline Print &operator <<(Print &obj, _EndLineCode arg) 
+{ obj.println(); return obj; }
+
+#endif

File firmware/firmware.pde

View file
 #include <math.h>
 #include <EEPROM.h>
-#include <Streaming.h>
+#include "Streaming.h"
 #include "io_pins.h"
 #include "RGBLed.h"
 #include "ColorSensor.h"
     Serial.begin(9600);
     colorimeter.initialize();
     colorimeter.numSamples = 500;
-    //colorimeter.numSamples = 1000;
-    //colorimeter.EEPROM_loadCalibration();
-    
 }
 
 void loop() {
     comm.processInput();
-
-    //colorimeter.led.setRed();
-    //colorimeter.sensor.setChannelRed();
-
-    //colorimeter.led.setGreen();
-    //colorimeter.sensor.setChannelGreen();
-
-    //colorimeter.led.setBlue();
-    //colorimeter.sensor.setChannelBlue();
-
-    //colorimeter.led.setWhite();
-    //colorimeter.sensor.setChannelClear();
 }