Red Light Work

Dependencies:   EthernetInterface QEI_hw QEIx4 mbed-rtos mbed realtimeMMLib

Fork of realtimeMM_V3 by Graham Nicholson

Files at this revision

API Documentation at this revision

Comitter:
GTNicholson
Date:
Mon Oct 02 11:19:05 2017 +0000
Child:
1:8a447cfb30ea
Commit message:
Initial Publish Version

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
MMini.cpp Show annotated file Show diff for this revision Revisions of this file
MMini.h Show annotated file Show diff for this revision Revisions of this file
MMiniFileReader.cpp Show annotated file Show diff for this revision Revisions of this file
MMiniFileReader.h Show annotated file Show diff for this revision Revisions of this file
QEI_hw.lib Show annotated file Show diff for this revision Revisions of this file
QEIx4.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
realtimeMMLib.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMini.cpp	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,6 @@
+#include "MMini.h"
+
+iniFile::iniFile()
+{
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMini.h	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,54 @@
+#include <string> 
+using namespace std;
+
+struct iniSensor
+{
+    char ID[32];
+    char SensorType[32];
+    char Enabled[32];
+    char SampleTime[32];
+    char DataSampleQty[32];
+    char Pins[32];
+    char SensorOptions[32];
+
+};
+
+struct iniServer {
+    char IPAddress[32];
+    char Port[32];
+};
+
+struct iniStation
+{
+    char ID[32];
+    char Name[32];
+    char ComsType[32];
+    char IPAddress[32];
+    char NetworkMask[32];
+    char DefaultGateway[32];
+    char DataSendTime[32];
+};
+
+class iniFile
+{
+public:
+    iniFile();
+
+    iniStation Station;
+
+    iniServer Server;
+
+    iniSensor Sensor1;
+    iniSensor Sensor2;
+    iniSensor Sensor3;
+    iniSensor Sensor4;
+    iniSensor Sensor5;
+    iniSensor Sensor6;
+    iniSensor Sensor7;
+    iniSensor Sensor8;
+    iniSensor Sensor9;
+    iniSensor Sensor10;
+
+};
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMiniFileReader.cpp	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,219 @@
+#include "MMiniFileReader.h"
+#include <iostream>
+#include <fstream>
+#include "mbed.h"
+#include <sstream>
+#include <string>
+
+using namespace std;
+
+iniFileReader::iniFileReader()
+{
+}
+
+
+iniFileReader::~iniFileReader()
+{
+}
+
+iniFile iniFileReader::ReadFile(const char* filePath)
+{
+    iniFile fileConfig;
+
+    char line[32];
+    //std::ifstream myfile(filePath);
+
+    //Serial pc(USBTX, USBRX);
+
+
+    //std::ifstream myfile("filePath");
+
+    //pc.printf("file=%s \n", filePath);
+
+    FILE *File2 = fopen(filePath, "r"); // open file for reading
+                                                                                            //fgets(read_string,256,File2); // read first data value
+    //pc.printf("text data: %s \n", read_string);
+
+
+    if (File2)
+    {
+        char section[32];
+
+        while (fgets(line, 32, File2))
+        {
+
+            if (IsSection(line))
+            {
+                 GetSectionName(line, section);
+            } 
+            else
+            {
+
+                if ( strncmp(section , "Station", 7) == 0)
+                {
+                    char paramName[32];
+                        
+                    GetParamName(line, paramName);
+
+                    if (strncmp(paramName, "ID", 2) == 0)
+                          GetParamValue(line, fileConfig.Station.ID);
+                    
+                    if (strncmp(paramName, "Name", 4) == 0)
+                         GetParamValue(line, fileConfig.Station.Name);
+
+                    if (strncmp(paramName, "ComsType", 8 ) == 0)
+                         GetParamValue(line, fileConfig.Station.ComsType);
+
+                    if (strncmp(paramName, "IPAddress",9) == 0)
+                          GetParamValue(line, fileConfig.Station.IPAddress);
+
+                    if (strncmp(paramName, "NetworkMask", 11) == 0)
+                          GetParamValue(line, fileConfig.Station.NetworkMask);
+
+                    if (strncmp(paramName, "DefaultGateway", 14) == 0)
+                          GetParamValue(line, fileConfig.Station.DefaultGateway);
+
+                    if (strncmp(paramName, "DataSendTime", 12) == 0)
+                          GetParamValue(line, fileConfig.Station.DataSendTime);
+                }
+
+
+                if (strncmp(section, "Server", 6) == 0)
+                {
+                    char paramName[32];
+
+                    GetParamName(line, paramName);
+
+                    if (strncmp(paramName, "IPAddress", 9) == 0)
+                        GetParamValue(line, fileConfig.Server.IPAddress);
+
+                    if (strncmp(paramName, "Port", 4) == 0)
+                         GetParamValue(line, fileConfig.Server.Port);
+                }
+
+                ReadSensorSection(section, "Sensor1",7, line, &fileConfig.Sensor1);
+                ReadSensorSection(section, "Sensor2", 7, line, &fileConfig.Sensor2);
+                ReadSensorSection(section, "Sensor3", 7, line, &fileConfig.Sensor3);
+                ReadSensorSection(section, "Sensor4", 7, line, &fileConfig.Sensor4);
+                ReadSensorSection(section, "Sensor5", 7, line, &fileConfig.Sensor5);
+                ReadSensorSection(section, "Sensor6", 7, line, &fileConfig.Sensor6);
+                ReadSensorSection(section, "Sensor7", 7, line, &fileConfig.Sensor7);
+                ReadSensorSection(section, "Sensor8", 7, line, &fileConfig.Sensor8);
+                ReadSensorSection(section, "Sensor9", 7, line, &fileConfig.Sensor9);
+                ReadSensorSection(section, "Sensor10", 8, line, &fileConfig.Sensor10);
+
+            }
+
+
+        }
+        fclose(File2);
+    }
+
+    return fileConfig;
+}
+
+
+void iniFileReader::ReadSensorSection(char currentSection[], char section[], int size, char line[], iniSensor *sensor) 
+{
+
+    if (strncmp(currentSection, section, size) == 0)
+    {
+        char paramName[32];
+
+        GetParamName(line, paramName);
+
+        if (strncmp(paramName, "ID", 2) == 0)
+             GetParamValue(line, (*sensor).ID);
+
+        if (strncmp(paramName, "Type",4) == 0)
+             GetParamValue(line, (*sensor).SensorType);
+
+        if (strncmp(paramName, "Enabled", 7) == 0)
+             GetParamValue(line, (*sensor).Enabled);
+
+        if (strncmp(paramName, "SampleTime",10) == 0)
+             GetParamValue(line, (*sensor).SampleTime);
+
+        if (strncmp(paramName, "DataSampleQty",13) ==0 )
+              GetParamValue(line, (*sensor).DataSampleQty);
+
+        if (strncmp(paramName, "Pins", 4) == 0 )
+             GetParamValue(line, (*sensor).Pins);
+
+        if (strncmp(paramName, "SensorOptions", 13) == 0)
+              GetParamValue(line, (*sensor).SensorOptions);
+    }
+
+}
+
+
+
+bool iniFileReader::IsSection(char line[])
+{
+    if (line[0] == '[')
+    {
+        return true;
+    }
+
+    return false;
+}
+
+void iniFileReader::GetSectionName(char line[], char* result)
+{
+    int start = 0;
+
+    for (int i = 1; i < 32; i++) {
+        if (line[i] == ']') {
+            result[start] = '\n';
+            break;
+        }
+            
+
+        result[start] = line[i];
+        start++;
+    }
+
+
+    //int pos = line - 2;
+    //string sectionName = line.substr(1, pos);
+
+    //return sectionName;
+}
+
+void iniFileReader::GetParamName(char line[], char* result)
+{
+
+    for (int i = 0; i < 32; i++) {
+        if (line[i] == '=')
+            break;
+
+        result[i] = line[i];
+    }
+
+}
+
+void iniFileReader::GetParamValue(char line[], char* result)
+{
+    bool found = false;
+    int start = 0;
+
+    for (int i = 0; i < 32; i++) {
+
+        if (!found) {
+            if (line[i] == '=')
+                found = true;
+        }
+        else
+        {
+            if (line[i] != '\n' && line[i] != NULL)
+                result[start] = line[i];
+            else
+                {
+                    result[start] = '\0';
+                    break;
+                }                
+
+            start++;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMiniFileReader.h	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,24 @@
+#include <string>
+#include "MMini.h"
+
+class iniFileReader
+{
+public:
+    iniFileReader();
+    ~iniFileReader();
+
+    //std::string ReadStringParam(std::string section, std::string param);
+
+    iniFile ReadFile(const char* filePath);
+
+private:
+
+    bool IsSection(char line[]);
+
+    void ReadSensorSection(char currentSection[], char section[],int size, char line[], iniSensor *sensor);
+    
+    void GetSectionName(char line[], char* result);
+    void GetParamName(char line[], char* result);
+    void GetParamValue(char line[], char* result);
+
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI_hw.lib	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/hexley/code/QEI_hw/#53f8ae2cf502
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QEIx4.lib	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/OpenMoCo/code/QEIx4/#c0b87b11b9cd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,185 @@
+#include "mbed.h"
+#include "MMiniFileReader.h"
+#include "rtos.h"
+#include "EthernetInterface.h"
+#include "MBedStation.h"
+#include "sensor_base.h"
+#include "Coms.h"
+ 
+LocalFileSystem local("local");               // Create the local filesystem under the name "local"
+Serial pc(USBTX, USBRX);
+iniFile iniconfig;
+char * ComsType;
+DigitalOut light1(LED1);
+DigitalOut light3(LED3);
+DigitalOut Red(p8);
+PwmOut Green(p21);
+EthernetInterface ethtmp;
+TCPSocketConnection socktmp;
+Coms MMComs;
+int connected = 0;
+int datasendtime;
+char datasample[250];
+char * datasample1;
+char * datasample2;
+char * datasample3;
+char * datasample4;
+
+MBedStation MBed1;
+
+sensor_pulse SensorTest(p6);
+    
+int main() 
+{    
+    int error;
+    Red=1;
+    light3=1;
+    Green.period(1);
+    Green=0;
+    light1=0;
+        
+    MBed1.setup();
+    
+    
+    sensor_onoff* sensor0;
+    sensor0 = &MBed1.sensor_0;
+
+    sensor_vin* sensor1;
+    sensor1 = &MBed1.sensor_1;
+
+    sensor_vin* sensor2;
+    sensor2 = &MBed1.sensor_2;
+
+    sensor_pulse SensorTest(p6);
+    SensorTest.sensor_id = 2;
+    SensorTest.enabled = true;
+    //sensor_pulse* sensor3;
+    //sensor3->IntIn.rise(sensor3, &sensor_pulse::ISR1);
+    //sensor3 = &MBed1.sensor_3;
+
+    //Ini file reading
+    iniFileReader filereader;
+    
+    iniconfig = filereader.ReadFile("/local/ini.txt");
+
+    ComsType=iniconfig.Station.ComsType;
+    
+    pc.printf("IPAddress=%s",iniconfig.Station.IPAddress);
+    pc.printf("Coms Type=%s",ComsType);
+    datasendtime = atoi(iniconfig.Station.DataSendTime);
+    pc.printf("datasendtime=%i\r\n",datasendtime); 
+
+    while (true) { //Main Program Loop - allows for re-establishing coms
+
+        //int retrys = 5;       
+    
+        // /* Ethernet connection
+         if (strncmp(ComsType,"Ethernet",8)==0) {
+            printf("\r\n");   
+            printf("Initialising ethernet...\r\n");
+        
+            error = MMComs.EthernetInitialise(iniconfig.Station.IPAddress, iniconfig.Station.NetworkMask, iniconfig.Station.DefaultGateway);
+        
+            if (error)  {
+                printf("Error: Could not initialise ethernet (code %i)...\r\n", error);
+                while(true);
+            }
+            printf("MAC Address is %s\r\n", MMComs.getMACAddress());
+            printf("Obtaining IP address...\r\n");
+            
+            error = MMComs.EthernetConnect();
+            if (error) {
+                printf("Error: Could not obtain IP address (code %i) \r\n", error);
+                while(true);
+            }
+            
+            printf("IP Address is %s\r\n", ethtmp.getIPAddress());
+            printf("Network Mask is %s\r\n", ethtmp.getNetworkMask());
+            printf("Default Gateway is %s\r\n", ethtmp.getGateway());  
+            printf("Connecting...\r\n");
+        
+        }
+        else {
+            printf("ethernet not required...\r\n");
+        }
+        
+        //retrys = 5;
+
+       // End of Ethernet connection
+
+
+
+        //retrys = 0;
+    
+        printf("Monitoring \r\n");
+        Green=0.01;
+        light1=1;
+    
+        //Start of Monitoring Loop
+        while (true)
+        {
+            Red=0;
+            light3=0;
+            datasample1 = sensor0->read_data();
+            datasample2 = sensor1->read_data();
+            datasample3 = sensor2->read_data();
+            //datasample4 = sensor3->read_data();
+            //sensor3->reset();
+            datasample4 = SensorTest.read_data();
+            SensorTest.reset();
+            //printf("Testr: %s\n", datasample4);
+               
+            sprintf(datasample,"<<1%s,%s>>\n",datasample1,datasample4);
+            //sprintf(datasample,"<<1%s,%s,%s>>\n",datasample1,datasample2,datasample3);
+            printf(datasample);  
+        
+            if (strncmp(ComsType,"Ethernet",8)==0) {
+                printf("Trying to Connect to %s Port: %s \r\n",iniconfig.Server.IPAddress,iniconfig.Server.Port);
+                
+                Red=1;
+                light3=1;
+                
+                error = MMComs.EthernetSocketConnect(iniconfig.Server.IPAddress, atoi(iniconfig.Server.Port));            
+                if (error) {
+                    printf("Connect Failed\r\n");
+                    continue;
+                    //break;
+                }
+                
+                error = MMComs.EthernetSocketSendAll(datasample, sizeof(datasample)-1);
+                if (error) {
+                    printf("Send Failed\r\n");
+                    //continue;
+                    //break;
+                }
+                else {
+                    Red=0;                    
+                    light3=0;
+                    
+                    char buffer[300];
+                    ////////////////start of receiving status
+                    int ret;
+                    while (true) {
+                        ret = MMComs.EthernetSocketReceive(buffer, sizeof(buffer)-1);
+                        //ret = sock.receive(buffer, sizeof(buffer)-1);
+                        if (ret <= 0) break;
+                        buffer[ret] = '\0';
+                        printf("Received %d chars from server:\r\n%s\r\n", ret, buffer);
+                    }
+                    
+                }
+                MMComs.EthernetSocketClose();            
+ 
+            }
+        
+            
+            Thread::wait(datasendtime);
+      
+        } // End of Monitoring Loop
+
+    } //End of Main Programme Loop
+    
+    //eth.disconnect();
+            
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/realtimeMMLib.lib	Mon Oct 02 11:19:05 2017 +0000
@@ -0,0 +1,1 @@
+realtimeMMLib#9f82ee1feae7