Adam Resnick / CNCAirbrush

Dependents:   CNCAirbrushCode

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wifly.h Source File

Wifly.h

00001 /**
00002 * @author Steven Rhodes
00003 *
00004 * @section LICENSE
00005 *
00006 * Copyright (c) 2012 Steven Rhodes
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy
00009 * of this software and associated documentation files (the "Software"), to deal
00010 * in the Software without restriction, including without limitation the rights
00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 * copies of the Software, and to permit persons to whom the Software is
00013 * furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included in
00016 * all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 * THE SOFTWARE.
00025 *
00026 * @section DESCRIPTION
00027 *
00028 * CNCAirbrush Berkeley ME102B Project
00029 *
00030 */
00031 
00032 #ifndef WIFLY_H
00033 #define WIFLY_H
00034 
00035 #include "mbed.h"
00036 #include "CBuffer.h"
00037 #include "CmdBuffer.h"
00038 #include "ImgBuffer.h"
00039 
00040 
00041 
00042 /** Wifly Class
00043  *
00044  * Example:
00045  * @code
00046  * #include "mbed.h"
00047  * #include "Wifly.h"
00048  *
00049  * Wifly wifly(p9, p10, p8);
00050  * Serial pc(USBTX, USBRX);
00051  *
00052  * int main()
00053  * {
00054  *   while(1){
00055  *     if(wifly.hasCmd()){
00056  *       Command * c;
00057 *        wifly.getCmd(c)
00058  *       pc.printf("Msg: %d\r\n", c->cmd);
00059        }
00060  * }
00061  * @endcode
00062  */
00063 class Wifly {
00064 
00065 public:
00066     
00067 
00068     /**
00069     * Constructor to create an adhoc network
00070     *
00071     * @param tx mbed pin to use for tx line of Serial interface
00072     * @param rx mbed pin to use for rx line of Serial interface
00073     * @param ssid ssid of the adhoc network which will be created
00074     * @param ip ip of the wifi module (default: "169.254.1.1")
00075     * @param netmask netmask (default: "255.255.0.0")
00076     * @param channel channel (default: "1")
00077     * @param baudrate speed of the communication (default: 460800)
00078     */
00079     Wifly(  PinName tx, PinName rx, PinName reset, char * ssid = "CNCAirbrush", char * ip = "169.254.1.1",
00080             char * netmask = "255.255.0.0", int channel = 1, int baudrate = 115200);
00081 
00082     /**
00083     * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
00084     * Useful to send a command to the module and wait a response.
00085     *
00086     *
00087     * @param str string to be sent
00088     * @param ACK string which must be acknowledge by the wifi module. If "ACK" == "NO", no string has to be acknoledged. (default: "NO")
00089     * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
00090     *
00091     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
00092     */
00093     bool send(char * str, char * ACK = "NO", char * res = NULL);
00094 
00095     /**
00096     * Attach an interrupt for the given command
00097     * 
00098     * @param cmd command number to trigger the interrupt for
00099     * @param func function to run on interrupt (must return void)
00100     * @param num interrupt number (I currently allow up to 3)
00101     *
00102     * @return true if interrupt has been attached
00103     */
00104     bool attach_interrupt(char cmd, void (*func)(Command *), int num);
00105 
00106     /**
00107     * Report that there's a command waiting to be read.
00108     *
00109     * @return true if there's a command waiting to be read.
00110     */
00111     bool hasCmd();
00112 
00113     /**
00114     * Fetch the most recent command
00115     *
00116     * @return the command on top of the stack
00117     */
00118     Command * getCmd();
00119 
00120     /**
00121     * Connect the wifi module to the ssid contained in the constructor.
00122     *
00123     * @return true if successfully sent
00124     */
00125     bool sendCmd(Command * c);
00126     
00127 
00128     /**
00129     * Create an adhoc network with the ssid contained in the constructor
00130     *
00131     * @return true if the network is well created, false otherwise
00132     */
00133     bool createAdhocNetwork();
00134 
00135     /**
00136     * Read a string if available
00137     *
00138     *@param str pointer where will be stored the string read
00139     */
00140     bool read(char * str);
00141 
00142 
00143     /**
00144     * Reset the wifi module
00145     */
00146     void reset();
00147 
00148     /**
00149     * Check if characters are available
00150     *
00151     * @return number of available characters
00152     */
00153     int readable();
00154 
00155     /**
00156     * Read a character
00157     *
00158     * @return the character read
00159     */
00160     char getc();
00161 
00162     /**
00163     * Flush the buffer
00164     */
00165     void flush();
00166 
00167     /**
00168     * Write a character
00169     *
00170     * @param the character which will be written
00171     */
00172     void putc(char c);
00173 
00174 
00175     /**
00176     * To enter in command mode (we can configure the module)
00177     *
00178     * @return true if successful, false otherwise
00179     */
00180     bool cmdMode();
00181 
00182 
00183     /**
00184     * To exit the command mode
00185     *
00186     * @return true if successful, false otherwise
00187     */
00188     bool exit();
00189 
00190 
00191      /**
00192      *  Attach a member function to call when a character is received.
00193      *
00194      *  @param tptr pointer to the object to call the member function on
00195      *  @param mptr pointer to the member function to be called
00196      */
00197     template<typename T>
00198     void attach(T* tptr, void (T::*mptr)(void)) {
00199         if ((mptr != NULL) && (tptr != NULL)) {
00200             rx.attach(tptr, mptr);
00201         }
00202     }
00203 
00204 
00205     /**
00206      * Attach a callback for when a character is received
00207      *
00208      * @param fptr function pointer
00209      */
00210     void attach(void (*fn)(void)) {
00211         if (fn != NULL) {
00212             rx.attach(fn);
00213         }
00214     }
00215 
00216 private:
00217     Serial wifi;
00218     DigitalOut reset_pin;
00219     bool wpa;
00220     bool adhoc;
00221     bool dhcp;
00222     char phrase[30];
00223     char ssid[30];
00224     char ip[20];
00225     char netmask[20];
00226     char imgpos;
00227     char last_char;
00228     int channel;
00229     FILE *fp;
00230     CBuffer buf_wifly;
00231     CmdBuffer cmd_buffer;
00232     ImgBuffer img_buffer;
00233 
00234     char char_to_num(char c);
00235     char num_to_char(char n);
00236     long str_to_long(char * str);
00237     float str_to_float(char * str);
00238     void long_to_str(char * str, long n);
00239     void float_to_str(char * str, float f);
00240     void attach_rx(bool null);
00241     void attach_img(bool null);
00242     void attach_cmd(bool null);
00243     void handler_rx(void);
00244     void handler_img(void);
00245     void handler_cmd(void);
00246     void write_img(void);
00247 
00248     FunctionPointer rx;
00249     bool gettingImg;
00250     bool gotImg;
00251 
00252 
00253 };
00254 
00255 #endif