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

Committer:
krebyy
Date:
Tue Dec 20 13:50:15 2016 +0000
Revision:
2:b3c3bf0b9101
Parent:
0:cbba28a205fa
Child:
3:9c7195d31602
IOTON-API using ESP8266Interface Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krebyy 0:cbba28a205fa 1 /* Ioton Boards Library
krebyy 0:cbba28a205fa 2 * Copyright (c) 2016 Ioton Technology
krebyy 0:cbba28a205fa 3 *
krebyy 0:cbba28a205fa 4 * Licensed under the Apache License, Version 2.0 (the "License");
krebyy 0:cbba28a205fa 5 * you may not use this file except in compliance with the License.
krebyy 0:cbba28a205fa 6 * You may obtain a copy of the License at
krebyy 0:cbba28a205fa 7 *
krebyy 0:cbba28a205fa 8 * http://www.apache.org/licenses/LICENSE-2.0
krebyy 0:cbba28a205fa 9 *
krebyy 0:cbba28a205fa 10 * Unless required by applicable law or agreed to in writing, software
krebyy 0:cbba28a205fa 11 * distributed under the License is distributed on an "AS IS" BASIS,
krebyy 0:cbba28a205fa 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
krebyy 0:cbba28a205fa 13 * See the License for the specific language governing permissions and
krebyy 0:cbba28a205fa 14 * limitations under the License.
krebyy 0:cbba28a205fa 15 */
krebyy 0:cbba28a205fa 16
krebyy 0:cbba28a205fa 17 #ifndef IOTON_H
krebyy 0:cbba28a205fa 18 #define IOTON_H
krebyy 0:cbba28a205fa 19
krebyy 0:cbba28a205fa 20 #include "mbed.h"
krebyy 0:cbba28a205fa 21 #include "BMX055.h"
krebyy 0:cbba28a205fa 22 #include "USBSerial.h"
krebyy 2:b3c3bf0b9101 23 #include "ESP8266Interface.h"
krebyy 2:b3c3bf0b9101 24 #include "MQTTESP8266.h"
krebyy 2:b3c3bf0b9101 25 #include "MQTTClient.h"
krebyy 0:cbba28a205fa 26
krebyy 0:cbba28a205fa 27 #define ON 1
krebyy 0:cbba28a205fa 28 #define OFF 0
krebyy 0:cbba28a205fa 29 #define BATTERY_SCALE 1.4681f // Voltage divider: [(R14 + R15) / R15]
krebyy 0:cbba28a205fa 30
krebyy 0:cbba28a205fa 31 // Generic signals namings
krebyy 0:cbba28a205fa 32 #define LED1 PB_0
krebyy 0:cbba28a205fa 33 #define LED2 PB_5
krebyy 0:cbba28a205fa 34 #define LED3 PB_4
krebyy 0:cbba28a205fa 35 #define USBTX PC_6 /* USART6 */
krebyy 0:cbba28a205fa 36 #define USBRX PC_7
krebyy 0:cbba28a205fa 37 #define I2C_SCL PA_8 /* I2C3 */
krebyy 0:cbba28a205fa 38 #define I2C_SDA PC_9
krebyy 0:cbba28a205fa 39 #define SPI_MOSI PA_7 /* SPI3 */
krebyy 0:cbba28a205fa 40 #define SPI_MISO PA_6
krebyy 0:cbba28a205fa 41 #define SPI_SCK PA_5
krebyy 0:cbba28a205fa 42 #define SPI_CS PA_4
krebyy 0:cbba28a205fa 43
krebyy 0:cbba28a205fa 44 // Ton Board signals namings
krebyy 0:cbba28a205fa 45 #define LED_RED PB_0
krebyy 0:cbba28a205fa 46 #define LED_GREEN PB_5
krebyy 0:cbba28a205fa 47 #define LED_BLUE PB_4
krebyy 0:cbba28a205fa 48 #define SW_USER PC_13
krebyy 0:cbba28a205fa 49 #define IMU_SCL PB_10 /* I2C2 */
krebyy 0:cbba28a205fa 50 #define IMU_SDA PB_11
krebyy 2:b3c3bf0b9101 51 #define BT_TX PA_9 /* USART1 */
krebyy 2:b3c3bf0b9101 52 #define BT_RX PA_10
krebyy 2:b3c3bf0b9101 53 #define WIFI_TX PA_2 /* USART2 */
krebyy 2:b3c3bf0b9101 54 #define WIFI_RX PA_3
krebyy 2:b3c3bf0b9101 55 #define WIFI_PWD PB_12
krebyy 2:b3c3bf0b9101 56 #define WIFI_MODE PB_13
krebyy 2:b3c3bf0b9101 57 #define WIFI_RST PC_15
krebyy 2:b3c3bf0b9101 58
krebyy 0:cbba28a205fa 59
krebyy 0:cbba28a205fa 60 #define PIN0 PC_6
krebyy 0:cbba28a205fa 61 #define PIN1 PC_7
krebyy 0:cbba28a205fa 62 #define PIN2 PC_8
krebyy 0:cbba28a205fa 63 #define PIN3 PC_9
krebyy 0:cbba28a205fa 64 #define PIN4 PA_8
krebyy 0:cbba28a205fa 65 #define PIN5 PC_10
krebyy 0:cbba28a205fa 66 #define PIN6 PC_11
krebyy 0:cbba28a205fa 67 #define PIN7 PC_12
krebyy 0:cbba28a205fa 68 #define PIN8 PD_2
krebyy 0:cbba28a205fa 69 #define PIN9 PB_6
krebyy 0:cbba28a205fa 70 #define PIN10 PB_7
krebyy 0:cbba28a205fa 71 #define PIN11 PB_8
krebyy 0:cbba28a205fa 72 #define PIN12 PB_9
krebyy 0:cbba28a205fa 73 #define PIN13 PB_3
krebyy 0:cbba28a205fa 74 #define PIN14 PA_15
krebyy 0:cbba28a205fa 75 #define PIN15 PC_0
krebyy 0:cbba28a205fa 76 #define PIN16 PC_1
krebyy 0:cbba28a205fa 77 #define PIN17 PC_2
krebyy 0:cbba28a205fa 78 #define PIN18 PC_3
krebyy 0:cbba28a205fa 79 #define PIN19 PA_0
krebyy 0:cbba28a205fa 80 #define PIN20 PA_1
krebyy 0:cbba28a205fa 81 #define PIN21 PA_4
krebyy 0:cbba28a205fa 82 #define PIN22 PA_5
krebyy 0:cbba28a205fa 83 #define PIN23 PA_6
krebyy 0:cbba28a205fa 84 #define PIN24 PA_7
krebyy 0:cbba28a205fa 85 #define PIN25 PC_4
krebyy 0:cbba28a205fa 86 #define PIN26 PC_5
krebyy 0:cbba28a205fa 87 #define PIN27 PA_2
krebyy 0:cbba28a205fa 88 #define PIN28 PA_3
krebyy 0:cbba28a205fa 89 #define PIN29 PA_9
krebyy 0:cbba28a205fa 90 #define PIN30 PA_10
krebyy 0:cbba28a205fa 91
krebyy 0:cbba28a205fa 92 typedef enum
krebyy 0:cbba28a205fa 93 {
krebyy 0:cbba28a205fa 94 RED,
krebyy 0:cbba28a205fa 95 GREEN,
krebyy 0:cbba28a205fa 96 BLUE,
krebyy 0:cbba28a205fa 97 YELLOW,
krebyy 0:cbba28a205fa 98 CYAN,
krebyy 0:cbba28a205fa 99 MAGENTA,
krebyy 0:cbba28a205fa 100 WHITE,
krebyy 0:cbba28a205fa 101 NONE
krebyy 0:cbba28a205fa 102 } LEDType_t;
krebyy 0:cbba28a205fa 103
krebyy 0:cbba28a205fa 104
krebyy 2:b3c3bf0b9101 105 Serial bluetooth(BT_TX, BT_RX);
krebyy 0:cbba28a205fa 106 USBSerial usb(0x1f00, 0x2012, 0x0001, false);
krebyy 2:b3c3bf0b9101 107 MQTTESP8266 wifi(WIFI_TX, WIFI_RX, WIFI_RST, WIFI_MODE, WIFI_PWD, "defualtssid", "defaultpass");
krebyy 2:b3c3bf0b9101 108 DigitalOut Reset(WIFI_RST);
krebyy 0:cbba28a205fa 109
krebyy 0:cbba28a205fa 110 DigitalIn USER(SW_USER);
krebyy 0:cbba28a205fa 111 PwmOut ledRED(LED_RED);
krebyy 0:cbba28a205fa 112 PwmOut ledGREEN(LED_GREEN);
krebyy 0:cbba28a205fa 113 PwmOut ledBLUE(LED_BLUE);
krebyy 0:cbba28a205fa 114 AnalogIn battery(PB_1);
krebyy 0:cbba28a205fa 115
krebyy 0:cbba28a205fa 116 BMX055 imu;
krebyy 0:cbba28a205fa 117
krebyy 0:cbba28a205fa 118
krebyy 0:cbba28a205fa 119 class Ioton
krebyy 0:cbba28a205fa 120 {
krebyy 0:cbba28a205fa 121 public:
krebyy 0:cbba28a205fa 122 Ioton()
krebyy 0:cbba28a205fa 123 {
krebyy 0:cbba28a205fa 124 setLED(NONE);
krebyy 0:cbba28a205fa 125
krebyy 0:cbba28a205fa 126 bluetooth.baud(9600);
krebyy 0:cbba28a205fa 127
krebyy 0:cbba28a205fa 128 if (this->USERisPressed())
krebyy 0:cbba28a205fa 129 {
krebyy 0:cbba28a205fa 130 *((unsigned long *) 0x2001FFFC) = 0xB00710AD;
krebyy 0:cbba28a205fa 131 NVIC_SystemReset();
krebyy 0:cbba28a205fa 132 }
krebyy 0:cbba28a205fa 133
krebyy 0:cbba28a205fa 134 wait_ms(1);
krebyy 0:cbba28a205fa 135 }
krebyy 0:cbba28a205fa 136
krebyy 0:cbba28a205fa 137 void enableBluetooth(void)
krebyy 0:cbba28a205fa 138 {
krebyy 0:cbba28a205fa 139 Reset.write(1);
krebyy 0:cbba28a205fa 140 }
krebyy 0:cbba28a205fa 141
krebyy 0:cbba28a205fa 142 void enableIMU(uint8_t mAscale = AFS_2G, uint8_t ACCBW = ABW_125Hz,
krebyy 0:cbba28a205fa 143 uint8_t mGscale = GFS_125DPS, uint8_t GODRBW = G_200Hz23Hz,
krebyy 0:cbba28a205fa 144 uint8_t Mmode = Regular, uint8_t MODR = MODR_30Hz)
krebyy 0:cbba28a205fa 145 {
krebyy 0:cbba28a205fa 146 imu.init(mAscale, ACCBW, mGscale, GODRBW, Mmode, MODR);
krebyy 0:cbba28a205fa 147 }
krebyy 0:cbba28a205fa 148
krebyy 0:cbba28a205fa 149 int USERisPressed(void)
krebyy 0:cbba28a205fa 150 {
krebyy 0:cbba28a205fa 151 return USER;
krebyy 0:cbba28a205fa 152 }
krebyy 0:cbba28a205fa 153
krebyy 0:cbba28a205fa 154 float getBattery(void)
krebyy 0:cbba28a205fa 155 {
krebyy 0:cbba28a205fa 156 float vBat = battery.read();
krebyy 0:cbba28a205fa 157
krebyy 0:cbba28a205fa 158 return (vBat * 3.3f * BATTERY_SCALE);
krebyy 0:cbba28a205fa 159 }
krebyy 0:cbba28a205fa 160
krebyy 0:cbba28a205fa 161 void setLED(PwmOut led, float intensity)
krebyy 0:cbba28a205fa 162 {
krebyy 0:cbba28a205fa 163 if (intensity > 1.0f)
krebyy 0:cbba28a205fa 164 {
krebyy 0:cbba28a205fa 165 intensity = 1.0f;
krebyy 0:cbba28a205fa 166 }
krebyy 0:cbba28a205fa 167 else if (intensity < 0.0f)
krebyy 0:cbba28a205fa 168 {
krebyy 0:cbba28a205fa 169 intensity = 0.0f;
krebyy 0:cbba28a205fa 170 }
krebyy 0:cbba28a205fa 171
krebyy 0:cbba28a205fa 172 led = 1.0f - intensity;
krebyy 0:cbba28a205fa 173 }
krebyy 0:cbba28a205fa 174
krebyy 0:cbba28a205fa 175 void setLED(LEDType_t color)
krebyy 0:cbba28a205fa 176 {
krebyy 0:cbba28a205fa 177 setLED(ledRED, OFF);
krebyy 0:cbba28a205fa 178 setLED(ledGREEN, OFF);
krebyy 0:cbba28a205fa 179 setLED(ledBLUE, OFF);
krebyy 0:cbba28a205fa 180
krebyy 0:cbba28a205fa 181 switch(color)
krebyy 0:cbba28a205fa 182 {
krebyy 0:cbba28a205fa 183 case RED:
krebyy 0:cbba28a205fa 184 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 185 break;
krebyy 0:cbba28a205fa 186
krebyy 0:cbba28a205fa 187 case GREEN:
krebyy 0:cbba28a205fa 188 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 189 break;
krebyy 0:cbba28a205fa 190
krebyy 0:cbba28a205fa 191 case BLUE:
krebyy 0:cbba28a205fa 192 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 193 break;
krebyy 0:cbba28a205fa 194
krebyy 0:cbba28a205fa 195 case YELLOW:
krebyy 0:cbba28a205fa 196 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 197 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 198 break;
krebyy 0:cbba28a205fa 199
krebyy 0:cbba28a205fa 200 case CYAN:
krebyy 0:cbba28a205fa 201 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 202 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 203 break;
krebyy 0:cbba28a205fa 204
krebyy 0:cbba28a205fa 205 case MAGENTA:
krebyy 0:cbba28a205fa 206 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 207 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 208 break;
krebyy 0:cbba28a205fa 209
krebyy 0:cbba28a205fa 210 case WHITE:
krebyy 0:cbba28a205fa 211 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 212 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 213 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 214 break;
krebyy 0:cbba28a205fa 215
krebyy 0:cbba28a205fa 216 case NONE:
krebyy 0:cbba28a205fa 217 break;
krebyy 0:cbba28a205fa 218
krebyy 0:cbba28a205fa 219 default:
krebyy 0:cbba28a205fa 220 break;
krebyy 0:cbba28a205fa 221 }
krebyy 0:cbba28a205fa 222 }
krebyy 0:cbba28a205fa 223
krebyy 0:cbba28a205fa 224 // code: HTML Color Code (without #) - Examples: "00ff00", "70befc"
krebyy 0:cbba28a205fa 225 void setLED(const char * code)
krebyy 0:cbba28a205fa 226 {
krebyy 0:cbba28a205fa 227 int hexValue = strtol(code, NULL, 16);
krebyy 0:cbba28a205fa 228
krebyy 0:cbba28a205fa 229 float r = ((hexValue >> 16) & 0xFF) / 255.0; // Extract the RR byte
krebyy 0:cbba28a205fa 230 float g = ((hexValue >> 8) & 0xFF) / 255.0; // Extract the GG byte
krebyy 0:cbba28a205fa 231 float b = ((hexValue) & 0xFF) / 255.0; // Extract the BB byte
krebyy 0:cbba28a205fa 232
krebyy 0:cbba28a205fa 233 setLED(ledRED, r);
krebyy 0:cbba28a205fa 234 setLED(ledGREEN, g);
krebyy 0:cbba28a205fa 235 setLED(ledBLUE, b);
krebyy 0:cbba28a205fa 236 }
krebyy 0:cbba28a205fa 237
krebyy 0:cbba28a205fa 238 void toggleLED(LEDType_t color)
krebyy 0:cbba28a205fa 239 {
krebyy 0:cbba28a205fa 240 switch(color)
krebyy 0:cbba28a205fa 241 {
krebyy 0:cbba28a205fa 242 case RED:
krebyy 0:cbba28a205fa 243 ledRED = !ledRED;
krebyy 0:cbba28a205fa 244 break;
krebyy 0:cbba28a205fa 245
krebyy 0:cbba28a205fa 246 case GREEN:
krebyy 0:cbba28a205fa 247 ledGREEN = !ledGREEN;
krebyy 0:cbba28a205fa 248 break;
krebyy 0:cbba28a205fa 249
krebyy 0:cbba28a205fa 250 case BLUE:
krebyy 0:cbba28a205fa 251 ledBLUE = !ledBLUE;
krebyy 0:cbba28a205fa 252 break;
krebyy 0:cbba28a205fa 253
krebyy 0:cbba28a205fa 254 default:
krebyy 0:cbba28a205fa 255 break;
krebyy 0:cbba28a205fa 256 }
krebyy 0:cbba28a205fa 257 }
krebyy 0:cbba28a205fa 258 };
krebyy 0:cbba28a205fa 259
krebyy 0:cbba28a205fa 260 Ioton ton;
krebyy 0:cbba28a205fa 261
krebyy 0:cbba28a205fa 262 #endif // IOTON_H