Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:32ac32201889
- Parent:
- 0:da6d7d20a81a
- Child:
- 2:b258510a2573
--- a/main.cpp Sun Apr 24 20:15:26 2016 +0000
+++ b/main.cpp Mon Apr 25 01:32:33 2016 +0000
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <iostream>
#include <string>
+#include <ctype.h>
/*
Master firmware
@@ -20,10 +21,10 @@
RawSerial pc(USBTX,USBRX); //UART monitor from pc
RawSerial wifi(p9, p10); // master to wifi UART
-AnalogIn m_pot(p15);
+AnalogIn mPot(p15);
-float m_pot_val = 0.0;
-float s_pot_val = 0.0;
+float mPot_val = 0.0;
+float sPot_val = 0.0;
PwmOut pwm(p27);
@@ -35,11 +36,16 @@
string feather_receive = ""; //data recieved from Feather
string feather_sent = ""; //data sent to the Feather
+string mDimStr = "";
+string mTempStr = "";
+string sTempStr = "";
+string sDimStr = "";
+
int convert_val = 0;
-int s_dim_amt = 0;
-int m_dim_amt = 0;
-int s_temp = 0;
-int m_temp = 0;
+int sDimAmt = 0;
+int mDimAmt = 0;
+int sTemp = 0;
+int mTemp = 0;
float dim_percent = 0.0;
int i = 0; //intialize
@@ -51,7 +57,7 @@
while(1){
// Data frame[18]: G S | M A | 000 | 000 | S | A | 000 | 000
// 'GWDM' 0-100 Master Light Level, OR 'GWDS' 0-100 Slave Light Level
- char wifi_message [18] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
+ char wifi_message [18] = {};
string message = ""; // Empty Message String
int i = 0; // Iterator
@@ -66,58 +72,90 @@
i = 0; // Reset the iterator
// ------------------------------Data parsing --------------------------------------------------------
- if ( toupper(wifi_message [0]) == "G" && wifi_message [2] == "M" && wifi_message [10] == "S" )
+ if ( toupper(wifi_message [0]) == 'G' && toupper(wifi_message[2]) == 'M' && toupper(wifi_message[10]) == 'S')
{
- m_dim_amt = atoi(wifi_message[4:6]);
- m_temp = atoi(wifi_message[7:9]);
- s_dim_amt = atoi(wifi_message[12:14]);
- s_temp = aoti(wifi_message[15:17]);
-
- if (wifi_message [1] == "S") //Print status
+ int data [12] = {};
+ for(int i = 0; i < 7; i++) // Parsing master and slave dim and temp values as int array
+ {
+ data[i] = (int)wifi_message[i+4]-48; // first 6 values are in char, subtracting 48 gives raw int
+ data[i+6] = (int)wifi_message[i+12]-48; // second 6 values are for slave values
+ }
+
+ if (toupper(wifi_message [1]) == 'S') //Print status
{
- pc.printf(wifi_message); //Echo to terminal
+ for(int i = 0; i < 3; i++) // Convert integer array to string
+ {
+ mDimStr += data[i+4];
+ mTempStr += data[i+7];
+ sDimStr += data[i+12];
+ sTempStr += data[i+15];
+ }
+ pc.printf("Master Dim: %s\n", mDimStr); // Print to terminal, NOTICE: weird warning
+ pc.printf("Master Temp: %s\n", mTempStr);
+ pc.printf("Slave Dim: %s\n", sDimStr);
+ pc.printf("Slave Temp: %s\n", sTempStr);
}
- else if (wifi_message [1] == "D") //Dim
+ else if (toupper(wifi_message [1]) == 'D') //Dim
{
- m_dim_amt = atoi(wifi_message[4:6]);
- m_temp = atoi(wifi_message[7:9]);
- s_dim_amt = atoi(wifi_message[12:14]);
- s_temp = aoti(wifi_message[15:17]);
+ for(int i = 0; i < 3; i++) // Grab master and slave dim values
+ {
+ mDimAmt += data[i];
+ //mTemp += data[i+3];
+ sDimAmt += data[i+6];
+ //sTemp += data[i+9];
+ }
- }
-
+ }
+
+ /* IGNORE THIS
+ int md1, md2, md3, mt1, mt2, mt3; // Master dim and temp values
+ int sd1, sd2, sd3, st1, st2, st3; // Slave dim and temp values
+ md1 = (int)wifi_message[4]-48;
+ md2 = (int)wifi_message[5]-48;
+ md3 = (int)wifi_message[6]-48;
+ mt1 = (int)wifi_message[7]-48;
+ mt2 = (int)wifi_message[8]-48;
+ mt3 = (int)wifi_message[9]-48;
+ sd1 = (int)wifi_message[12]-48;
+ sd2 = (int)wifi_message[13]-48;
+ sd3 = (int)wifi_message[14]-48;
+ st1 = (int)wifi_message[15]-48;
+ st2 = (int)wifi_message[16]-48;
+ st3 = (int)wifi_message[17]-48;
+ */
+
}
// ----------------------------- Prioritize Wireless/Manual Mode -----------------------------------
- if ( m_pot.read() > 0.0 )
+ if ( mPot.read() > 0.0 )
{
- if (wifi_message [3] == "0" ) // Check: Manual mode for Master
+ if (wifi_message [3] == '0' ) // Check: Manual mode for Master
{
master_manual = true;
- m_pot_val = m_pot.read(); //read in value from potentiometer
- pwm.write(m_pot_val); //output potentiometer value to pwm
+ mPot_val = mPot.read(); //read in value from potentiometer
+ pwm.write(mPot_val); //output potentiometer value to pwm
}
else if (wifi_message [3] == "1" ) //Check: Wireless for master
{
- pwm.write((float)(m_dim_amt/100)); //Send out dim value from parsed data wirelessly
+ pwm.write((float)(mDimAmt/100)); //Send out dim value from parsed data wirelessly
}
}
- if ( s_pot.read() > 0.0)
+ if ( sPot.read() > 0.0)
{
if (wifi_message [11] == "0") // Check: Manual mode for Slave
{
slave_manual = true;
- s_pot_val = s_pot.read();
- pwm.write(s_pot_val);
+ sPot_val = sPot.read();
+ pwm.write(sPot_val);
}
else if (wifi_message [11] == "1" ) //Check: Wireless for slave
{
- pwm.write((float)(s_dim_amt/100)); //Send out dim value from parsed data wirelessly
+ pwm.write((float)(sDimAmt/100)); //Send out dim value from parsed data wirelessly
}
}