Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ton_demo ton_template
Ioton.h
00001 /* Ioton Boards Library 00002 * Copyright (c) 2016-2017 Ioton Technology 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef IOTON_H 00018 #define IOTON_H 00019 00020 #include "mbed.h" 00021 #include "BMX055.h" 00022 #include "ESP8266.h" 00023 #include "USBTon.h" 00024 00025 #define ON 1 00026 #define OFF 0 00027 #define BATTERY_SCALE 1.4681f /* Voltage divider: [(R18 + R19) / R19] */ 00028 #define BATTERY_MIN 3.6f 00029 #define BATTERY_MAX 4.2f 00030 00031 typedef enum 00032 { 00033 RED, 00034 GREEN, 00035 BLUE, 00036 YELLOW, 00037 CYAN, 00038 MAGENTA, 00039 WHITE, 00040 NONE 00041 } LEDType_t; 00042 00043 00044 Serial bluetooth(BT_TX, BT_RX); 00045 ESP8266 wifi(WIFI_TX, WIFI_RX); 00046 DigitalOut BT_RST(PC_15); 00047 DigitalIn VBUS(PA_9); 00048 DigitalOut WIFI_PWD(PB_12); 00049 DigitalOut WIFI_MODE(PB_13); 00050 DigitalOut WIFI_RST(PC_14); 00051 00052 DigitalIn USER(SW_USER); 00053 PwmOut ledRED(LED_RED); 00054 PwmOut ledGREEN(LED_GREEN); 00055 PwmOut ledBLUE(LED_BLUE); 00056 AnalogIn battery(BAT_METER); 00057 00058 BMX055 imu; 00059 USBTon usb; 00060 00061 00062 class Ioton 00063 { 00064 public: 00065 Ioton() 00066 { 00067 setLED(NONE); 00068 bluetooth.baud(9600); 00069 WIFI_PWD.write(0); 00070 WIFI_MODE.write(1); 00071 WIFI_RST.write(0); 00072 BT_RST.write(0); 00073 wait_ms(1); 00074 00075 if (VBUS) initUSB(); 00076 } 00077 00078 void batteryStatus(bool loop = true) 00079 { 00080 float batPercertage = 0; 00081 00082 /* loop = true: forever | loop = false: only once */ 00083 while (loop) 00084 { 00085 batPercertage = ((getBattery() - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)); 00086 00087 /* GREEN: fully charged; RED: fully discharged; 00088 ORANGE/YELLOW: intermediate load */ 00089 setLED(ledRED, 1.0f - batPercertage); 00090 setLED(ledGREEN, batPercertage); 00091 wait(0.5); 00092 00093 /* BLINK: if it is critically or fully charged */ 00094 if (batPercertage > 1.0f || batPercertage < 0.0f) 00095 { 00096 setLED(NONE); 00097 wait(0.5); 00098 } 00099 } 00100 } 00101 00102 void enableBluetooth(void) 00103 { 00104 BT_RST.write(1); 00105 } 00106 00107 void enableIMU(uint8_t mAscale = AFS_2G, uint8_t ACCBW = ABW_125Hz, 00108 uint8_t mGscale = GFS_125DPS, uint8_t GODRBW = G_200Hz23Hz, 00109 uint8_t Mmode = Regular, uint8_t MODR = MODR_30Hz) 00110 { 00111 imu.init(mAscale, ACCBW, mGscale, GODRBW, Mmode, MODR); 00112 } 00113 00114 void enableWifi(void) 00115 { 00116 WIFI_PWD.write(1); 00117 WIFI_RST.write(1); 00118 } 00119 00120 int USERisPressed(void) 00121 { 00122 return USER; 00123 } 00124 00125 float getBattery(void) 00126 { 00127 float vBat = battery.read(); 00128 00129 return (vBat * 3.3f * BATTERY_SCALE); 00130 } 00131 00132 void setLED(PwmOut led, float intensity) 00133 { 00134 if (intensity > 1.0f) 00135 { 00136 intensity = 1.0f; 00137 } 00138 else if (intensity < 0.0f) 00139 { 00140 intensity = 0.0f; 00141 } 00142 00143 led = 1.0f - intensity; 00144 } 00145 00146 void setLED(LEDType_t color) 00147 { 00148 setLED(ledRED, OFF); 00149 setLED(ledGREEN, OFF); 00150 setLED(ledBLUE, OFF); 00151 00152 switch(color) 00153 { 00154 case RED: 00155 setLED(ledRED, ON); 00156 break; 00157 00158 case GREEN: 00159 setLED(ledGREEN, ON); 00160 break; 00161 00162 case BLUE: 00163 setLED(ledBLUE, ON); 00164 break; 00165 00166 case YELLOW: 00167 setLED(ledRED, ON); 00168 setLED(ledGREEN, ON); 00169 break; 00170 00171 case CYAN: 00172 setLED(ledGREEN, ON); 00173 setLED(ledBLUE, ON); 00174 break; 00175 00176 case MAGENTA: 00177 setLED(ledRED, ON); 00178 setLED(ledBLUE, ON); 00179 break; 00180 00181 case WHITE: 00182 setLED(ledRED, ON); 00183 setLED(ledGREEN, ON); 00184 setLED(ledBLUE, ON); 00185 break; 00186 00187 case NONE: 00188 break; 00189 00190 default: 00191 break; 00192 } 00193 } 00194 00195 // code: HTML Color Code (without #) - Examples: "00ff00", "70befc" 00196 void setLED(const char * code) 00197 { 00198 int hexValue = strtol(code, NULL, 16); 00199 00200 float r = ((hexValue >> 16) & 0xFF) / 255.0; // Extract the RR byte 00201 float g = ((hexValue >> 8) & 0xFF) / 255.0; // Extract the GG byte 00202 float b = ((hexValue) & 0xFF) / 255.0; // Extract the BB byte 00203 00204 setLED(ledRED, r); 00205 setLED(ledGREEN, g); 00206 setLED(ledBLUE, b); 00207 } 00208 00209 void toggleLED(LEDType_t color) 00210 { 00211 switch(color) 00212 { 00213 case RED: 00214 ledRED = !ledRED; 00215 break; 00216 00217 case GREEN: 00218 ledGREEN = !ledGREEN; 00219 break; 00220 00221 case BLUE: 00222 ledBLUE = !ledBLUE; 00223 break; 00224 00225 default: 00226 break; 00227 } 00228 } 00229 }; 00230 00231 Ioton ton; 00232 00233 #endif // IOTON_H
Generated on Tue Jul 12 2022 20:52:33 by
1.7.2