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
Ioton.h@1:3a73d77c2cef, 2017-06-29 (annotated)
- Committer:
- krebyy
- Date:
- Thu Jun 29 20:11:00 2017 +0000
- Revision:
- 1:3a73d77c2cef
- Parent:
- 0:cbba28a205fa
- Child:
- 3:9c7195d31602
Update to TON Board V1.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krebyy | 0:cbba28a205fa | 1 | /* Ioton Boards Library |
krebyy | 1:3a73d77c2cef | 2 | * Copyright (c) 2016-2017 Ioton Technology |
krebyy | 1:3a73d77c2cef | 3 | * |
krebyy | 1:3a73d77c2cef | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
krebyy | 1:3a73d77c2cef | 5 | * you may not use this file except in compliance with the License. |
krebyy | 1:3a73d77c2cef | 6 | * You may obtain a copy of the License at |
krebyy | 1:3a73d77c2cef | 7 | * |
krebyy | 1:3a73d77c2cef | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
krebyy | 1:3a73d77c2cef | 9 | * |
krebyy | 1:3a73d77c2cef | 10 | * Unless required by applicable law or agreed to in writing, software |
krebyy | 1:3a73d77c2cef | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
krebyy | 1:3a73d77c2cef | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
krebyy | 1:3a73d77c2cef | 13 | * See the License for the specific language governing permissions and |
krebyy | 1:3a73d77c2cef | 14 | * limitations under the License. |
krebyy | 1:3a73d77c2cef | 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 | 1:3a73d77c2cef | 23 | #include "USBTon.h" |
krebyy | 0:cbba28a205fa | 24 | |
krebyy | 0:cbba28a205fa | 25 | #define ON 1 |
krebyy | 0:cbba28a205fa | 26 | #define OFF 0 |
krebyy | 1:3a73d77c2cef | 27 | #define BATTERY_SCALE 1.4681f /* Voltage divider: [(R18 + R19) / R19] */ |
krebyy | 1:3a73d77c2cef | 28 | #define BATTERY_MIN 3.6f |
krebyy | 1:3a73d77c2cef | 29 | #define BATTERY_MAX 4.2f |
krebyy | 0:cbba28a205fa | 30 | |
krebyy | 0:cbba28a205fa | 31 | typedef enum |
krebyy | 0:cbba28a205fa | 32 | { |
krebyy | 1:3a73d77c2cef | 33 | RED, |
krebyy | 1:3a73d77c2cef | 34 | GREEN, |
krebyy | 1:3a73d77c2cef | 35 | BLUE, |
krebyy | 1:3a73d77c2cef | 36 | YELLOW, |
krebyy | 1:3a73d77c2cef | 37 | CYAN, |
krebyy | 1:3a73d77c2cef | 38 | MAGENTA, |
krebyy | 1:3a73d77c2cef | 39 | WHITE, |
krebyy | 1:3a73d77c2cef | 40 | NONE |
krebyy | 0:cbba28a205fa | 41 | } LEDType_t; |
krebyy | 0:cbba28a205fa | 42 | |
krebyy | 0:cbba28a205fa | 43 | |
krebyy | 1:3a73d77c2cef | 44 | Serial bluetooth(BT_TX, BT_RX); |
krebyy | 1:3a73d77c2cef | 45 | ESP8266 wifi(WIFI_TX, WIFI_RX); |
krebyy | 1:3a73d77c2cef | 46 | DigitalOut BT_RST(PC_15); |
krebyy | 1:3a73d77c2cef | 47 | DigitalIn VBUS(PA_9); |
krebyy | 1:3a73d77c2cef | 48 | DigitalOut WIFI_PWD(PB_12); |
krebyy | 1:3a73d77c2cef | 49 | DigitalOut WIFI_MODE(PB_13); |
krebyy | 1:3a73d77c2cef | 50 | DigitalOut WIFI_RST(PC_14); |
krebyy | 0:cbba28a205fa | 51 | |
krebyy | 0:cbba28a205fa | 52 | DigitalIn USER(SW_USER); |
krebyy | 0:cbba28a205fa | 53 | PwmOut ledRED(LED_RED); |
krebyy | 0:cbba28a205fa | 54 | PwmOut ledGREEN(LED_GREEN); |
krebyy | 0:cbba28a205fa | 55 | PwmOut ledBLUE(LED_BLUE); |
krebyy | 1:3a73d77c2cef | 56 | AnalogIn battery(BAT_METER); |
krebyy | 0:cbba28a205fa | 57 | |
krebyy | 0:cbba28a205fa | 58 | BMX055 imu; |
krebyy | 1:3a73d77c2cef | 59 | USBTon usb; |
krebyy | 0:cbba28a205fa | 60 | |
krebyy | 0:cbba28a205fa | 61 | |
krebyy | 0:cbba28a205fa | 62 | class Ioton |
krebyy | 0:cbba28a205fa | 63 | { |
krebyy | 0:cbba28a205fa | 64 | public: |
krebyy | 1:3a73d77c2cef | 65 | Ioton() |
krebyy | 1:3a73d77c2cef | 66 | { |
krebyy | 1:3a73d77c2cef | 67 | setLED(NONE); |
krebyy | 1:3a73d77c2cef | 68 | bluetooth.baud(9600); |
krebyy | 1:3a73d77c2cef | 69 | WIFI_PWD.write(0); |
krebyy | 1:3a73d77c2cef | 70 | WIFI_MODE.write(1); |
krebyy | 1:3a73d77c2cef | 71 | WIFI_RST.write(0); |
krebyy | 1:3a73d77c2cef | 72 | BT_RST.write(0); |
krebyy | 1:3a73d77c2cef | 73 | wait_ms(1); |
krebyy | 0:cbba28a205fa | 74 | |
krebyy | 1:3a73d77c2cef | 75 | if (VBUS) initUSB(); |
krebyy | 1:3a73d77c2cef | 76 | } |
krebyy | 0:cbba28a205fa | 77 | |
krebyy | 1:3a73d77c2cef | 78 | void batteryStatus(bool loop = true) |
krebyy | 1:3a73d77c2cef | 79 | { |
krebyy | 1:3a73d77c2cef | 80 | float batPercertage = 0; |
krebyy | 0:cbba28a205fa | 81 | |
krebyy | 1:3a73d77c2cef | 82 | /* loop = true: forever | loop = false: only once */ |
krebyy | 1:3a73d77c2cef | 83 | while (loop) |
krebyy | 1:3a73d77c2cef | 84 | { |
krebyy | 1:3a73d77c2cef | 85 | batPercertage = ((getBattery() - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)); |
krebyy | 0:cbba28a205fa | 86 | |
krebyy | 1:3a73d77c2cef | 87 | /* GREEN: fully charged; RED: fully discharged; |
krebyy | 1:3a73d77c2cef | 88 | ORANGE/YELLOW: intermediate load */ |
krebyy | 1:3a73d77c2cef | 89 | setLED(ledRED, 1.0f - batPercertage); |
krebyy | 1:3a73d77c2cef | 90 | setLED(ledGREEN, batPercertage); |
krebyy | 1:3a73d77c2cef | 91 | wait(0.5); |
krebyy | 0:cbba28a205fa | 92 | |
krebyy | 1:3a73d77c2cef | 93 | /* BLINK: if it is critically or fully charged */ |
krebyy | 1:3a73d77c2cef | 94 | if (batPercertage > 1.0f || batPercertage < 0.0f) |
krebyy | 1:3a73d77c2cef | 95 | { |
krebyy | 1:3a73d77c2cef | 96 | setLED(NONE); |
krebyy | 1:3a73d77c2cef | 97 | wait(0.5); |
krebyy | 1:3a73d77c2cef | 98 | } |
krebyy | 0:cbba28a205fa | 99 | } |
krebyy | 1:3a73d77c2cef | 100 | } |
krebyy | 0:cbba28a205fa | 101 | |
krebyy | 1:3a73d77c2cef | 102 | void enableBluetooth(void) |
krebyy | 1:3a73d77c2cef | 103 | { |
krebyy | 1:3a73d77c2cef | 104 | BT_RST.write(1); |
krebyy | 1:3a73d77c2cef | 105 | } |
krebyy | 0:cbba28a205fa | 106 | |
krebyy | 1:3a73d77c2cef | 107 | void enableIMU(uint8_t mAscale = AFS_2G, uint8_t ACCBW = ABW_125Hz, |
krebyy | 1:3a73d77c2cef | 108 | uint8_t mGscale = GFS_125DPS, uint8_t GODRBW = G_200Hz23Hz, |
krebyy | 1:3a73d77c2cef | 109 | uint8_t Mmode = Regular, uint8_t MODR = MODR_30Hz) |
krebyy | 1:3a73d77c2cef | 110 | { |
krebyy | 1:3a73d77c2cef | 111 | imu.init(mAscale, ACCBW, mGscale, GODRBW, Mmode, MODR); |
krebyy | 1:3a73d77c2cef | 112 | } |
krebyy | 0:cbba28a205fa | 113 | |
krebyy | 1:3a73d77c2cef | 114 | void enableWifi(void) |
krebyy | 1:3a73d77c2cef | 115 | { |
krebyy | 1:3a73d77c2cef | 116 | WIFI_PWD.write(1); |
krebyy | 1:3a73d77c2cef | 117 | WIFI_RST.write(1); |
krebyy | 1:3a73d77c2cef | 118 | } |
krebyy | 0:cbba28a205fa | 119 | |
krebyy | 1:3a73d77c2cef | 120 | int USERisPressed(void) |
krebyy | 1:3a73d77c2cef | 121 | { |
krebyy | 1:3a73d77c2cef | 122 | return USER; |
krebyy | 1:3a73d77c2cef | 123 | } |
krebyy | 0:cbba28a205fa | 124 | |
krebyy | 1:3a73d77c2cef | 125 | float getBattery(void) |
krebyy | 1:3a73d77c2cef | 126 | { |
krebyy | 1:3a73d77c2cef | 127 | float vBat = battery.read(); |
krebyy | 1:3a73d77c2cef | 128 | |
krebyy | 1:3a73d77c2cef | 129 | return (vBat * 3.3f * BATTERY_SCALE); |
krebyy | 1:3a73d77c2cef | 130 | } |
krebyy | 1:3a73d77c2cef | 131 | |
krebyy | 1:3a73d77c2cef | 132 | void setLED(PwmOut led, float intensity) |
krebyy | 1:3a73d77c2cef | 133 | { |
krebyy | 1:3a73d77c2cef | 134 | if (intensity > 1.0f) |
krebyy | 0:cbba28a205fa | 135 | { |
krebyy | 1:3a73d77c2cef | 136 | intensity = 1.0f; |
krebyy | 1:3a73d77c2cef | 137 | } |
krebyy | 1:3a73d77c2cef | 138 | else if (intensity < 0.0f) |
krebyy | 1:3a73d77c2cef | 139 | { |
krebyy | 1:3a73d77c2cef | 140 | intensity = 0.0f; |
krebyy | 0:cbba28a205fa | 141 | } |
krebyy | 0:cbba28a205fa | 142 | |
krebyy | 1:3a73d77c2cef | 143 | led = 1.0f - intensity; |
krebyy | 1:3a73d77c2cef | 144 | } |
krebyy | 1:3a73d77c2cef | 145 | |
krebyy | 1:3a73d77c2cef | 146 | void setLED(LEDType_t color) |
krebyy | 1:3a73d77c2cef | 147 | { |
krebyy | 1:3a73d77c2cef | 148 | setLED(ledRED, OFF); |
krebyy | 1:3a73d77c2cef | 149 | setLED(ledGREEN, OFF); |
krebyy | 1:3a73d77c2cef | 150 | setLED(ledBLUE, OFF); |
krebyy | 0:cbba28a205fa | 151 | |
krebyy | 1:3a73d77c2cef | 152 | switch(color) |
krebyy | 1:3a73d77c2cef | 153 | { |
krebyy | 1:3a73d77c2cef | 154 | case RED: |
krebyy | 1:3a73d77c2cef | 155 | setLED(ledRED, ON); |
krebyy | 1:3a73d77c2cef | 156 | break; |
krebyy | 0:cbba28a205fa | 157 | |
krebyy | 1:3a73d77c2cef | 158 | case GREEN: |
krebyy | 1:3a73d77c2cef | 159 | setLED(ledGREEN, ON); |
krebyy | 1:3a73d77c2cef | 160 | break; |
krebyy | 0:cbba28a205fa | 161 | |
krebyy | 1:3a73d77c2cef | 162 | case BLUE: |
krebyy | 1:3a73d77c2cef | 163 | setLED(ledBLUE, ON); |
krebyy | 1:3a73d77c2cef | 164 | break; |
krebyy | 0:cbba28a205fa | 165 | |
krebyy | 1:3a73d77c2cef | 166 | case YELLOW: |
krebyy | 1:3a73d77c2cef | 167 | setLED(ledRED, ON); |
krebyy | 1:3a73d77c2cef | 168 | setLED(ledGREEN, ON); |
krebyy | 1:3a73d77c2cef | 169 | break; |
krebyy | 0:cbba28a205fa | 170 | |
krebyy | 1:3a73d77c2cef | 171 | case CYAN: |
krebyy | 1:3a73d77c2cef | 172 | setLED(ledGREEN, ON); |
krebyy | 1:3a73d77c2cef | 173 | setLED(ledBLUE, ON); |
krebyy | 1:3a73d77c2cef | 174 | break; |
krebyy | 0:cbba28a205fa | 175 | |
krebyy | 1:3a73d77c2cef | 176 | case MAGENTA: |
krebyy | 1:3a73d77c2cef | 177 | setLED(ledRED, ON); |
krebyy | 1:3a73d77c2cef | 178 | setLED(ledBLUE, ON); |
krebyy | 1:3a73d77c2cef | 179 | break; |
krebyy | 0:cbba28a205fa | 180 | |
krebyy | 1:3a73d77c2cef | 181 | case WHITE: |
krebyy | 1:3a73d77c2cef | 182 | setLED(ledRED, ON); |
krebyy | 1:3a73d77c2cef | 183 | setLED(ledGREEN, ON); |
krebyy | 1:3a73d77c2cef | 184 | setLED(ledBLUE, ON); |
krebyy | 1:3a73d77c2cef | 185 | break; |
krebyy | 0:cbba28a205fa | 186 | |
krebyy | 1:3a73d77c2cef | 187 | case NONE: |
krebyy | 1:3a73d77c2cef | 188 | break; |
krebyy | 0:cbba28a205fa | 189 | |
krebyy | 1:3a73d77c2cef | 190 | default: |
krebyy | 1:3a73d77c2cef | 191 | break; |
krebyy | 1:3a73d77c2cef | 192 | } |
krebyy | 1:3a73d77c2cef | 193 | } |
krebyy | 1:3a73d77c2cef | 194 | |
krebyy | 1:3a73d77c2cef | 195 | // code: HTML Color Code (without #) - Examples: "00ff00", "70befc" |
krebyy | 1:3a73d77c2cef | 196 | void setLED(const char * code) |
krebyy | 1:3a73d77c2cef | 197 | { |
krebyy | 1:3a73d77c2cef | 198 | int hexValue = strtol(code, NULL, 16); |
krebyy | 0:cbba28a205fa | 199 | |
krebyy | 1:3a73d77c2cef | 200 | float r = ((hexValue >> 16) & 0xFF) / 255.0; // Extract the RR byte |
krebyy | 1:3a73d77c2cef | 201 | float g = ((hexValue >> 8) & 0xFF) / 255.0; // Extract the GG byte |
krebyy | 1:3a73d77c2cef | 202 | float b = ((hexValue) & 0xFF) / 255.0; // Extract the BB byte |
krebyy | 0:cbba28a205fa | 203 | |
krebyy | 1:3a73d77c2cef | 204 | setLED(ledRED, r); |
krebyy | 1:3a73d77c2cef | 205 | setLED(ledGREEN, g); |
krebyy | 1:3a73d77c2cef | 206 | setLED(ledBLUE, b); |
krebyy | 1:3a73d77c2cef | 207 | } |
krebyy | 0:cbba28a205fa | 208 | |
krebyy | 1:3a73d77c2cef | 209 | void toggleLED(LEDType_t color) |
krebyy | 1:3a73d77c2cef | 210 | { |
krebyy | 1:3a73d77c2cef | 211 | switch(color) |
krebyy | 0:cbba28a205fa | 212 | { |
krebyy | 1:3a73d77c2cef | 213 | case RED: |
krebyy | 1:3a73d77c2cef | 214 | ledRED = !ledRED; |
krebyy | 1:3a73d77c2cef | 215 | break; |
krebyy | 0:cbba28a205fa | 216 | |
krebyy | 1:3a73d77c2cef | 217 | case GREEN: |
krebyy | 1:3a73d77c2cef | 218 | ledGREEN = !ledGREEN; |
krebyy | 1:3a73d77c2cef | 219 | break; |
krebyy | 0:cbba28a205fa | 220 | |
krebyy | 1:3a73d77c2cef | 221 | case BLUE: |
krebyy | 1:3a73d77c2cef | 222 | ledBLUE = !ledBLUE; |
krebyy | 1:3a73d77c2cef | 223 | break; |
krebyy | 0:cbba28a205fa | 224 | |
krebyy | 1:3a73d77c2cef | 225 | default: |
krebyy | 1:3a73d77c2cef | 226 | break; |
krebyy | 0:cbba28a205fa | 227 | } |
krebyy | 1:3a73d77c2cef | 228 | } |
krebyy | 1:3a73d77c2cef | 229 | }; |
krebyy | 0:cbba28a205fa | 230 | |
krebyy | 1:3a73d77c2cef | 231 | Ioton ton; |
krebyy | 0:cbba28a205fa | 232 | |
krebyy | 1:3a73d77c2cef | 233 | #endif // IOTON_H |