seeed unified library interface
Embed:
(wiki syntax)
Show/hide line numbers
suli2.cpp
00001 /* 00002 suli2.c/suli2.cpp 00003 Seeed Unified Library Interface v2.0 00004 2015 Copyright (c) Seeed Technology Inc. All right reserved. 00005 00006 Author:Jack Shao, Loovee 00007 Change Logs: 00008 2015-4-17: initial version for v2 00009 00010 suli is designed for the purpose of reusing the high level implementation 00011 of each libraries for different platforms out of the hardware layer. 00012 suli2 is the new reversion of original suli. There're lot of improvements upon 00013 the previous version. Currently, it can be treated as the internal strategy for 00014 quick library development of seeed. But always welcome the community to 00015 follow the basis of suli to contribute grove libraries. 00016 00017 The MIT License (MIT) 00018 00019 Permission is hereby granted, free of charge, to any person obtaining a copy 00020 of this software and associated documentation files (the "Software"), to deal 00021 in the Software without restriction, including without limitation the rights 00022 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00023 copies of the Software, and to permit persons to whom the Software is 00024 furnished to do so, subject to the following conditions: 00025 00026 The above copyright notice and this permission notice shall be included in 00027 all copies or substantial portions of the Software. 00028 00029 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00030 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00031 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00032 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00033 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00034 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00035 THE SOFTWARE. 00036 */ 00037 00038 #include "suli2.h" 00039 00040 //---------------------------------------- mbed --------------------------------------------- 00041 #if defined(__MBED__) 00042 00043 /** 00044 * uint32_t suli_pin_pulse_in(IO_T *, what_state, timeout) 00045 */ 00046 uint32_t suli_pin_pulse_in(IO_T *pio, int state, uint32_t timeout) 00047 { 00048 //TODO: more efficient implementation 00049 uint32_t t = us_ticker_read(); 00050 while (gpio_read(pio) != state) 00051 { 00052 if (timeout > 0 && (us_ticker_read() - t) > timeout) return 0; 00053 } 00054 uint32_t t1 = us_ticker_read(); 00055 while (gpio_read(pio) == state) 00056 { 00057 if (timeout > 0 && (us_ticker_read() - t) > timeout) return 0; 00058 } 00059 return us_ticker_read() - t1 /*- ??? some wasting code consumes some time */; 00060 } 00061 00062 /** 00063 * send bytes to uart 00064 * int suli_uart_write_bytes(UART_T *, uint8_t *, int) 00065 */ 00066 int suli_uart_write_bytes(UART_T *uart, uint8_t *data, int len) 00067 { 00068 for (int i = 0; i < len;i++) 00069 { 00070 serial_putc(uart, (int)(*(data + i)) ); 00071 } 00072 return len; 00073 } 00074 00075 /** 00076 * write a float 00077 * num - number to write 00078 * decimal - x decimal point 00079 */ 00080 void suli_uart_write_float(UART_T *uart, float float_num, int decimal) 00081 { 00082 char fmt[6]; 00083 char buff[32]; 00084 snprintf(fmt, 6, "%%.%df", decimal); 00085 int r = snprintf(buff, 32, (const char *)fmt, float_num); 00086 suli_uart_write_bytes(uart, (uint8_t *)buff, r); 00087 } 00088 /** 00089 * write an integer 00090 * num - number to write 00091 */ 00092 void suli_uart_write_int(UART_T *uart, int32_t num) 00093 { 00094 char buff[32]; 00095 int r = snprintf(buff, 32, "%ld", num); 00096 suli_uart_write_bytes(uart, (uint8_t *)buff, r); 00097 } 00098 00099 /** 00100 * read bytes from uart 00101 */ 00102 int suli_uart_read_bytes(UART_T *uart, uint8_t *buff, int len) 00103 { 00104 uint8_t *ptr = buff; 00105 uint8_t *end = ptr + len; 00106 while (ptr != end) 00107 { 00108 int c = serial_getc(uart); 00109 *ptr++ = c; 00110 } 00111 return ptr - buff; 00112 } 00113 00114 /** 00115 * read bytes from uart with timeout ms 00116 */ 00117 int suli_uart_read_bytes_timeout(UART_T *uart, uint8_t *buff, int len, int timeout_ms) 00118 { 00119 uint8_t *ptr = buff; 00120 uint8_t *end = ptr + len; 00121 uint32_t t = suli_millis(); 00122 00123 while (ptr != end) 00124 { 00125 if ((suli_millis() - t) > timeout_ms) break; 00126 int c = serial_getc(uart); 00127 *ptr++ = c; 00128 } 00129 return ptr - buff; 00130 } 00131 00132 00133 00134 //---------------------------------------------arduino--------------------------------------------- 00135 #elif defined(ARDUINO) 00136 00137 #if defined (ARDUINO_USE_I2C) || defined(ESP8266) 00138 /** 00139 * I2C interface initialize. 00140 */ 00141 void suli_i2c_init(I2C_T *i2c_device, int pin_sda, int pin_clk) 00142 { 00143 #ifdef ESP8266 00144 *i2c_device = new TwoWire(); //change the pin defined in pin_arduino.h 00145 (*i2c_device)->begin(SDA, SCL); 00146 #else 00147 *i2c_device = &Wire; 00148 (*i2c_device)->begin(); 00149 #endif 00150 } 00151 00152 00153 /** 00154 * write a buff to I2C 00155 * - i2c_device: i2c device pointer 00156 * - dev_addr: device address 00157 * - data: data buff 00158 * - len: data lenght 00159 */ 00160 uint8_t suli_i2c_write(I2C_T *i2c_device, uint8_t dev_addr, uint8_t *data, int len) 00161 { 00162 dev_addr = dev_addr >> 1; 00163 00164 (*i2c_device)->beginTransmission(dev_addr); // start 00165 for (int i = 0; i < len; i++) 00166 { 00167 (*i2c_device)->write(data[i]); // send a byte 00168 } 00169 (*i2c_device)->endTransmission(); // end 00170 00171 return len; 00172 } 00173 00174 00175 /** 00176 * read a buff to I2C 00177 * - i2c_device: i2c device pointer 00178 * - dev_addr: device address 00179 * - data: data buff 00180 * - len: data lenght 00181 * return 00182 */ 00183 uint8_t suli_i2c_read(I2C_T *i2c_device, uint8_t dev_addr, uint8_t *buff, int len) 00184 { 00185 dev_addr = dev_addr >> 1; 00186 (*i2c_device)->requestFrom(dev_addr, (uint8_t)len); 00187 00188 int sum_len = 0; 00189 while ((*i2c_device)->available()) 00190 { 00191 buff[sum_len++] = (*i2c_device)->read(); 00192 } 00193 return sum_len; 00194 } 00195 00196 #endif 00197 00198 /** 00199 * void suli_uart_init(UART_T *, int pin_tx, int pin_rx, uint32_t baud) 00200 */ 00201 #if defined(ARDUINO_USE_SOFTWARE_SERIAL) 00202 #include "SoftwareSerial.h" 00203 #endif 00204 00205 void suli_uart_init(UART_T *uart, int pin_tx, int pin_rx, uint32_t baud) 00206 { 00207 #if defined(ESP8266) //a 00208 if (pin_tx == 1 && pin_rx == 3) 00209 { 00210 *uart = (Stream *)&Serial; 00211 Serial.begin(baud); 00212 } else if (pin_tx == 2) 00213 { 00214 *uart = (Stream *)&Serial1; 00215 Serial1.begin(baud); 00216 } else 00217 { 00218 *uart = NULL; //this will cause the program not passing the compiling 00219 } 00220 #else //a 00221 if (pin_tx == 1 && pin_rx == 0) 00222 { 00223 #if defined(__AVR_ATmega32U4__) //b 00224 *uart = (Stream *)&Serial1; 00225 Serial1.begin(baud); 00226 #else //b 00227 *uart = (Stream *)&Serial; 00228 Serial.begin(baud); 00229 #endif //b 00230 } 00231 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 00232 else if (pin_tx == 18 && pin_rx == 19) 00233 { 00234 *uart = (Stream *)&Serial1; 00235 Serial1.begin(baud); 00236 } 00237 else if (pin_tx == 16 && pin_rx == 17) 00238 { 00239 *uart = (Stream *)&Serial2; 00240 Serial2.begin(baud); 00241 } 00242 else if (pin_tx == 14 && pin_rx == 15) 00243 { 00244 *uart = (Stream *)&Serial3; 00245 Serial3.begin(baud); 00246 } 00247 #endif //defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 00248 00249 #endif //a 00250 00251 #if defined(ARDUINO_SOFTWARE_SERIAL) 00252 SoftwareSerial *ser = new SoftwareSerial(pin_rx, pin_tx); 00253 *uart = ser; 00254 ser->begin(baud); 00255 #endif 00256 00257 } 00258 00259 /** 00260 * send bytes to uart 00261 * int suli_uart_write_bytes(UART_T *, uint8_t *, int) 00262 */ 00263 int suli_uart_write_bytes(UART_T *uart, uint8_t *data, int len) 00264 { 00265 for (int i = 0; i < len; i++) 00266 { 00267 (*uart)->write(*(data + i)); 00268 } 00269 return len; 00270 } 00271 00272 00273 00274 #endif
Generated on Fri Jul 15 2022 03:40:37 by 1.7.2