├── DFRobotCar.cpp ├── DFRobotCar.h ├── DFRobot_utility.h ├── DF_utility.cpp ├── DF_utility.h ├── LICENSE ├── README.md ├── checksum.cpp ├── checksum.h ├── color.cpp ├── color.h ├── examples ├── DFCharBuffer_test │ └── DFCharBuffer_test.ino ├── DFRobotCar_test │ └── DFRobotCar_test.ino ├── DFTimer_breathe │ └── DFTimer_breathe.ino ├── DFTimer_sample │ └── DFTimer_sample.ino ├── DF_GPS_test │ └── DF_GPS_test.ino ├── SerialProtocol_test │ └── SerialProtocol_test.ino ├── at_parse_test │ └── at_parse_test.ino ├── io_sample │ └── io_sample.ino ├── rainbow_test │ └── rainbow_test.ino ├── rgbLed_test │ └── rgbLed_test.ino ├── serialReadCmd │ └── serialReadCmd.ino ├── serialRead_sample │ └── serialRead_sample.ino ├── split_test │ └── split_test.ino └── urm37_sample │ └── urm37_sample.ino ├── io.cpp ├── io.h ├── keycheck.cpp ├── keycheck.h ├── keywords.txt ├── license.txt ├── serialStream.cpp ├── serialStream.h ├── split.cpp ├── split.h └── utility ├── DFCharBuffer.cpp ├── DFCharBuffer.h ├── DFCommon.cpp ├── DFCommon.h ├── DFTimer.cpp ├── DFTimer.h ├── DF_AT_parse.cpp ├── DF_AT_parse.h ├── DF_GPS.cpp ├── DF_GPS.h ├── SerialProtocol.cpp ├── SerialProtocol.h ├── rgbLed.cpp ├── rgbLed.h ├── urm37.cpp └── urm37.h /DFRobotCar.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFRobotCar.cpp 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | provide some useful function make it easy to control two DC car 8 | 9 | Copyright (C) 2014 DFRobot 10 | */ 11 | 12 | #include 13 | #include "DFRobotCar.h" 14 | 15 | //DFRobotCar mycar (4,5,7,6); 16 | DFRobotCar::DFRobotCar (uint8_t left_en, uint8_t left_pwm, uint8_t right_en, uint8_t right_pwm) { 17 | _left_en = left_en; 18 | _left_pwm = left_pwm; 19 | _right_en = right_en; 20 | _right_pwm = right_pwm; 21 | pinMode (_left_en, OUTPUT); 22 | pinMode (_left_pwm, OUTPUT); 23 | pinMode (_right_en, OUTPUT); 24 | pinMode (_right_pwm, OUTPUT); 25 | 26 | _left_advance = LOW; 27 | _left_back = HIGH; 28 | _right_advance = HIGH; 29 | _right_back = LOW; 30 | } 31 | 32 | // 33 | void DFRobotCar::changeDir (bool left, bool right) { 34 | if (left) { 35 | _left_advance = !_left_advance; 36 | _left_back = !_left_back; 37 | } 38 | if (right) { 39 | _right_advance = !_right_advance; 40 | _right_back = !_right_back; 41 | } 42 | 43 | } 44 | 45 | // 46 | void DFRobotCar::control (int16_t left_speed, int16_t right_speed) { 47 | if (left_speed < 0) { 48 | left_speed = -left_speed; 49 | digitalWrite (_left_en, _left_back); 50 | } else { 51 | digitalWrite (_left_en, _left_advance); 52 | } 53 | if (right_speed < 0) { 54 | right_speed = -right_speed; 55 | digitalWrite (_right_en, _right_back); 56 | } else { 57 | digitalWrite (_right_en, _right_advance); 58 | } 59 | if (left_speed > 255) 60 | left_speed = 255; 61 | if (right_speed > 255) 62 | right_speed = 255; 63 | analogWrite (_left_pwm, left_speed); 64 | analogWrite (_right_pwm, right_speed); 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /DFRobotCar.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFRobotCar.h 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | provide some useful function make it easy to control two DC car 8 | 9 | Copyright (C) 2014 DFRobot 10 | */ 11 | 12 | #include 13 | 14 | class DFRobotCar { 15 | private: 16 | bool _left_advance;// = LOW; 17 | bool _left_back;// = HIGH; 18 | bool _right_advance;// = HIGH; 19 | bool _right_back;// = LOW; 20 | 21 | uint8_t _left_en;// = 4; 22 | uint8_t _left_pwm;// = 5; 23 | uint8_t _right_en;// = 7; 24 | uint8_t _right_pwm;// = 6; 25 | 26 | public: 27 | DFRobotCar (uint8_t left_en, uint8_t left_pwm, uint8_t right_en, uint8_t right_pwm); 28 | 29 | void changeDir (bool left, bool right); 30 | 31 | void control (int16_t left, int16_t right); 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /DFRobot_utility.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * DFRobot_utility, This library provides some useful function for Arduino. * 3 | * www.github.com/dfrobot/DFRobot-utility (github as default source provider) * 4 | * DFRobot-A great source for opensource hardware and robot. * 5 | * * 6 | * This file is part of the DFRobot_utility library. * 7 | * * 8 | * DFRobot_utility is free software: you can redistribute it and/or * 9 | * modify it under the terms of the GNU Lesser General Public License as * 10 | * published by the Free Software Foundation, either version 3 of * 11 | * the License, or any later version. * 12 | * * 13 | * DFRobot_utility is distributed in the hope that it will be useful, * 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 16 | * GNU Lesser General Public License for more details. * 17 | * * 18 | * DFRobot_utility is distributed in the hope that it will be useful, * 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 21 | * GNU Lesser General Public License for more details. * 22 | * * 23 | * You should have received a copy of the GNU Lesser General Public * 24 | * License along with DFRobot_utility. If not, see * 25 | * . * 26 | ******************************************************************************/ 27 | 28 | /* 29 | * Copyright: DFRobot 30 | * name: DFRobot_utility 31 | * version: 1.0 32 | * Author: lisper (lisper.li@dfrobot.com) 33 | * Date: 2014-05-22 34 | * Description: Common library for Arduino 35 | */ 36 | 37 | #include "split.h" 38 | #include "serialStream.h" 39 | #include "checksum.h" 40 | #include "keycheck.h" 41 | #include "DF_utility.h" 42 | #include "DFRobotCar.h" 43 | #include "io.h" 44 | #include "color.h" 45 | -------------------------------------------------------------------------------- /DF_utility.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | // 3 | void fill_uint16_bigend (uint8_t *thebuf, uint16_t data) { 4 | *thebuf = (uint8_t)(data>>8); 5 | *(thebuf+1) = (uint8_t)data; 6 | } 7 | 8 | 9 | // 10 | void fill_uint16 (uint8_t *thebuf, uint16_t data) { 11 | *(uint16_t*)(thebuf) = data; 12 | } 13 | -------------------------------------------------------------------------------- /DF_utility.h: -------------------------------------------------------------------------------- 1 | // 2 | void fill_uint16_bigend (uint8_t *thebuf, uint16_t data); 3 | 4 | 5 | // 6 | void fill_uint16 (uint8_t *thebuf, uint16_t data); 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DFRobot-utility 2 | ================= 3 | provide some useful function for Arduino 4 | 5 | Install instructions: 6 | 7 | * Download file 8 | * decompress 9 | * copy inside folder to your Arduino library folder 10 | * restart your Arduino IDE 11 | 12 | Copyright (C) DFRobot - www.dfrobot.com -------------------------------------------------------------------------------- /checksum.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file checksum.cpp 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | provide some useful function make it easy to get checksum 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include "Arduino.h" 14 | 15 | uint8_t checksum_add (uint8_t *data, uint8_t leng) { 16 | uint8_t sum = 0; 17 | for (uint8_t i=0; i(0~360), s=>(0~1.0) add=>(0~1.0) 36 | float hsb[3] = { 37 | h, s, b}; 38 | uint8_t rgb[3]; 39 | HSB2RGB (hsb, rgb); 40 | return ((uint32_t)rgb[0] << 16) | ((uint32_t)rgb[1] << 8) | (uint32_t)rgb[2]; 41 | } 42 | 43 | #ifdef colorCircle 44 | uint32_t hbToColor (uint16_t h, float b) { 45 | //return color:0x00rrggbb, very fast, note: s always is 1.0 46 | //h=>(0~360), b=>(0~1.0) 47 | uint32_t color = color_array[h]; 48 | uint8_t r = (uint8_t)color * b; 49 | uint8_t g = (uint8_t)(color>>8) * b; 50 | uint8_t add = (uint8_t)(color>>16) * b; 51 | return ((uint32_t)r << 16) | ((uint32_t)g << 8) | (uint32_t)add; 52 | } 53 | 54 | void setColorCircle (uint32_t *array, uint32_t length, float s, float add) { 55 | //array length is 360 56 | for (int i=0; i(0~360), s=>(0~1.0) add=>(0~1.0) 18 | uint32_t hsbToColor (float h, float s, float b); 19 | 20 | #ifdef colorCircle 21 | uint32_t hbToColor (uint16_t h, float b); 22 | //return color:0x00rrggbb, very fast, note: s always is 1.0 23 | //h=>(0~360), b=>(0~1.0) 24 | 25 | void setColorCircle (uint32_t *array, uint16_t length, float s, float add); 26 | //array length is 360 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /examples/DFCharBuffer_test/DFCharBuffer_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | DFCharBuffer mybuf (10); 5 | 6 | void setup () { 7 | for (int i=0; i<6; i++) { 8 | mybuf.writeFromHead (i+'a'); 9 | } 10 | } 11 | 12 | void loop () { 13 | for (int i=0; i<10; i++) { 14 | int value = mybuf.readFromTail (); 15 | if (value > 0) { 16 | Serial.println ((char)value); 17 | } 18 | } 19 | delay (1000); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /examples/DFRobotCar_test/DFRobotCar_test.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFRobotCar_test.ino 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | test code for DFRobotCar library 8 | 9 | Copyright (C) 2014 DFRobot 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | 15 | DFRobotCar mycar (7, 6, 4, 5); 16 | 17 | void setup () { 18 | mycar.changeDir (true, true); 19 | pinMode (13, OUTPUT); 20 | } 21 | 22 | 23 | void loop () { 24 | mycar.control (100, 0); 25 | digitalWrite (13, HIGH); 26 | delay (1000); 27 | 28 | mycar.control (100, 100); 29 | digitalWrite (13, LOW); 30 | delay (1000); 31 | 32 | mycar.control (0, 0); 33 | delay (2000); 34 | } 35 | 36 | 37 | /* 38 | * 39 | * DFRobotCar (uint8_t left_en, uint8_t left_pwm, uint8_t right_en, uint8_t right_pwm); 40 | * 41 | * //switch left and right side direction 42 | * void changeDir (boolean left, boolean right); 43 | * 44 | * //specify left and right side's speed, round is (-255 ~ 255) 45 | * void control (int16_t left, int16_t right); 46 | * 47 | */ 48 | -------------------------------------------------------------------------------- /examples/DFTimer_breathe/DFTimer_breathe.ino: -------------------------------------------------------------------------------- 1 | #include "DFRobot_utility.h" 2 | #include 3 | 4 | DFTimer breathe_timer = DFTimer (breathe_func, 1, 3000); 5 | 6 | // 7 | void setup () { 8 | pinMode (13, OUTPUT); 9 | Serial.begin (9600); 10 | } 11 | 12 | // 13 | void loop () { 14 | breathe_timer.check (); 15 | } 16 | 17 | // 18 | void breathe_func () { 19 | static int16_t ledValue = 0; 20 | static boolean dir = true; 21 | if (dir) 22 | ledValue ++; 23 | else 24 | ledValue --; 25 | if (ledValue < 0) { 26 | dir = true; 27 | ledValue = 0; 28 | } 29 | else if (ledValue > 255) { 30 | dir = false; 31 | ledValue = 255; 32 | } 33 | analogWrite (13 , ledValue); 34 | // Serial.println (ledValue); 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /examples/DFTimer_sample/DFTimer_sample.ino: -------------------------------------------------------------------------------- 1 | #include "DFRobot_utility.h" 2 | #include 3 | 4 | void blink_func (); 5 | void func1s (); 6 | void func4s (void *args); 7 | 8 | DFTimer blink_timer = DFTimer (blink_func, 1000); 9 | DFTimer timer1s = DFTimer (func1s, 1000); 10 | DFTimer timer4s = DFTimer (func4s, 4000, (void*)"4s"); 11 | 12 | void setup () { 13 | pinMode (13, OUTPUT); 14 | Serial.begin (9600); 15 | } 16 | 17 | void loop () { 18 | blink_timer.check (); 19 | timer1s.check (); 20 | timer4s.check (); 21 | } 22 | 23 | void blink_func () { 24 | static boolean ledstate = true; 25 | ledstate = !ledstate; 26 | digitalWrite (13 , ledstate); 27 | } 28 | 29 | void func1s () { 30 | Serial.println ("hello, 1s"); 31 | } 32 | 33 | void func4s (void *args) { 34 | Serial.println ((char*)args); 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /examples/DF_GPS_test/DF_GPS_test.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DF_GPS_test.ino 4 | @author lisper (leyapin@gmail.com, lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | test on uno or leonardo 7 | Copyright (C) DFRobot - www.dfrobot.com 8 | */ 9 | /**************************************************************************/ 10 | #include 11 | #include 12 | 13 | //gpgga_s gpgga_data; 14 | 15 | DFGPS gps (Serial); 16 | //DFGPS gps (Serial1); if use leonardo and Serial1 17 | 18 | void setup () { 19 | Serial.begin (9600); 20 | //Serial1.begin (9600); if use leonardo and Serial1 21 | //while (! Serial1); 22 | } 23 | 24 | // 25 | void loop () { 26 | int res = gps.read (); 27 | if (res) { 28 | if (gps.fix() == 1) { 29 | //gps.gpgga (&gpgga_data); 30 | Serial.println ("fixed"); 31 | Serial.print ("latitude="); 32 | Serial.println (gps.getLatitude ()); 33 | Serial.print ("longitude="); 34 | Serial.println (gps.getLongitude()); 35 | print_gps_data (); 36 | } 37 | gps.printGPGGA (); //will always print 38 | } 39 | } 40 | 41 | // 42 | void print_gps_data () { 43 | Serial.print ("getTime: "); 44 | Serial.println (gps.getTime ()); 45 | Serial.print ("getLatitude: "); 46 | Serial.println (gps.getLatitude (), 6); 47 | Serial.print ("getLongitude: "); 48 | Serial.println (gps.getLongitude (), 5); 49 | Serial.print ("fixc: "); 50 | Serial.println (gps.fixc ()); 51 | Serial.print ("getNum: "); 52 | Serial.println (gps.getNum ()); 53 | Serial.print ("getHDOP: "); 54 | Serial.println (gps.getHDOP ()); 55 | Serial.print ("getAltidude: "); 56 | Serial.print (gps.getAltidude ()); 57 | Serial.println (gps.getAunits ()); 58 | 59 | Serial.print ("getLevel: "); 60 | Serial.print (gps.getLevelStr ()); 61 | Serial.println (gps.getLunits ()); 62 | Serial.println (); 63 | 64 | } 65 | 66 | //typedef struct { 67 | // //int id; 68 | // struct utc{ //1:UTC time 69 | // int hour; 70 | // int minutes; 71 | // int second; 72 | // }utc; 73 | // double latitude; //2: 74 | // char ns; //3: 75 | // double longtitude; //4: 76 | // char ew; //5: 77 | // int fix; //6:gps status 0 1 2 3 4 5 6 7 8 9 78 | // int num; //7: 79 | // double hdop; //8:水平精度 80 | // double altitude; //9:海拔高度 81 | // char a_units; //10:单位M 82 | // double level; //11:大地水准面高度 83 | // char l_units; //12:单位M 84 | // double diff_time; 13 85 | // int diff_id; //14 86 | //} gpgga_s; 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /examples/SerialProtocol_test/SerialProtocol_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 5 | struct mytype { 6 | uint8_t cmd; 7 | uint32_t data; 8 | }; 9 | 10 | // 11 | struct mytype receiveCmd; 12 | 13 | // 14 | void setup () { 15 | Serial.begin (115200); 16 | } 17 | 18 | // 19 | void loop () { 20 | int result = readData (&receiveCmd, sizeof (receiveCmd)); 21 | if (result> 0) { 22 | parseData (&receiveCmd); 23 | } 24 | } 25 | 26 | // 27 | void parseData (struct mytype *theData) { 28 | mytype sendCmd; 29 | switch(theData->cmd) { 30 | case 0x01: //say "hello" 31 | Serial.println ("hello"); 32 | break; 33 | case 0x02: //return the same data 34 | sendCmd.cmd = theData->cmd | 0x80; 35 | sendCmd.data = theData->data; 36 | sendData ((void*)&sendCmd, sizeof (sendCmd)); 37 | break; 38 | case 0x03: //print the command and data 39 | Serial.println (theData->cmd, HEX); 40 | Serial.println (theData->data, HEX); 41 | break; 42 | default: 43 | Serial.println ("error! no this command!"); 44 | } 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /examples/at_parse_test/at_parse_test.ino: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | * dfrobot AT command parse library example V0.2 3 | * 4 | * you can input: 5 | * +++ :without crlf to into AT mode 6 | * AT+MODE=? or AT+MODE=CLIENT or SERVER 7 | * AT+NAME=? or AT+NAME=hello 8 | * AT+SPEED=? or AT+SPEED=12 9 | * quit : to quit AT mode 10 | * 11 | * template: 12 | * at_template at_tmp[] = { 13 | * { cmd_string1, func1 }, 14 | * { cmd_string2, func2 }, 15 | * { cmd_string3, func3 }, 16 | * { "", NULL}, 17 | * }; 18 | * 19 | * func template: 20 | * void func (char *param) { 21 | * if (param == NULL) { // is '?' command 22 | * //show value 23 | * } else { 24 | * //write param as value 25 | * //such as: speed = atoi (param); 26 | * //such as: at_string_write (name, param, sizeof (name)); 27 | * } 28 | * } 29 | * 30 | *************************************************************************/ 31 | 32 | /************************************************************************* 33 | * DFRobot_utility offer function: 34 | * serialReads (); 35 | * 36 | * DF_AT_parse offer function: 37 | * at_string_write (char *dest, char *src, int length); 38 | * parse_at_string (char *buf, at_template at_temp); 39 | * at_template struct 40 | *************************************************************************/ 41 | 42 | #include 43 | #include 44 | 45 | #define BUF_MAX 36 46 | 47 | #define SERVER 0 48 | #define CLIENT 1 49 | 50 | char at_string[BUF_MAX]; 51 | 52 | int mode = SERVER; 53 | int speed = 10; 54 | char name [10]= "DFRobot"; 55 | 56 | // 57 | void mode_func (char *param) { 58 | if (param == NULL) { 59 | Serial.print ("mode="); 60 | Serial.println (mode ? "CLIENT" : "SERVER"); 61 | } else { 62 | if (strcmp (param, "SERVER") == 0) { 63 | mode = SERVER; 64 | Serial.println ("ok"); 65 | } else if (strcmp (param, "CLIENT") == 0) { 66 | mode = CLIENT; 67 | Serial.println ("ok"); 68 | } else { 69 | Serial.println ("error, no this mode"); 70 | } 71 | } 72 | } 73 | 74 | // 75 | void name_func (char *param) { 76 | if (param == NULL) { 77 | Serial.print ("name="); 78 | Serial.println (name); 79 | } else { 80 | at_string_write (name, param, sizeof (name)); 81 | if (strlen (param) >= sizeof (name)) { 82 | Serial.println ("error: name too long, will cut"); 83 | Serial.print ("name="); 84 | Serial.println (name); 85 | } 86 | Serial.println ("ok"); 87 | 88 | } 89 | } 90 | 91 | // 92 | void speed_func (char *param) { 93 | if (param == NULL) { 94 | Serial.print ("speed="); 95 | Serial.println (speed); 96 | } else { 97 | speed = atoi (param); 98 | Serial.println ("ok"); 99 | } 100 | } 101 | 102 | //at template struct 103 | at_template at_tmp[] = { 104 | {"MODE", mode_func}, 105 | {"NAME", name_func}, 106 | {"SPEED", speed_func}, 107 | {"", NULL}, 108 | }; 109 | 110 | 111 | boolean isLogin = false; 112 | 113 | // 114 | void setup () { 115 | Serial.begin (115200); 116 | } 117 | 118 | // 119 | void loop () { 120 | int leng = serialReads (Serial, (uint8_t*)at_string, BUF_MAX, 5); 121 | if (leng) { 122 | if (isLogin) { 123 | if (strncmp (at_string, "quit", 4) == 0) { 124 | isLogin = false; 125 | Serial.println ("quit AT mode"); 126 | } else { 127 | parse_at_string (at_string, at_tmp); 128 | } 129 | } else { 130 | if (strcmp (at_string, "+++") == 0) { 131 | isLogin = true; 132 | Serial.println ("into AT mode"); 133 | } else { 134 | Serial.print (strlen (at_string)); 135 | Serial.print(":"); 136 | Serial.println (at_string); 137 | } 138 | } 139 | } 140 | } 141 | 142 | -------------------------------------------------------------------------------- /examples/io_sample/io_sample.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | digitalOut led (13); 4 | digitalIn button (12); 5 | 6 | void setup () { 7 | } 8 | 9 | void loop () { 10 | led = button; 11 | } 12 | 13 | /* 14 | * digitalWrite (led, digitalRead (button)); 15 | */ 16 | -------------------------------------------------------------------------------- /examples/rainbow_test/rainbow_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | @file rainbow_test.ino 3 | @author lisper (lisper.li@dfrobot.com) 4 | @license LGPLv3 (see license.txt) 5 | 6 | Copyright (C) 2014 DFRobot 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #define PIN 9 13 | #define LED_LENG 7 14 | 15 | 16 | // Parameter 1 = number of pixels in strip 17 | // Parameter 2 = pin number (most are valid) 18 | // Parameter 3 = pixel type flags, add together as needed: 19 | // NEO_RGB Pixels are wired for RGB bitstream 20 | // NEO_GRB Pixels are wired for GRB bitstream 21 | // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) 22 | // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip) 23 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_LENG, PIN, NEO_GRB + NEO_KHZ800); 24 | 25 | void setup() { 26 | Serial.begin (9600); 27 | strip.begin(); 28 | strip.show(); // Initialize all pixels to 'off' 29 | } 30 | 31 | 32 | // 33 | void loop () { 34 | static float add = 0; 35 | add += 0.01; 36 | float h = add; 37 | for (int i=0; i 2 | #include 3 | 4 | rgbLed led (3,4,5); 5 | 6 | void setup () { 7 | } 8 | 9 | int a = 0; 10 | 11 | void loop () { 12 | a++; 13 | if (a > 360) 14 | a = 0; 15 | led.setColor (a); 16 | delay (1); 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /examples/serialReadCmd/serialReadCmd.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file serialReadCmd.ino 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | test read cmd from serial and get argc/argv 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | #include "DFRobot_utility.h" 13 | 14 | void setup () { 15 | Serial.begin (9600); 16 | } 17 | 18 | void loop () { 19 | serialReadCmd (Serial); 20 | if (argc > 0) { 21 | Serial.print ("cmd num is: "); 22 | Serial.println (argc); 23 | for (int i=0; i for digital pin 7 | * issticked (uint16_t key, uint16_t value); 8 | * -> for analog pin 9 | * issticked2 (uint16_t key, uint16_t left, uint16_t right); 10 | * -> for analog pin 11 | * 12 | */ 13 | 14 | #include "Arduino.h" 15 | 16 | extern uint16_t key_analog_range; 17 | extern uint16_t key_delay_time; 18 | 19 | 20 | //digital pin key 0 or 1 21 | boolean ispressed (uint16_t key, uint16_t state) { 22 | if (digitalRead (key) == state) { 23 | delay (key_delay_time); 24 | if (digitalRead (key) == state) 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | 31 | //analog pin key 32 | boolean issticked (uint16_t key, uint16_t range) { 33 | if (abs (analogRead (key) - range) < key_analog_range) { 34 | delay (key_delay_time); 35 | if (abs (analogRead (key) - range) < key_analog_range) 36 | return true; 37 | } 38 | return false; 39 | } 40 | 41 | //analog pin key 42 | boolean issticked2 (uint16_t key, uint16_t left, uint16_t right) { 43 | uint16_t value = analogRead (key); 44 | if (value >= left && value < right) { 45 | delay (key_delay_time); 46 | value = analogRead (key); 47 | if (value >= left && value < right) 48 | return true; 49 | } 50 | return false; 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /keycheck.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file keycheck.h 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | key check for Arduino 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | 11 | 12 | function: ispressed (uint16_t key, uint16_t state); 13 | -> for digital pin 14 | issticked (uint16_t key, uint16_t value); 15 | -> for analog pin 16 | issticked2 (uint16_t key, uint16_t left, uint16_t right); 17 | -> for analog pin 18 | 19 | */ 20 | /**************************************************************************/ 21 | 22 | #ifdef __KEYCHECK__ 23 | #define __KEYCHECK__ 24 | 25 | 26 | uint16_t key_analog_range = 50; 27 | uint16_t key_delay_time = 20; 28 | 29 | 30 | //digital pin key 0 or 1 31 | boolean ispressed (uint16_t key, uint16_t state); 32 | 33 | //analog pin key 34 | boolean issticked (uint16_t key, uint16_t range); 35 | 36 | //analog pin key 37 | boolean issticked2 (uint16_t key, uint16_t left, uint16_t right); 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For DFRobot_utility 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | DFRobotCar KEYWORD1 10 | digitalOut KEYWORD1 11 | digitalIn KEYWORD1 12 | analogOut KEYWORD1 13 | analogIn KEYWORD1 14 | 15 | ####################################### 16 | # Methods and Functions (KEYWORD2) 17 | ####################################### 18 | 19 | changeDir KEYWORD2 20 | control KEYWORD2 21 | 22 | serialRead KEYWORD2 23 | serialReads KEYWORD2 24 | 25 | split KEYWORD2 26 | splitc KEYWORD2 27 | 28 | checksum_add KEYWORD2 29 | checksum_nadd KEYWORD2 30 | checksum_xor KEYWORD2 31 | checksum_add_n KEYWORD2 32 | checksum_nadd_n KEYWORD2 33 | checksum_xor_n KEYWORD2 34 | 35 | fill_uint16 KEYWORD2 36 | fill_uint16_bigend KEYWORD2 37 | 38 | # 39 | ####################################### 40 | # Constants (LITERAL1) 41 | ####################################### 42 | 43 | #LITERAL1 44 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /serialStream.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file read_serial.cpp 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | provide some useful function make it easy to read many data from serial 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | 15 | //call like : serialRead (Serial1, buffer, 12, 5) 16 | uint8_t serialRead (HardwareSerial theSerial, 17 | uint8_t *buf, uint8_t leng, uint8_t timeout) { 18 | int sub; 19 | if (theSerial.available ()) { 20 | for (sub=0; sub timeout) 24 | return sub; 25 | } 26 | buf[sub] = theSerial.read (); 27 | } 28 | return sub; 29 | } 30 | return 0; 31 | } 32 | 33 | uint8_t serialReads (HardwareSerial theSerial, 34 | uint8_t *buf, uint8_t leng, uint8_t timeout) { 35 | int sub; 36 | if (theSerial.available ()) { 37 | for (sub=0; sub timeout) { 41 | buf[sub] = '\0'; 42 | return sub; 43 | } 44 | } 45 | buf[sub] = theSerial.read (); 46 | } 47 | buf[sub] = '\0'; 48 | return sub; 49 | } 50 | return 0; 51 | } 52 | 53 | #if defined(__AVR_ATmega32U4__) 54 | 55 | //call like : serialRead (Serial, buffer, 12, 5) 56 | uint8_t serialRead (Serial_ theSerial, 57 | uint8_t *buf, uint8_t leng, uint8_t timeout) { 58 | int sub; 59 | if (theSerial.available ()) { 60 | for (sub=0; sub timeout) 64 | return sub; 65 | } 66 | buf[sub] = theSerial.read (); 67 | } 68 | return sub; 69 | } 70 | return 0; 71 | } 72 | 73 | uint8_t serialReads (Serial_ theSerial, 74 | uint8_t *buf, uint8_t leng, uint8_t timeout) { 75 | int sub; 76 | if (theSerial.available ()) { 77 | for (sub=0; sub timeout) { 81 | buf[sub] = '\0'; 82 | return sub; 83 | } 84 | } 85 | buf[sub] = theSerial.read (); 86 | } 87 | buf[sub] = '\0'; 88 | return sub; 89 | } 90 | return 0; 91 | } 92 | 93 | #endif 94 | 95 | 96 | #if defined(__AVR_ATmega32U4__) 97 | 98 | uint8_t serial1Read (uint8_t *buf, uint8_t leng) { 99 | int sub; 100 | if (Serial1.available ()) { 101 | for (sub=0; sub 14 | 15 | //call like : read_serial_with_timeout (Serial1, buffer, 12, 5) 16 | uint8_t serialRead (HardwareSerial the_serial, 17 | uint8_t *buf, uint8_t leng, uint8_t timeout); 18 | 19 | uint8_t serialReads (HardwareSerial the_serial, 20 | uint8_t *buf, uint8_t leng, uint8_t timeout); 21 | 22 | #if defined(__AVR_ATmega32U4__) 23 | //call like : read_serial_with_timeout (Serial, buffer, 12, 5) 24 | uint8_t serialRead (Serial_ the_serial, 25 | uint8_t *buf, uint8_t leng, uint8_t timeout); 26 | 27 | uint8_t serialReads (Serial_ the_serial, 28 | uint8_t *buf, uint8_t leng, uint8_t timeout); 29 | #endif 30 | 31 | 32 | #if defined(__AVR_ATmega32U4__) 33 | uint8_t serial1Read (uint8_t *buf, uint8_t leng); 34 | //send data to serial1 35 | void serial1Write (uint8_t *buf, uint8_t leng); 36 | #endif 37 | 38 | #if defined(__AVR_ATmega2560__) 39 | //send data to serial2 40 | void serial2Write (uint8_t *theBuf, uint8_t leng); 41 | #endif 42 | 43 | //print data to PC in hex for test 44 | void printHex (uint8_t *thebuf, uint8_t leng); 45 | 46 | // 47 | void pauseSerial (uint16_t delayTime); 48 | // 49 | void pauseSerial (); 50 | 51 | #if defined(__AVR_ATmega32U4__) 52 | void pauseSerial (Serial_ the_serial, uint16_t delayTime); 53 | #endif 54 | 55 | void pauseSerial (HardwareSerial the_serial, uint16_t delayTime); 56 | 57 | 58 | -------------------------------------------------------------------------------- /split.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file split.cpp 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | build command string cmdstr 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include 15 | #include "split.h" 16 | 17 | //#define ARGV_SIZE 8 18 | //#define CMD_BUF_SIZE 32 19 | 20 | uint8_t argc; 21 | char cmdBuf[CMD_BUF_SIZE]; 22 | char *argv[ARGV_SIZE]; 23 | 24 | // 25 | void serialReadCmd (HardwareSerial theSerial) { 26 | int leng = serialReads (theSerial, (uint8_t*)cmdBuf, CMD_BUF_SIZE, 4); 27 | if (leng) { 28 | argc = split (argv, cmdBuf, ARGV_SIZE); 29 | } else { 30 | argc = 0; 31 | } 32 | } 33 | 34 | //split str to cmdstr by space 35 | int split (char **cmdstr, char *str, int leng) { 36 | int i; 37 | for (i=0; *str && i 14 | 15 | #define ARGV_SIZE 8 16 | #define CMD_BUF_SIZE 64 17 | 18 | extern uint8_t argc; 19 | extern char cmdBuf[CMD_BUF_SIZE]; 20 | extern char *argv[ARGV_SIZE]; 21 | 22 | // 23 | void serialReadCmd (HardwareSerial); 24 | 25 | //split str to cmdstr by space 26 | int split (char **cmdstr, char *str, int leng); 27 | 28 | //split str to cmdstr by space and delimiter 29 | int splitc (uint8_t **cmdstr, uint8_t *str, char delimiter, int leng); 30 | 31 | -------------------------------------------------------------------------------- /utility/DFCharBuffer.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFCharBuffer.cpp 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | queue, stack, ring buffer for character based on array 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include 15 | 16 | DFCharBuffer::DFCharBuffer (uint16_t theSize, boolean theMode) { 17 | buffer = new char [size]; 18 | size = theSize; 19 | head = 1; 20 | tail = 0; 21 | isOverMode = theMode; 22 | }; 23 | void DFCharBuffer::setMode (boolean theMode) { 24 | isOverMode = theMode; 25 | }; 26 | DFCharBuffer::~DFCharBuffer () { 27 | delete [] buffer; 28 | }; 29 | 30 | // 31 | int DFCharBuffer::writeFromHead (char data) { 32 | uint16_t temp = head; 33 | INDEX_FOR (temp, size); 34 | if (temp == tail) { 35 | if (isOverMode) { 36 | buffer[head] = data; 37 | head = temp; 38 | INDEX_FOR (tail, size); 39 | } else { 40 | return -1; 41 | } 42 | } else { 43 | buffer[head] = data; 44 | head = temp; 45 | } 46 | return 0; 47 | }; 48 | 49 | // 50 | int DFCharBuffer::writeFromTail (char data) { 51 | uint16_t temp = tail; 52 | INDEX_BACK (temp, size); 53 | if (temp == head) { 54 | if (isOverMode) { 55 | buffer[tail] = data; 56 | tail = temp; 57 | INDEX_BACK (head, size); 58 | } else { 59 | return -1; 60 | } 61 | } else { 62 | buffer[tail] = data; 63 | tail = temp; 64 | } 65 | return 0; 66 | }; 67 | 68 | // 69 | int DFCharBuffer::readFromHead () { 70 | uint16_t temp = head; 71 | INDEX_BACK (temp, size); 72 | 73 | if (temp == tail) { 74 | return -1; 75 | } else { 76 | head = temp; 77 | return buffer[head]; 78 | } 79 | }; 80 | 81 | // 82 | int DFCharBuffer::readFromTail () { 83 | uint16_t temp = tail; 84 | INDEX_FOR (temp, size); 85 | if (temp == head) { 86 | return -1; 87 | } else { 88 | tail = temp; 89 | return buffer[tail]; 90 | } 91 | }; 92 | 93 | -------------------------------------------------------------------------------- /utility/DFCharBuffer.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFCharBuffer.h 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | ring, queue, stack buffer for character based on array 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #ifndef _DF_CHAR_BUFFER 14 | #define _DF_CHAR_BUFFER 15 | 16 | #include 17 | 18 | #define INDEX_FOR(x, max) (x = (x+1)%max) 19 | #define INDEX_BACK(x, max) (x==0 ? x = max-1 : x--) 20 | 21 | class DFCharBuffer { 22 | private: 23 | char *buffer; 24 | uint16_t head; 25 | uint16_t size; 26 | uint16_t tail; 27 | boolean isOverMode; 28 | public: 29 | DFCharBuffer (uint16_t theSize, boolean theMode = true) ; 30 | void setMode (boolean theMode) ; 31 | ~DFCharBuffer () ; 32 | // 33 | int writeFromHead (char data) ; 34 | 35 | // 36 | int writeFromTail (char data) ; 37 | 38 | // 39 | int readFromHead (); 40 | 41 | // 42 | int readFromTail (); 43 | }; 44 | #endif 45 | -------------------------------------------------------------------------------- /utility/DFCommon.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFCommon.cpp 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | some usefull function 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | 15 | 16 | // 17 | uint8_t decToInt2 (char *the_buf) { 18 | uint8_t value = 0; 19 | value += (the_buf[0]-'0') *10; 20 | value += (the_buf[1]-'0'); 21 | return value; 22 | } 23 | 24 | // 25 | uint16_t decToInt (char *theBuf, uint8_t theSize) { 26 | uint16_t value = 0; 27 | for (int i = 0; i= '0' && the_buf[0] <= '9') { 38 | value += (the_buf[0] - '0') * 16; 39 | } else { 40 | value += (the_buf[0] - 'A' + 10) * 16; 41 | } 42 | 43 | if (the_buf[1] >= '0' && the_buf[1] <= '9') { 44 | value += (the_buf[1] - '0'); 45 | } else { 46 | value += (the_buf[1] - 'A' + 10); 47 | } 48 | return value; 49 | } 50 | 51 | 52 | // 53 | uint8_t split_by_char (char *the_src, char the_char, char **the_des, uint8_t the_siz) { 54 | uint8_t src_len = strlen (the_src); 55 | uint8_t di=0; 56 | the_des[di++] = the_src; 57 | for (uint8_t si=0; si 17 | 18 | // 19 | uint8_t decToInt2 (char *the_buf); 20 | 21 | // 22 | uint16_t decToInt (char *theBuf, uint8_t theSize); 23 | 24 | // 25 | uint8_t hexToInt2 (char *the_buf); 26 | 27 | // 28 | uint8_t split_by_char (char *the_src, char the_char, char **the_des, uint8_t the_siz); 29 | 30 | // 31 | uint8_t split_by_comma (char *the_src, char **the_des, uint8_t the_siz); 32 | 33 | // 34 | uint8_t delete_crlf (char *the_buf); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /utility/DFTimer.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFTimer.cpp 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | timer library use millis () 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include "DFTimer.h" 15 | 16 | // 17 | DFTimer::DFTimer (void (*theTimeHandler)(), uint32_t theDelayTime) { 18 | isFirst = false; 19 | isRun = true; 20 | nowTime = millis (); 21 | timeHandlern = theTimeHandler; 22 | delayTime = theDelayTime; 23 | } 24 | 25 | // 26 | DFTimer::DFTimer (void (*theTimeHandler)(), uint32_t theDelayTime, uint32_t theStartTime) { 27 | isFirst = true; 28 | isRun = true; 29 | nowTime = millis (); 30 | timeHandlern = theTimeHandler; 31 | delayTime = theDelayTime; 32 | startTime = theStartTime; 33 | } 34 | 35 | // 36 | DFTimer::DFTimer (void (*theTimeHandler)(void*), uint32_t theDelayTime, void *theArgs) { 37 | isFirst = true; 38 | isRun = true; 39 | nowTime = millis (); 40 | timeHandler = theTimeHandler; 41 | delayTime = theDelayTime; 42 | args = theArgs; 43 | } 44 | 45 | // 46 | DFTimer::DFTimer (void (*theTimeHandler)(void*), uint32_t theDelayTime, uint32_t theStartTime, void *theArgs) { 47 | isFirst = true; 48 | isRun = true; 49 | nowTime = millis (); 50 | timeHandler = theTimeHandler; 51 | delayTime = theDelayTime; 52 | startTime = theStartTime; 53 | args = theArgs; 54 | } 55 | 56 | // 57 | void DFTimer::check () { 58 | if (!isRun) 59 | return; 60 | uint32_t divTime = millis () - nowTime; 61 | if (isFirst ? (divTime > startTime) : (divTime > delayTime)) { 62 | isFirst = false; 63 | nowTime = millis (); 64 | if (args) 65 | timeHandler (args); 66 | else 67 | timeHandlern (); 68 | } 69 | } 70 | 71 | // 72 | void DFTimer::stop () { 73 | isRun = false; 74 | } 75 | 76 | // 77 | void DFTimer::start () { 78 | isRun = true; 79 | nowTime = millis (); 80 | } 81 | 82 | -------------------------------------------------------------------------------- /utility/DFTimer.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DFTimer.h 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | timer library use millis () 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #ifndef _DF_TIMER 14 | #define _DF_TIMER 15 | 16 | #include 17 | 18 | class DFTimer { 19 | public: 20 | void *args; 21 | void check (); 22 | void start (); 23 | void stop (); 24 | DFTimer (void (*theTimeHandler)(), uint32_t theDelayTime); 25 | DFTimer (void (*theTimeHandler)(void*), uint32_t theDelayTime, void *args); 26 | DFTimer (void (*theTimeHandler)(), uint32_t theDelayTime, uint32_t startTime); 27 | DFTimer (void (*timeHandler)(void*), uint32_t delayTime, uint32_t startTime, void *args); 28 | private: 29 | uint32_t delayTime; 30 | uint32_t startTime; 31 | uint32_t nowTime; 32 | boolean isFirst; 33 | boolean isRun; 34 | void (*timeHandlern) (); 35 | void (*timeHandler) (void*); 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /utility/DF_AT_parse.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DF_AT_parse.cpp 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | AT command parse library 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | 14 | #include 15 | #include "DF_AT_parse.h" 16 | 17 | // 18 | int startWithAT (char* theBuf) { 19 | if (strncmp (theBuf, "AT+", 3) == 0) { 20 | return 1; 21 | } else { 22 | return 0; 23 | } 24 | } 25 | 26 | // 27 | int findChar (char* theBuf, char c) { 28 | for (uint8_t i=0; theBuf[i] != '\0'; i++) { 29 | if (theBuf[i] == c) { 30 | return i; 31 | } 32 | } 33 | return -1; 34 | } 35 | 36 | int endWithCRLF (char *theBuf) { 37 | for (int i=1; theBuf[i] != '\0'; i++) { 38 | if (theBuf[i-1] == '\r' && theBuf[i] == '\n') { 39 | return i-1; 40 | } 41 | } 42 | return -1; 43 | } 44 | 45 | void at_string_write (char *theBuf, char *cmd, int length) { 46 | strncpy (theBuf, cmd, length - 1); 47 | theBuf [length-1] = '\0'; 48 | } 49 | 50 | 51 | // 52 | void parse_command (char *theBuf, char *param, at_template *at_tmp) { 53 | for (uint8_t i=0; (at_tmp[i].cmd) != '\0'; i++) { 54 | if (strcmp (theBuf, at_tmp[i].cmd) == 0) { 55 | if (strncmp (param, "?", 1) == 0) { 56 | at_tmp[i].func (NULL); 57 | } else { 58 | at_tmp[i].func (param); 59 | } 60 | return; 61 | } 62 | } 63 | Serial.println ("no this command"); 64 | } 65 | 66 | // 67 | void parse_at_string (char *theBuf, at_template *at_tmp) { 68 | if (! startWithAT (theBuf)) { 69 | Serial.println ("not AT command"); 70 | return; 71 | } // 72 | int crlf_flag = endWithCRLF (theBuf); 73 | if (crlf_flag == -1) { 74 | Serial.println ("error, no crlf"); 75 | return ; 76 | } // 77 | theBuf[crlf_flag] = '\0'; 78 | theBuf += 3; 79 | int sub_eq = findChar (theBuf, '='); 80 | if (sub_eq == -1) { 81 | Serial.println ("error, lose \'=\'"); 82 | return ; 83 | } else if (sub_eq == 0) { 84 | Serial.println ("error, no command"); 85 | return ; 86 | } 87 | theBuf[sub_eq] = '\0'; 88 | char *param = theBuf+sub_eq+1; 89 | if (*param == '\0') { 90 | Serial.println ("error, no param"); 91 | return ; 92 | } // 93 | parse_command (theBuf, param, at_tmp); 94 | } 95 | 96 | 97 | -------------------------------------------------------------------------------- /utility/DF_AT_parse.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DF_AT_parse.h 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | AT command parse library 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #ifndef _DF_AT_PARSE 14 | #define _DF_AT_PARSE 15 | 16 | #include 17 | 18 | 19 | typedef struct at_template { 20 | char *cmd; 21 | void (*func) (char *); 22 | }at_template; 23 | 24 | 25 | void at_string_write (char* theBuf, char* cmd, int length); 26 | // 27 | void parse_at_string (char *theBuf, at_template *at_tmp); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /utility/DF_GPS.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DF_GPS.cpp 4 | @author lisper (leyapin@gmail.com, lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | parse gps gpgga protocol 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #define _debug 18 | 19 | 20 | DFGPS::DFGPS (Stream &theSerial) { 21 | _mySerial = &theSerial; 22 | } 23 | 24 | // check sum using xor 25 | uint8_t DFGPS::gpsCalcChecksum (char *array) { 26 | uint8_t sum = array[1]; 27 | for (uint8_t i=2; array[i] != '*'; i++) { 28 | sum ^= array[i]; 29 | } 30 | return sum; 31 | } 32 | 33 | //get gga checksum 34 | uint8_t DFGPS::gpsReadChecksum (char **the_str) { 35 | char *temp = the_str[14]; 36 | if (temp[0] != '*') { 37 | //printf ("error no *"); 38 | return 0; 39 | } 40 | uint8_t sum = hexToInt2 (temp+1); 41 | return sum; 42 | } 43 | 44 | // 45 | void DFGPS::gps_print_gpgga (gpgga_s *my_gpgga) { 46 | printf ("now time = %d:%d:%d\n", my_gpgga->utc.hour, my_gpgga->utc.minute, my_gpgga->utc.second); 47 | printf ("%c : %f\n", my_gpgga->ns, my_gpgga->latitude); 48 | printf ("%c : %f\n", my_gpgga->ew, my_gpgga->longitude); 49 | printf ("fix = %d\nnum = %d\n", my_gpgga->fix, my_gpgga->num); 50 | printf ("hdop = %f\n", my_gpgga->hdop); 51 | printf ("altitude = %f %c\n", my_gpgga->altitude, my_gpgga->a_units); 52 | printf ("level = %f %c\n", my_gpgga->level, my_gpgga->l_units); 53 | } 54 | 55 | // 56 | void DFGPS::gpgga (gpgga_s *gpgga_data) { 57 | /////////////////////////////////// 58 | gpgga_data->utc.hour = decToInt2 (gpsp[1]); 59 | gpgga_data->utc.minute = decToInt2 (gpsp[1]+2); 60 | gpgga_data->utc.second = decToInt2 (gpsp[1]+4); 61 | /////////////////////////////////// 62 | gpgga_data->latitude= atof (gpsp[2]); 63 | gpgga_data->ns = gpsp[3][0]; 64 | gpgga_data->longitude= atof (gpsp[4]); 65 | gpgga_data->ew = gpsp[5][0]; 66 | /////////////////////////////////// 67 | gpgga_data->fix = atoi (gpsp[6]); 68 | gpgga_data->num = atoi (gpsp[7]); 69 | gpgga_data->hdop = atof (gpsp[8]); 70 | gpgga_data->altitude = atof (gpsp[9]); 71 | gpgga_data->a_units = gpsp[10][0]; 72 | gpgga_data->level = atof (gpsp[11]); 73 | gpgga_data->l_units = gpsp[12][0]; 74 | } 75 | 76 | // 77 | int DFGPS::parse () { 78 | delete_crlf (gps_buffer); 79 | uint8_t sum = gpsCalcChecksum (gps_buffer); 80 | wordNum = split_by_comma (gps_buffer, gpsp, sizeof (gpsp)/sizeof (char*)); 81 | uint8_t check_result = gpsReadChecksum (gpsp); 82 | if (check_result != sum) { 83 | return 0; 84 | } else 85 | return 1; 86 | } 87 | 88 | void DFGPS::printGPGGA () { 89 | for (int i=0; iavailable ()) { 98 | if (_mySerial->read () == '$') { 99 | gps_buffer[0] = '$'; 100 | while (_mySerial->available () == 0); 101 | _mySerial->readBytes (gps_buffer+1, 5); 102 | if (strncmp (gps_buffer+1, "GPGGA", 5) == 0) { 103 | while (_mySerial->available () == 0); 104 | _mySerial->readBytesUntil ('$', gps_buffer+6, 100-6); 105 | #ifdef _debug 106 | Serial.println (gps_buffer); 107 | #endif 108 | //////////////////////////////////// 109 | parse (); 110 | return 1; 111 | } 112 | } 113 | } 114 | return 0; 115 | } 116 | 117 | char DFGPS::fixc () { 118 | return gpsp[6][0]; 119 | } 120 | 121 | uint8_t DFGPS::fix () { 122 | if (gpsp[6][0]) 123 | return gpsp[6][0]-'0'; 124 | else 125 | return 0; 126 | } 127 | 128 | boolean DFGPS::gpsAvailable () { 129 | if (fixc () == '1') 130 | return true; 131 | else 132 | return false; 133 | } 134 | 135 | boolean DFGPS::timeAvailable () { 136 | if (gpsp[1] != '\0') 137 | return true; 138 | else 139 | return false; 140 | } 141 | 142 | uint8_t DFGPS::getHour () { 143 | if (gpsp[1][0]) 144 | return decToInt2 (gpsp[1]); 145 | else 146 | return 0; 147 | } 148 | 149 | char *DFGPS::getTime () { 150 | return gpsp[1]; 151 | } 152 | 153 | uint8_t DFGPS::getMinute () { 154 | if (gpsp[1][0]) 155 | return decToInt2 (gpsp[1]+2); 156 | else 157 | return 0; 158 | } 159 | 160 | 161 | uint8_t DFGPS::getSecond () { 162 | if (gpsp[1][0]) 163 | return decToInt2 (gpsp[1]+4); 164 | else 165 | return 0; 166 | } 167 | 168 | 169 | double DFGPS::getLatitude () { 170 | double lat = (double)decToInt (gpsp[2], 2); 171 | return lat + atof (gpsp[2]+2) / 60.0; 172 | } 173 | 174 | char *DFGPS::getLatitudeStr () { 175 | return gpsp[2]; 176 | } 177 | 178 | double DFGPS::getLongitude () { 179 | double lon = (double)decToInt (gpsp[4], 3); 180 | return lon + atof (gpsp[4]+3) / 60.0; 181 | } 182 | 183 | char *DFGPS::getLongitudeStr () { 184 | return gpsp[4]; 185 | } 186 | 187 | char DFGPS::getNS () { 188 | return gpsp[3][0]; 189 | } 190 | 191 | char DFGPS::getEW () { 192 | return gpsp[5][0]; 193 | } 194 | 195 | uint8_t DFGPS::getNum () { 196 | return atoi (gpsp[7]); 197 | } 198 | 199 | char *DFGPS::getNumStr () { 200 | return gpsp[7]; 201 | } 202 | 203 | char *DFGPS::getHDOPStr () { 204 | return gpsp[8]; 205 | } 206 | 207 | double DFGPS::getHDOP () { 208 | return atof (gpsp[8]); 209 | } 210 | 211 | char *DFGPS::getAltidudeStr () { 212 | return gpsp[9]; 213 | } 214 | 215 | double DFGPS::getAltidude () { 216 | return atof (gpsp[9]); 217 | } 218 | 219 | char DFGPS::getAunits () { 220 | return gpsp[10][0]; 221 | } 222 | 223 | char *DFGPS::getLevelStr () { 224 | return gpsp[11]; 225 | } 226 | 227 | double DFGPS::getLevel () { 228 | return atof (gpsp[11]); 229 | } 230 | 231 | char DFGPS::getLunits () { 232 | return gpsp[12][0]; 233 | } 234 | 235 | -------------------------------------------------------------------------------- /utility/DF_GPS.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file DF_GPS.h 4 | @author lisper (leyapin@gmail.com, lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | serial GPS library 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #ifndef _DF_GPS 14 | #define _DF_GPS 15 | 16 | #include 17 | 18 | 19 | typedef struct { 20 | struct utc{ //1:UTC time 21 | uint8_t hour; 22 | uint8_t minute; 23 | uint8_t second; 24 | }utc; 25 | double latitude; //2: 26 | char ns; //3: 27 | double longitude; //4: 28 | char ew; //5: 29 | uint8_t fix; //6:gps status 0 1 2 3 4 5 6 7 8 9 30 | uint8_t num; //7: 31 | double hdop; //8:水平精度 32 | double altitude; //9:海拔高度 33 | char a_units; //10:单位M 34 | double level; //11:大地水准面高度 35 | char l_units; //12:单位M 36 | double diff_time; //13:差分GPS数据期限(RTCM SC-104),最后设立RTCM传送的秒数量,如不是差分定位则为空 37 | uint8_t diff_id; //14:差分参考基站标号 38 | } gpgga_s; 39 | 40 | #define GPS_BUF_SIZE 101 41 | #define GPSP_SIZE 15 42 | 43 | 44 | class DFGPS { 45 | private: 46 | Stream *_mySerial; 47 | 48 | // check sum using xor 49 | uint8_t gpsCalcChecksum (char *array); 50 | 51 | //get gga checksum 52 | uint8_t gpsReadChecksum (char **the_str); 53 | // 54 | void gps_print_gpgga (gpgga_s *my_gpgga); 55 | int parse (); 56 | uint8_t wordNum; 57 | 58 | public : 59 | DFGPS (Stream & mySerial); 60 | char gps_buffer[GPS_BUF_SIZE]; 61 | char *gpsp[GPSP_SIZE]; 62 | int read (); 63 | void printGPGGA (); 64 | void gpgga (gpgga_s *gpgga_data); 65 | 66 | boolean gpsAvailable (); 67 | boolean timeAvailable (); 68 | uint8_t getHour (); 69 | 70 | char *getTime (); 71 | uint8_t getMinute (); 72 | 73 | uint8_t getSecond (); 74 | 75 | double getLatitude (); 76 | char *getLatitudeStr (); 77 | double getLongitude (); 78 | char *getLongitudeStr (); 79 | 80 | char getNS (); 81 | 82 | char getEW (); 83 | 84 | char fixc (); 85 | uint8_t fix (); 86 | uint8_t getNum (); 87 | char *getNumStr (); 88 | 89 | char *getHDOPStr (); 90 | double getHDOP (); 91 | char *getAltidudeStr (); 92 | double getAltidude (); 93 | 94 | char getAunits (); 95 | 96 | char *getLevelStr (); 97 | double getLevel (); 98 | 99 | char getLunits (); 100 | 101 | 102 | }; 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /utility/SerialProtocol.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file SerialProtocol.cpp 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | serial protocol with checksum 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include 15 | #include "SerialProtocol.h" 16 | 17 | #define PRO_MAX 19 //max data length 18 | 19 | //#define _debug //send error message to Serial 20 | 21 | // 22 | uint8_t getChecksum (uint8_t *theData, uint8_t leng) { 23 | uint8_t sum = 0; 24 | for (uint8_t i=0; i PRO_MAX) 61 | return -1; 62 | uint8_t pro_size = theLeng+3; 63 | uint8_t buffer[pro_size]; 64 | int leng = serialRead (Serial, buffer, pro_size, 4); 65 | if (leng == 0) { 66 | return 0; 67 | } 68 | 69 | 70 | if (leng != pro_size) { 71 | #ifdef _debug 72 | Serial.print ("data leng error:"); 73 | printHex (buffer, leng); 74 | #endif 75 | return -2; 76 | } 77 | if (!checksum (buffer, leng)) { 78 | #ifdef _debug 79 | Serial.println ("checksum error"); 80 | printHex (buffer, leng); 81 | Serial.print ("checksum is:"); 82 | Serial.println (getChecksum (buffer, leng-1), HEX); 83 | #endif 84 | return -3; 85 | } 86 | memcpy ((void*)theData, (void*)(buffer+2), theLeng); 87 | } 88 | 89 | 90 | -------------------------------------------------------------------------------- /utility/SerialProtocol.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file SerialProtocol.h 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | serial protocol with checksum 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #ifndef _SERIAL_PROTOCOL 14 | #define _SERIAL_PROTOCOL 15 | 16 | #include 17 | 18 | // 19 | void sendData (void *theData, uint8_t ); 20 | 21 | // 22 | int readData (void *theData, uint8_t); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /utility/rgbLed.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file rbgLed.cpp 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | rgb led library 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include 15 | 16 | 17 | rgbLed::rgbLed (uint8_t rPin, uint8_t gPin, uint8_t bPin) { 18 | rPin = rPin; 19 | gPin = gPin; 20 | bPin = bPin; 21 | pinMode (rPin, OUTPUT); 22 | pinMode (gPin, OUTPUT); 23 | pinMode (bPin, OUTPUT); 24 | } 25 | 26 | void rgbLed::setColorRGB (uint8_t rV, uint8_t gV, uint8_t bV) { 27 | analogWrite (rPin, rV); 28 | analogWrite (gPin, gV); 29 | analogWrite (bPin, bV); 30 | } 31 | 32 | void rgbLed::setColor (uint16_t color) { 33 | float hsb[3] = {(float)color, 1, 1}; 34 | uint8_t rgb[3]; 35 | HSB2RGB (hsb, rgb); 36 | setColorRGB (rgb[0], rgb[1], rgb[2]); 37 | 38 | } 39 | 40 | void rgbLed::setColor (uint16_t color, float s, float b) { 41 | float hsb[3] = {color, s, b}; 42 | uint8_t rgb[3]; 43 | HSB2RGB (hsb, rgb); 44 | setColorRGB (rgb[0], rgb[1], rgb[2]); 45 | } 46 | -------------------------------------------------------------------------------- /utility/rgbLed.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file rbgLed.h 4 | @author lisper (leyapin@gmail.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | rgb led library 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #ifndef _DF_RGBLED 14 | #define _DF_RGBLED 15 | 16 | class rgbLed { 17 | public: 18 | uint8_t rPin; 19 | uint8_t gPin; 20 | uint8_t bPin; 21 | rgbLed (uint8_t rPin, uint8_t gPin, uint8_t bPin); 22 | void setColorRGB (uint8_t rV, uint8_t gV, uint8_t bV); 23 | void setColor (uint16_t color); 24 | void setColor (uint16_t color, float s, float b); 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /utility/urm37.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file urm37.cpp 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | urm37 library use serial 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | #include 14 | #include 15 | #include "urm37.h" 16 | 17 | 18 | uint8_t distCmd[4] = { 19 | 0x22, 0x00, 0x00, 0x22}; 20 | uint8_t dataBuf[4]; 21 | 22 | uint16_t urm37GetDist () { 23 | for (int i=0; i<4; i++) { 24 | Serial.write (distCmd[i]); 25 | } 26 | int leng = serialRead (Serial, dataBuf, 4, 4); 27 | if (leng) { 28 | //printHex (dataBuf, leng); 29 | if (dataBuf[0] == 0xff) { 30 | //Serial.println ("error!"); 31 | return 0; 32 | } else { 33 | uint16_t dist = dataBuf[1]*255+dataBuf[2]; 34 | //Serial.print ("=="); 35 | //Serial.println (dist); 36 | return (dist); 37 | } 38 | } 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /utility/urm37.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file urm37.h 4 | @author lisper (lisper.li@dfrobot.com) 5 | @license LGPLv3 (see license.txt) 6 | 7 | urm37 library use serial 8 | 9 | Copyright (C) DFRobot - www.dfrobot.com 10 | */ 11 | /**************************************************************************/ 12 | 13 | 14 | #include 15 | uint16_t urm37GetDist (); 16 | 17 | --------------------------------------------------------------------------------