Dependencies:   PWM-Coil-driver

Fork of TEMPFINALInterrupt_copy by Ian Wolf

Revision:
3:60a74a913c40
Parent:
2:4425049f4174
--- a/main.cpp	Thu Aug 31 13:57:01 2017 +0000
+++ b/main.cpp	Wed Oct 18 19:04:04 2017 +0000
@@ -1,114 +1,52 @@
 #include "mbed.h"
 #include <string>
-
-// Read temperature from MAX1363 3-channel ADC
+#include <algorithm>
+#include <vector>
 
-I2C i2c(PB_9 , PB_8);
-DigitalOut Relay(D7);
+using namespace std;
 Serial pc(SERIAL_TX, SERIAL_RX);
-
-std::string str;
+vector<string> tokens;
 char ch;
-double SetTemp=0;
-float x=0;
-
-// address information for the Serenity temperature sensor device
-const int SLAVE_ADDRESS = 0x34 << 1;
+string str("1234 4567 91011");
 
-// MAX1363 registers
-const unsigned char MAX1363_CONFIG_REG = 0x03;  // single-ended inputs, read CH0-CH1 only
-const unsigned char MAX1363_SETUP_REG  = 0xF2;  // AIN3=output, INT REF always on
-
-// MAX1363 high byte format (byte 2 is D7-D0)
-const unsigned char MAX1363_VALID_MASK = 0x90;      // HIGH bit and 12/10 bit should always be 1
-const unsigned char MAX1363_HIGH_DATA_MASK = 0x0F;
+double x;
+double y;
+double z;
 
-// channel address (0-3) is in CH1/CH0 (bits 5/6)
-const unsigned char MAX1363_CHANNEL_MASK = 0x60;
-const unsigned char MAX1363_CHANNEL_SHIFT = 5;
-
-/** conversion factors */
-const float CONV_COEFF_A3 = 4.39403;
-const float CONV_COEFF_A2 = -11.15;
-const float CONV_COEFF_A1 = 36.4955;
-const float CONV_COEFF_A0 = 266.909;
-
-const int CONV_N = 12;
-const float CONV_VREF = 2.048;
+void Tokenize(const string& str,vector<string>& tokens,const string& delimiters = " ")
+{
+    // Skip delimiters at beginning.
+    string::size_type lastPos= str.find_first_not_of(delimiters, 0);
+    // Find first "non-delimiter".
+    string::size_type pos= str.find_first_of(delimiters, lastPos);
 
-const float CONV_K_TO_C = -272.15;
-
-float AdcToC(unsigned int _adc)
-{
-    float d = CONV_VREF * _adc / (1L << CONV_N);
-    float d2 = d*d;
-    float d3 = d2*d;
-    float temp_k = (CONV_COEFF_A3 * d3) +
-        (CONV_COEFF_A2 * d2) +
-        (CONV_COEFF_A1 * d) +
-        CONV_COEFF_A0;
-    return temp_k + CONV_K_TO_C;
+    while (string::npos != pos || string::npos != lastPos){
+        // Found a token, add it to the vector.
+        tokens.push_back(str.substr(lastPos, pos - lastPos));
+        // Skip delimiters.  Note the "not_of"
+        lastPos = str.find_first_not_of(delimiters, pos);
+        // Find next "non-delimiter"
+        pos = str.find_first_of(delimiters, lastPos);
+    }
 }
 
 void callback(){
 ch=pc.getc();
 str+=ch;
-SetTemp= atof(str.c_str());
 return;
 }
 
-
-int main() {
+int main()
+{
     pc.baud(115200);
-    
     pc.attach(&callback);
-
-    // reserve 4 bytes for transfer
-    char cmd[4];
-
-    // initialize the adc device
-    cmd[0] = MAX1363_CONFIG_REG;
-    cmd[1] = MAX1363_SETUP_REG;
-    i2c.write(SLAVE_ADDRESS, cmd, 2);
-
-    // holds raw adc values as we convert them
-    unsigned int adc[2] = {0};
-    float values[2] = {0};
-
-    while (1) {
-        //Clear string holding the input from serial
-        str.clear();
-        
-        wait(1);
-
-        // clear buffer, only to detect invalid data
-        cmd[0] = 0x00;
-        cmd[1] = 0x00;
-        cmd[2] = 0x00;
-        cmd[3] = 0x00;
-
-        // read 4 bytes from
-        i2c.read(SLAVE_ADDRESS, cmd, 4);
-
-        for (int i=0; i<4; i+=2) {
-            // first 4 bits contain expected bits and channel id
-            if ((cmd[i] & MAX1363_VALID_MASK) == MAX1363_VALID_MASK) {
-                unsigned char ch = ((cmd[i] & MAX1363_CHANNEL_MASK) >> MAX1363_CHANNEL_SHIFT);
-                adc[ch] = ((int)(cmd[i] & MAX1363_HIGH_DATA_MASK) << 8) | cmd[i+1];
-                values[ch] = AdcToC(adc[ch]);
-            } else {
-                // handle invalid data
-            }
-        }
-        x=values[1];
-        if (x<SetTemp){
-            Relay=0;
-        }
-        if (x>SetTemp+1){
-            Relay=1;
-        }
-        // output temperature data
-        printf("%0.2f\t %0.2f\t %0.2f\r\n", SetTemp, values[0], values[1]);
-
-    }
+    
+    while(1){
+    Tokenize(str, tokens);
+    x=atof(tokens[0].c_str());
+    y=atof(tokens[1].c_str());
+    z=atof(tokens[2].c_str());
+    str.clear();
+    pc.printf("%0.2f\t %0.2f\r\n",x,y);
 }
+}
\ No newline at end of file