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 Nov 08 00:17:12 2016 +0000
Revision:
0:cbba28a205fa
Child:
1:3a73d77c2cef
Child:
2:b3c3bf0b9101
First commit - to use with the mbed online compiler

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 "ESP8266.h"
krebyy 0:cbba28a205fa 23 #include "USBSerial.h"
krebyy 0:cbba28a205fa 24
krebyy 0:cbba28a205fa 25 #define ON 1
krebyy 0:cbba28a205fa 26 #define OFF 0
krebyy 0:cbba28a205fa 27 #define BATTERY_SCALE 1.4681f // Voltage divider: [(R14 + R15) / R15]
krebyy 0:cbba28a205fa 28
krebyy 0:cbba28a205fa 29 // Generic signals namings
krebyy 0:cbba28a205fa 30 #define LED1 PB_0
krebyy 0:cbba28a205fa 31 #define LED2 PB_5
krebyy 0:cbba28a205fa 32 #define LED3 PB_4
krebyy 0:cbba28a205fa 33 #define USBTX PC_6 /* USART6 */
krebyy 0:cbba28a205fa 34 #define USBRX PC_7
krebyy 0:cbba28a205fa 35 #define I2C_SCL PA_8 /* I2C3 */
krebyy 0:cbba28a205fa 36 #define I2C_SDA PC_9
krebyy 0:cbba28a205fa 37 #define SPI_MOSI PA_7 /* SPI3 */
krebyy 0:cbba28a205fa 38 #define SPI_MISO PA_6
krebyy 0:cbba28a205fa 39 #define SPI_SCK PA_5
krebyy 0:cbba28a205fa 40 #define SPI_CS PA_4
krebyy 0:cbba28a205fa 41
krebyy 0:cbba28a205fa 42 // Ton Board signals namings
krebyy 0:cbba28a205fa 43 #define LED_RED PB_0
krebyy 0:cbba28a205fa 44 #define LED_GREEN PB_5
krebyy 0:cbba28a205fa 45 #define LED_BLUE PB_4
krebyy 0:cbba28a205fa 46 #define SW_USER PC_13
krebyy 0:cbba28a205fa 47 #define IMU_SCL PB_10 /* I2C2 */
krebyy 0:cbba28a205fa 48 #define IMU_SDA PB_11
krebyy 0:cbba28a205fa 49 #define BTTX PA_9 /* USART1 */
krebyy 0:cbba28a205fa 50 #define BTRX PA_10
krebyy 0:cbba28a205fa 51 #define WIFITX PA_2 /* USART2 */
krebyy 0:cbba28a205fa 52 #define WIFIRX PA_3
krebyy 0:cbba28a205fa 53
krebyy 0:cbba28a205fa 54 #define PIN0 PC_6
krebyy 0:cbba28a205fa 55 #define PIN1 PC_7
krebyy 0:cbba28a205fa 56 #define PIN2 PC_8
krebyy 0:cbba28a205fa 57 #define PIN3 PC_9
krebyy 0:cbba28a205fa 58 #define PIN4 PA_8
krebyy 0:cbba28a205fa 59 #define PIN5 PC_10
krebyy 0:cbba28a205fa 60 #define PIN6 PC_11
krebyy 0:cbba28a205fa 61 #define PIN7 PC_12
krebyy 0:cbba28a205fa 62 #define PIN8 PD_2
krebyy 0:cbba28a205fa 63 #define PIN9 PB_6
krebyy 0:cbba28a205fa 64 #define PIN10 PB_7
krebyy 0:cbba28a205fa 65 #define PIN11 PB_8
krebyy 0:cbba28a205fa 66 #define PIN12 PB_9
krebyy 0:cbba28a205fa 67 #define PIN13 PB_3
krebyy 0:cbba28a205fa 68 #define PIN14 PA_15
krebyy 0:cbba28a205fa 69 #define PIN15 PC_0
krebyy 0:cbba28a205fa 70 #define PIN16 PC_1
krebyy 0:cbba28a205fa 71 #define PIN17 PC_2
krebyy 0:cbba28a205fa 72 #define PIN18 PC_3
krebyy 0:cbba28a205fa 73 #define PIN19 PA_0
krebyy 0:cbba28a205fa 74 #define PIN20 PA_1
krebyy 0:cbba28a205fa 75 #define PIN21 PA_4
krebyy 0:cbba28a205fa 76 #define PIN22 PA_5
krebyy 0:cbba28a205fa 77 #define PIN23 PA_6
krebyy 0:cbba28a205fa 78 #define PIN24 PA_7
krebyy 0:cbba28a205fa 79 #define PIN25 PC_4
krebyy 0:cbba28a205fa 80 #define PIN26 PC_5
krebyy 0:cbba28a205fa 81 #define PIN27 PA_2
krebyy 0:cbba28a205fa 82 #define PIN28 PA_3
krebyy 0:cbba28a205fa 83 #define PIN29 PA_9
krebyy 0:cbba28a205fa 84 #define PIN30 PA_10
krebyy 0:cbba28a205fa 85
krebyy 0:cbba28a205fa 86 typedef enum
krebyy 0:cbba28a205fa 87 {
krebyy 0:cbba28a205fa 88 RED,
krebyy 0:cbba28a205fa 89 GREEN,
krebyy 0:cbba28a205fa 90 BLUE,
krebyy 0:cbba28a205fa 91 YELLOW,
krebyy 0:cbba28a205fa 92 CYAN,
krebyy 0:cbba28a205fa 93 MAGENTA,
krebyy 0:cbba28a205fa 94 WHITE,
krebyy 0:cbba28a205fa 95 NONE
krebyy 0:cbba28a205fa 96 } LEDType_t;
krebyy 0:cbba28a205fa 97
krebyy 0:cbba28a205fa 98
krebyy 0:cbba28a205fa 99 Serial bluetooth(PA_9, PA_10);
krebyy 0:cbba28a205fa 100 USBSerial usb(0x1f00, 0x2012, 0x0001, false);
krebyy 0:cbba28a205fa 101 ESP8266 wifi(PA_2, PA_3);
krebyy 0:cbba28a205fa 102 DigitalOut PWD(PB_12);
krebyy 0:cbba28a205fa 103 DigitalOut Mode(PB_13);
krebyy 0:cbba28a205fa 104 DigitalOut Reset(PC_15);
krebyy 0:cbba28a205fa 105
krebyy 0:cbba28a205fa 106 DigitalIn USER(SW_USER);
krebyy 0:cbba28a205fa 107 PwmOut ledRED(LED_RED);
krebyy 0:cbba28a205fa 108 PwmOut ledGREEN(LED_GREEN);
krebyy 0:cbba28a205fa 109 PwmOut ledBLUE(LED_BLUE);
krebyy 0:cbba28a205fa 110 AnalogIn battery(PB_1);
krebyy 0:cbba28a205fa 111
krebyy 0:cbba28a205fa 112 BMX055 imu;
krebyy 0:cbba28a205fa 113
krebyy 0:cbba28a205fa 114
krebyy 0:cbba28a205fa 115 class Ioton
krebyy 0:cbba28a205fa 116 {
krebyy 0:cbba28a205fa 117 public:
krebyy 0:cbba28a205fa 118 Ioton()
krebyy 0:cbba28a205fa 119 {
krebyy 0:cbba28a205fa 120 setLED(NONE);
krebyy 0:cbba28a205fa 121
krebyy 0:cbba28a205fa 122 bluetooth.baud(9600);
krebyy 0:cbba28a205fa 123
krebyy 0:cbba28a205fa 124 PWD.write(0);
krebyy 0:cbba28a205fa 125 Mode.write(1);
krebyy 0:cbba28a205fa 126 Reset.write(0);
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 void enableWifi(void)
krebyy 0:cbba28a205fa 150 {
krebyy 0:cbba28a205fa 151 PWD.write(1);
krebyy 0:cbba28a205fa 152 Reset.write(1);
krebyy 0:cbba28a205fa 153 }
krebyy 0:cbba28a205fa 154
krebyy 0:cbba28a205fa 155 int USERisPressed(void)
krebyy 0:cbba28a205fa 156 {
krebyy 0:cbba28a205fa 157 return USER;
krebyy 0:cbba28a205fa 158 }
krebyy 0:cbba28a205fa 159
krebyy 0:cbba28a205fa 160 float getBattery(void)
krebyy 0:cbba28a205fa 161 {
krebyy 0:cbba28a205fa 162 float vBat = battery.read();
krebyy 0:cbba28a205fa 163
krebyy 0:cbba28a205fa 164 return (vBat * 3.3f * BATTERY_SCALE);
krebyy 0:cbba28a205fa 165 }
krebyy 0:cbba28a205fa 166
krebyy 0:cbba28a205fa 167 void setLED(PwmOut led, float intensity)
krebyy 0:cbba28a205fa 168 {
krebyy 0:cbba28a205fa 169 if (intensity > 1.0f)
krebyy 0:cbba28a205fa 170 {
krebyy 0:cbba28a205fa 171 intensity = 1.0f;
krebyy 0:cbba28a205fa 172 }
krebyy 0:cbba28a205fa 173 else if (intensity < 0.0f)
krebyy 0:cbba28a205fa 174 {
krebyy 0:cbba28a205fa 175 intensity = 0.0f;
krebyy 0:cbba28a205fa 176 }
krebyy 0:cbba28a205fa 177
krebyy 0:cbba28a205fa 178 led = 1.0f - intensity;
krebyy 0:cbba28a205fa 179 }
krebyy 0:cbba28a205fa 180
krebyy 0:cbba28a205fa 181 void setLED(LEDType_t color)
krebyy 0:cbba28a205fa 182 {
krebyy 0:cbba28a205fa 183 setLED(ledRED, OFF);
krebyy 0:cbba28a205fa 184 setLED(ledGREEN, OFF);
krebyy 0:cbba28a205fa 185 setLED(ledBLUE, OFF);
krebyy 0:cbba28a205fa 186
krebyy 0:cbba28a205fa 187 switch(color)
krebyy 0:cbba28a205fa 188 {
krebyy 0:cbba28a205fa 189 case RED:
krebyy 0:cbba28a205fa 190 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 191 break;
krebyy 0:cbba28a205fa 192
krebyy 0:cbba28a205fa 193 case GREEN:
krebyy 0:cbba28a205fa 194 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 195 break;
krebyy 0:cbba28a205fa 196
krebyy 0:cbba28a205fa 197 case BLUE:
krebyy 0:cbba28a205fa 198 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 199 break;
krebyy 0:cbba28a205fa 200
krebyy 0:cbba28a205fa 201 case YELLOW:
krebyy 0:cbba28a205fa 202 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 203 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 204 break;
krebyy 0:cbba28a205fa 205
krebyy 0:cbba28a205fa 206 case CYAN:
krebyy 0:cbba28a205fa 207 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 208 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 209 break;
krebyy 0:cbba28a205fa 210
krebyy 0:cbba28a205fa 211 case MAGENTA:
krebyy 0:cbba28a205fa 212 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 213 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 214 break;
krebyy 0:cbba28a205fa 215
krebyy 0:cbba28a205fa 216 case WHITE:
krebyy 0:cbba28a205fa 217 setLED(ledRED, ON);
krebyy 0:cbba28a205fa 218 setLED(ledGREEN, ON);
krebyy 0:cbba28a205fa 219 setLED(ledBLUE, ON);
krebyy 0:cbba28a205fa 220 break;
krebyy 0:cbba28a205fa 221
krebyy 0:cbba28a205fa 222 case NONE:
krebyy 0:cbba28a205fa 223 break;
krebyy 0:cbba28a205fa 224
krebyy 0:cbba28a205fa 225 default:
krebyy 0:cbba28a205fa 226 break;
krebyy 0:cbba28a205fa 227 }
krebyy 0:cbba28a205fa 228 }
krebyy 0:cbba28a205fa 229
krebyy 0:cbba28a205fa 230 // code: HTML Color Code (without #) - Examples: "00ff00", "70befc"
krebyy 0:cbba28a205fa 231 void setLED(const char * code)
krebyy 0:cbba28a205fa 232 {
krebyy 0:cbba28a205fa 233 int hexValue = strtol(code, NULL, 16);
krebyy 0:cbba28a205fa 234
krebyy 0:cbba28a205fa 235 float r = ((hexValue >> 16) & 0xFF) / 255.0; // Extract the RR byte
krebyy 0:cbba28a205fa 236 float g = ((hexValue >> 8) & 0xFF) / 255.0; // Extract the GG byte
krebyy 0:cbba28a205fa 237 float b = ((hexValue) & 0xFF) / 255.0; // Extract the BB byte
krebyy 0:cbba28a205fa 238
krebyy 0:cbba28a205fa 239 setLED(ledRED, r);
krebyy 0:cbba28a205fa 240 setLED(ledGREEN, g);
krebyy 0:cbba28a205fa 241 setLED(ledBLUE, b);
krebyy 0:cbba28a205fa 242 }
krebyy 0:cbba28a205fa 243
krebyy 0:cbba28a205fa 244 void toggleLED(LEDType_t color)
krebyy 0:cbba28a205fa 245 {
krebyy 0:cbba28a205fa 246 switch(color)
krebyy 0:cbba28a205fa 247 {
krebyy 0:cbba28a205fa 248 case RED:
krebyy 0:cbba28a205fa 249 ledRED = !ledRED;
krebyy 0:cbba28a205fa 250 break;
krebyy 0:cbba28a205fa 251
krebyy 0:cbba28a205fa 252 case GREEN:
krebyy 0:cbba28a205fa 253 ledGREEN = !ledGREEN;
krebyy 0:cbba28a205fa 254 break;
krebyy 0:cbba28a205fa 255
krebyy 0:cbba28a205fa 256 case BLUE:
krebyy 0:cbba28a205fa 257 ledBLUE = !ledBLUE;
krebyy 0:cbba28a205fa 258 break;
krebyy 0:cbba28a205fa 259
krebyy 0:cbba28a205fa 260 default:
krebyy 0:cbba28a205fa 261 break;
krebyy 0:cbba28a205fa 262 }
krebyy 0:cbba28a205fa 263 }
krebyy 0:cbba28a205fa 264 };
krebyy 0:cbba28a205fa 265
krebyy 0:cbba28a205fa 266 Ioton ton;
krebyy 0:cbba28a205fa 267
krebyy 0:cbba28a205fa 268 #endif // IOTON_H