Dependencies:   mbed C12832 LM75B FXOS8700Q

Files at this revision

API Documentation at this revision

Comitter:
tweagle
Date:
Wed Nov 04 12:41:46 2020 +0000
Commit message:
project1-iot-simondeneve

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
FXOS8700Q.lib Show annotated file Show diff for this revision Revisions of this file
LM75B.lib Show annotated file Show diff for this revision Revisions of this file
StateMachine.cpp Show annotated file Show diff for this revision Revisions of this file
StateMachine.h 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-os.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/chris/code/C12832/#7de323fa46fe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700Q.lib	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Freescale/code/FXOS8700Q/#aee7dea904e2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/chris/code/LM75B/#6a70c9303bbe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StateMachine.cpp	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,162 @@
+#include "StateMachine.h"
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "LM75B.h"
+    
+    uint16_t temperature =(sensor.read());
+    char upper = (temperature >> 8) & 0x00FF;
+    char lower = temperature & 0x00FF;
+    char pwm = (char)(pot1);
+    char id = 0x14;
+
+    char payload[4] = {upper, lower, pwm, id};
+    
+StateMachine::StateMachine(){
+    r = new PwmOut(D5);
+    g = new PwmOut(D9);
+    b = new PwmOut(D8);
+    
+    pot1 = new AnalogIn (A0);
+    pot2  = new AnalogIn(A1);
+    up = new DigitalIn(A2);
+    down = new DigitalIn(A3);
+    left = new DigitalIn(A4);
+    right = new DigitalIn(A5);
+
+    lcd = new C12832(D11, D13, D12, D7, D10);
+    
+    sensor = new LM75B(D14,D15);
+    
+    led = new DigitalOut(LED1);
+    
+    
+    currentState = INIT;
+    }
+    
+StateMachine::~StateMachine(){
+    delete r;
+    delete g;
+    delete b;
+    delete pot1;
+    delete pot2;
+    delete up;
+    delete down;
+    delete left;
+    delete right;
+
+    delete lcd;
+    
+    delete sensor;
+    
+    delete led;
+}
+
+void StateMachine::start()
+{
+    while(true) 
+    {
+//            switch(currentState) {
+//            case INIT:
+//            actionInit();
+//            currentstate =;
+//            break;
+//            case BLUE:
+//            actionBlue();
+//            currentState = INIT;
+//            break;
+//            case GREEN:
+//            actionGreen();
+//            currentState = INIT;
+//            break;
+//            default:
+//            currentState = CERROR;
+//            return;
+        if(currentState == INIT){actionInit();}
+        if(currentState == SERVER) {actionServer();}
+        if(currentState == CLIENT) {actionClient();}    
+    }
+}
+
+void StateMachine::actionInit(){
+    printf("Select left for server, right for client.");
+    if(left){currentState = SERVER;}
+    if(right){currentState = CLIENT;}
+    wait(1.0);
+    }
+
+void StateMachine::actionClient(){
+    
+    printf("Client example\n\r");
+ 
+    EthernetInterface eth;
+    eth.set_network("192.168.0.20","255.255.255.0","192.168.0.1");
+    eth.connect();
+ 
+    printf("The client IP address is '%s'\n\r", eth.get_ip_address());
+ 
+    TCPSocket socket;
+    socket.open(&eth);
+    socket.connect("192.168.0.28",4000);
+ 
+
+    
+    payload[0] = upper;
+    payload[1] = lower;
+    payload[2] = pwm;
+    payload[3] = id;
+    
+    printf("%d\n", upper);
+    printf("%d\n", lower);
+
+    // print hex value of temperature on lcd
+    lcd.printf("Temp %.1x\n", sensor.read() );
+    printf(payload);
+    
+    socket.send(payload, 4);
+    socket.close();
+    wait(1.0);
+    }
+
+void StateMachine::actionServer(){
+    printf("Server example\n\r");
+    
+    EthernetInterface eth;
+    eth.set_network("192.168.0.28","255.255.255.0","192.168.0.1");
+    eth.connect();
+    
+    printf("The Server IP address is '%s'\n\r", eth.get_ip_address());
+    
+    TCPServer srv(&eth);  
+    
+    srv.bind(4000);
+    
+    srv.listen();
+    
+    while(true){
+        TCPSocket client;
+        SocketAddress client_addr;
+        char *buffer = "Hello TCP client!\r\n";
+        
+        srv.accept(&client, &client_addr);
+        
+        printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), 
+                    client_addr.get_port());
+                    
+        client.send(buffer, 256);
+        
+        char rbuffer[4];
+        client.recv(rbuffer, sizeof rbuffer);
+        
+        uint8_t upper = rbuffer[0];
+        uint8_t lower = rbuffer[1];
+        uint8_t PWM = rbuffer[2];
+        uint8_t ID = rbuffer[3];        
+        
+        float temp = ((upper << 8) | lower) / 256.00;
+        
+        printf("Temperature is %f\n", temp);
+        
+        client.close();
+        wait(1.0);
+        }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StateMachine.h	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,38 @@
+#ifndef STATEMACHINE_H
+#define STATEMACHINE_H
+
+#include "mbed.h"
+#include "LM75B.h"
+#include "C12832.h"
+
+enum State {INIT, CLIENT, SERVER, CERROR};
+
+class StateMachine {
+private:
+    State currentState;
+    void actionInit();
+    void actionClient();
+    void actionServer();
+    PwmOut* r;
+    PwmOut* g;
+    PwmOut* b;
+    AnalogIn* pot1;
+    AnalogIn* pot2;
+    DigitalIn* up;
+    DigitalIn* down;
+    DigitalIn* left;
+    DigitalIn* right;
+
+    C12832* lcd;
+    
+    LM75B* sensor;
+    
+    DigitalOut* led;
+public:     
+    StateMachine();
+    ~StateMachine();
+    
+    void start();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,8 @@
+#include "mbed.h"
+#include "StateMachine.h"
+
+int main()
+{
+    StateMachine statemachine;
+    statemachine.start();    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e62a1b9236b44e70ae3b0902dc538481c04d455b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Nov 04 12:41:46 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file