IOTON boards API using mbed SDK - http://ioton.cc/plataforma-ton

Dependents:   ton-bot_teste ton-bot_seguidor_linha ton-bot_seguidor_parede

Fork of IOTON-API by Kleber Silva

Ioton.h

Committer:
krebyy
Date:
2016-12-20
Revision:
2:b3c3bf0b9101
Parent:
0:cbba28a205fa
Child:
3:9c7195d31602

File content as of revision 2:b3c3bf0b9101:

/* Ioton Boards Library
 * Copyright (c) 2016 Ioton Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IOTON_H
#define IOTON_H

#include "mbed.h"
#include "BMX055.h"
#include "USBSerial.h"
#include "ESP8266Interface.h"
#include "MQTTESP8266.h"
#include "MQTTClient.h"

#define ON  1
#define OFF 0
#define BATTERY_SCALE   1.4681f // Voltage divider: [(R14 + R15) / R15]

// Generic signals namings
#define LED1          PB_0
#define LED2          PB_5
#define LED3          PB_4
#define USBTX         PC_6  /* USART6 */
#define USBRX         PC_7
#define I2C_SCL       PA_8  /* I2C3 */
#define I2C_SDA       PC_9
#define SPI_MOSI      PA_7  /* SPI3 */
#define SPI_MISO      PA_6
#define SPI_SCK       PA_5
#define SPI_CS        PA_4

// Ton Board signals namings
#define LED_RED       PB_0
#define LED_GREEN     PB_5
#define LED_BLUE      PB_4
#define SW_USER       PC_13
#define IMU_SCL       PB_10 /* I2C2 */
#define IMU_SDA       PB_11
#define BT_TX         PA_9  /* USART1 */
#define BT_RX         PA_10
#define WIFI_TX       PA_2  /* USART2 */
#define WIFI_RX       PA_3
#define WIFI_PWD      PB_12
#define WIFI_MODE     PB_13
#define WIFI_RST      PC_15


#define PIN0          PC_6
#define PIN1          PC_7
#define PIN2          PC_8
#define PIN3          PC_9
#define PIN4          PA_8
#define PIN5          PC_10
#define PIN6          PC_11
#define PIN7          PC_12
#define PIN8          PD_2
#define PIN9          PB_6
#define PIN10         PB_7
#define PIN11         PB_8
#define PIN12         PB_9
#define PIN13         PB_3
#define PIN14         PA_15
#define PIN15         PC_0
#define PIN16         PC_1
#define PIN17         PC_2
#define PIN18         PC_3
#define PIN19         PA_0
#define PIN20         PA_1
#define PIN21         PA_4
#define PIN22         PA_5
#define PIN23         PA_6
#define PIN24         PA_7
#define PIN25         PC_4
#define PIN26         PC_5
#define PIN27         PA_2
#define PIN28         PA_3
#define PIN29         PA_9
#define PIN30         PA_10

typedef enum
{
    RED,
    GREEN,
    BLUE,
    YELLOW,
    CYAN,
    MAGENTA,
    WHITE,
    NONE
} LEDType_t;


Serial bluetooth(BT_TX, BT_RX);
USBSerial usb(0x1f00, 0x2012, 0x0001, false);
MQTTESP8266 wifi(WIFI_TX, WIFI_RX, WIFI_RST, WIFI_MODE, WIFI_PWD, "defualtssid", "defaultpass");
DigitalOut Reset(WIFI_RST);

DigitalIn USER(SW_USER);
PwmOut ledRED(LED_RED);
PwmOut ledGREEN(LED_GREEN);
PwmOut ledBLUE(LED_BLUE);
AnalogIn battery(PB_1);

BMX055 imu;


class Ioton
{
public:
    Ioton()
    {
        setLED(NONE);

        bluetooth.baud(9600);

        if (this->USERisPressed())
        {
            *((unsigned long *) 0x2001FFFC) = 0xB00710AD;
            NVIC_SystemReset();
        }

        wait_ms(1);
    }

    void enableBluetooth(void)
    {
        Reset.write(1);
    }

    void enableIMU(uint8_t mAscale = AFS_2G, uint8_t ACCBW  = ABW_125Hz,
                uint8_t mGscale = GFS_125DPS, uint8_t GODRBW = G_200Hz23Hz,
                uint8_t Mmode  = Regular, uint8_t MODR   = MODR_30Hz)
    {
        imu.init(mAscale, ACCBW, mGscale, GODRBW, Mmode, MODR);
    }

    int USERisPressed(void)
    {
        return USER;
    }

    float getBattery(void)
    {
        float vBat = battery.read();

        return (vBat * 3.3f * BATTERY_SCALE);
    }

    void setLED(PwmOut led, float intensity)
    {
        if (intensity > 1.0f)
        {
            intensity = 1.0f;
        }
        else if (intensity < 0.0f)
        {
            intensity = 0.0f;
        }

        led = 1.0f - intensity;
    }

    void setLED(LEDType_t color)
    {
        setLED(ledRED, OFF);
        setLED(ledGREEN, OFF);
        setLED(ledBLUE, OFF);

        switch(color)
        {
            case RED:
                setLED(ledRED, ON);
                break;

            case GREEN:
                setLED(ledGREEN, ON);
                break;

            case BLUE:
                setLED(ledBLUE, ON);
                break;

            case YELLOW:
                setLED(ledRED, ON);
                setLED(ledGREEN, ON);
                break;

            case CYAN:
                setLED(ledGREEN, ON);
                setLED(ledBLUE, ON);
                break;

            case MAGENTA:
                setLED(ledRED, ON);
                setLED(ledBLUE, ON);
                break;

            case WHITE:
                setLED(ledRED, ON);
                setLED(ledGREEN, ON);
                setLED(ledBLUE, ON);
                break;

            case NONE:
                break;

            default:
                break;
        }
    }

    // code: HTML Color Code (without #) - Examples: "00ff00", "70befc"
    void setLED(const char * code)
    {
        int hexValue = strtol(code, NULL, 16);

        float r = ((hexValue >> 16) & 0xFF) / 255.0;  // Extract the RR byte
        float g = ((hexValue >> 8) & 0xFF) / 255.0;   // Extract the GG byte
        float b = ((hexValue) & 0xFF) / 255.0;        // Extract the BB byte

        setLED(ledRED, r);
        setLED(ledGREEN, g);
        setLED(ledBLUE, b);
    }

    void toggleLED(LEDType_t color)
    {
        switch(color)
        {
            case RED:
            ledRED = !ledRED;
            break;

            case GREEN:
            ledGREEN = !ledGREEN;
            break;

            case BLUE:
            ledBLUE = !ledBLUE;
            break;

            default:
            break;
        }
    }
};

Ioton ton;

#endif // IOTON_H