Backing up an unused program in case of future need

Dependencies:   mbed

wifi.cpp

Committer:
andrewboyson
Date:
2016-04-13
Revision:
0:09f915e6f9f6
Child:
1:94282484baae

File content as of revision 0:09f915e6f9f6:

#include "mbed.h"
#include  "esp.h"
#include   "at.h"
#include "wifi.h"
#include  "log.h"
#include   "io.h"

#define SSID "Andrew and Kate"
#define PASSWORD "Hopeless"


int WifiStatus;

#define AM_STOPPING    0
#define AM_STARTING    1
#define AM_AUTOBAUDING 2
#define AM_BAUDING     3
#define AM_CONNECTING  4
#define AM_MUXING      5
#define AM_STARTED     6
static int am = AM_STOPPING;
int WifiStarted() { return am == AM_STARTED; }

int WifiMain()
{
    if (AtBusy()) return 0;
    
    static int result = AT_NONE;
    static int autoBaud = 1;
    
    static int autoBaudSucceeded = false;
    
    switch (am)
    {
        case AM_STOPPING:
            AtResetAndStop();
            am = AM_STARTING;
            result = AT_NONE;
            break;
        case AM_STARTING:
            switch (result)
            {
                case AT_NONE:
                    AtReleaseResetAndStart(&result);
                    break;
                case AT_SUCCESS:
                    switch (WifiStatus)
                    {
                        case WIFI_GOT_IP:
                            am = AM_MUXING;
                            result = AT_NONE;
                            break;
                        default:
                            LogF("Started WiFi and expected WIFI_GOT_IP but had %d", WifiStatus);
                            return -1;
                    }
                    break;
                default:
                    switch (WifiStatus)
                    {
                        case WIFI_READY:
                            am = AM_CONNECTING;
                            result = AT_NONE;
                            break;
                        default:
                            am = AM_AUTOBAUDING;
                            result = AT_NONE;
                            autoBaud = 1;
                            break;
                    }
                    break;
            }
            break;
        case AM_AUTOBAUDING:
            if (autoBaudSucceeded) return -1;
            EspBaud(9600 * autoBaud);
            switch (result)
            {
                case AT_NONE:
                    AtAt(&result);
                    break;
                case AT_SUCCESS:
                    autoBaudSucceeded = true;
                    am = AM_BAUDING;
                    result = AT_NONE;
                    break;
                default:
                    if (autoBaud == 16)
                    {
                        LogCrLf("Could not connect to ESP");
                        return -1;
                    }
                    autoBaud++;
                    result = AT_NONE;
                    break;
            }
            break;
        case AM_BAUDING:
            switch (result)
            {
                case AT_NONE:
                    AtBaud(BAUD, &result);
                    break;
                case AT_SUCCESS:
                    EspBaud(BAUD);
                    am = AM_STOPPING;
                    result = AT_NONE;
                    break;
                default:
                    LogCrLf("Could not change baud");
                    return -1;
            }
            break;
        case AM_CONNECTING:
            switch (result)
            {
                case AT_NONE:
                    AtConnect(SSID, PASSWORD, &result);
                    break;
                case AT_SUCCESS:
                    am = AM_MUXING;
                    result = AT_NONE;
                    break;
                default:
                    LogCrLf("Could not connect to WiFi");
                    return -1;
            }
            break;
        case AM_MUXING:
            switch (result)
            {
                case AT_NONE:
                    AtMux(&result);
                    break;
                case AT_SUCCESS:
                    am = AM_STARTED;
                    result = AT_NONE;
                    break;
                default:
                    LogCrLf("Could not set up multiple ids");
                    return -1;
            }
            break;
        case AM_STARTED:
            LogEnable(false);
            return 0;
        default:
            LogF("Unknown \'am\' %d", am);
            return -1;
    }
   
    return 0;
}