KSduino 

 The Device Network
Your IP Address is: 54.161.130.145

DOCUMENTATION

KSduino.h
Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
00002 /*
00003  * KSduino.h
00004  *
00005  * KSduino-library for Arduino ver 0.0.1
00006  *
00007  * Copyright (C) Kirill Scherba 2012 <kirill@scherba.ru>
00008  * 
00009  * KSduino Library is free software: you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License  as published by 
00011  * the Free Software Foundation, either version 2.1 of the License, or
00012  * (at your option) any later version.
00013  * 
00014  * KSduino-library is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017  * See the GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License along
00020  * with this program.  If not, see <http://opensource.org/licenses/LGPL-2.1>
00021  *
00022  */
00023 
00031 #ifndef KSduino_h
00032 #define KSduino_h
00033 
00034 #define PRINT_SERIAL_MESSAGES 0
00035 
00036 #define PACKETS_QUEUE_SIZE  32 //64
00037 #define OLD_QUEUES_PACKET  30 //60
00038 #define FOUND_QUEUES_PACKET  255
00039 
00040 #define __arduino__ 
00041 //#define __linux__
00042 
00043 #ifdef __arduino__
00044 #
00045 #define __W5100__
00046 //#define __ENC28J60__
00047 #
00048 #include <Arduino.h>
00049 #
00050 #ifdef __W5100__
00051 #include <SPI.h>
00052 #include <Ethernet.h>
00053 #include <EthernetUdp.h>
00054 #include <utility/w5100.h>
00055 #endif
00056 #
00057 #ifdef __ENC28J60__
00058 //#include <EtherCard.h>
00059 #include "KSduinoEthernetUdp_ethercard.h"
00060 #endif
00061 #
00062 #ifdef __W5100__
00063   #define __arduino__W5100__
00064 #endif
00065 #
00066 #ifdef __ENC28J60__
00067   #define __arduino__ENC28J60__
00068 #endif
00069 #
00070 #endif
00071 
00072 #ifdef __linux__
00073 #
00074 #include <stdio.h>
00075 #include "KSduinoEthernetUdp.h"
00076 #
00077 #endif
00078 
00091 typedef int (*ksdReceptionFunc) (int device_id, 
00092                                  char param, int num, 
00093                                  void *value, char *message);
00094                                  
00101 typedef int (*ksdBeforeReset) ();  // if return 1 - do reset, 0 - skip reset
00102 
00112 typedef int (*ksdLostPacketCallback) (int type, int packetNum);  
00113 
00114 typedef struct
00115 {
00116   byte packet;
00117   byte num;     
00118 }
00119 ksdPacketsQueue;
00120 
00121 
00134 class KSduino
00135 {
00136   public:
00137 
00145     KSduino (unsigned int devID, unsigned int devPwd, byte *serverAddr, 
00146              unsigned int serverP);
00147     
00148     void begin (byte *mac, byte *ip, byte *dns, byte *gateway, 
00149                 unsigned int port);
00150     void begin (byte *mac, byte *ip, byte *dns, unsigned int port);
00151     void begin (byte *mac, byte *ip, unsigned int port);
00152     void begin (byte *mac, unsigned int port);
00153     #ifndef  __arduino__ENC28J60__
00154     void begin (byte *mac, byte *ip, byte *dns, byte *gateway, byte* subnet, 
00155                 unsigned int port);
00156     #endif
00157 
00166     void sendOne (const char *parameter, const char *value, 
00167                   const char *message = NULL, 
00168                   int checkAnswer = 2);
00169     void sendOne (const char *parameter, byte value, 
00170                   const char *message = NULL, 
00171                   int checkAnswer = 2);
00172     void sendOne (const char *parameter, int value, 
00173                   const char *message = NULL, 
00174                   int checkAnswer = 2);
00175     void sendOne (const char *parameter, unsigned int value, 
00176                   const char *message = NULL, 
00177                   int checkAnswer = 2);
00178     void sendOne (const char *parameter, unsigned long value, 
00179                   const char *message = NULL, 
00180                   int checkAnswer = 2);
00181     void sendOne (const char *parameter, float value, 
00182                   const char *message = NULL, 
00183                   int checkAnswer = 2);
00184 
00193     void sendBuffer (const char *userData, int checkAnswer = 2);
00194     #ifdef __arduino__
00195     void sendBuffer (String userData, int checkAnswer = 2);
00196     #endif
00197     
00205     void beginPacket (void);
00206     void addParameter (const char *parameter, const char *value);
00207     void addParameter (const char *parameter, byte value);
00208     void addParameter (const char *parameter, int value);
00209     void addParameter (const char *parameter, unsigned int value);
00210     void addParameter (const char *parameter, unsigned long value);
00211     void addParameter (const char *parameter, float value);
00212     void addMessage (const char *message);
00213     int  endPacket (int checkAnswer = 2);
00222     void sendToOther (int device_id, int device_passwd, const char *parameter, 
00223                       const char *value, const char *message = NULL, 
00224                       int checkAnswer = 2);
00225     void sendToOther (int device_id, int device_passwd, const char *parameter, 
00226                       byte value, const char *message = NULL, 
00227                       int checkAnswer = 2);
00228     void sendToOther (int device_id, int device_passwd, const char *parameter, 
00229                       int value, const char *message = NULL, 
00230                       int checkAnswer = 2);
00231     void sendToOther (int device_id, int device_passwd, const char *parameter, 
00232                       unsigned int value, const char *message = NULL, 
00233                       int checkAnswer = 2);
00234     void sendToOther (int device_id, int device_passwd, const char *parameter, 
00235                       unsigned long value, const char *message = NULL, 
00236                       int checkAnswer = 2);
00237     void sendToOther (int device_id, int device_passwd, const char *parameter, 
00238                       float value, const char *message = NULL, 
00239                       int checkAnswer = 2);
00249     unsigned long getLostPackets (void);
00250     unsigned long getWrongAnswer (void);
00251     unsigned long getTotalPackets(void);
00252     unsigned long getLoopTime (void);
00253     int getBytesInQueue (void);
00254                 // Define callback function
00255     void setLostPacketCallback (ksdLostPacketCallback);
00256     #ifdef __arduino__W5100__
00257                 // Define callback function
00258     void setBeforeResetCallback (ksdBeforeReset beforeResetFunc);
00259     #endif
00260 
00269     void update (void);
00270                 // Define callback function
00271     void setReceptionCallback (ksdReceptionFunc receptionFunc);    
00274     #ifdef __arduino__ENC28J60__
00275     unsigned int _len;
00276     unsigned int _pos;
00277     #endif
00278     
00279   private:
00280   
00281     EthernetUDP Udp;
00282     ksdPacketsQueue _packetsQueue [PACKETS_QUEUE_SIZE];
00283     word _port;
00284     byte _idx;
00285     
00286     byte *serverIpAddr;
00287     unsigned int deviceID;
00288     unsigned int serverPort;
00289     unsigned int devicePasswd;
00290     
00291     byte packet;
00292     
00293     unsigned long _startTime;
00294     unsigned long _loopTime;
00295     unsigned long _lastSend;
00296     unsigned long _lostPackets;
00297     unsigned long _totalPackets;
00298     unsigned long _wrongAnswer;
00299 
00300     ksdReceptionFunc _receptionFunc;
00301     #ifdef __arduino__W5100__
00302     ksdBeforeReset _beforeResetFunc;
00303     #endif
00304     ksdLostPacketCallback _lostPacketCallback;
00305     
00306     int waitPacketAnswer (byte packet);
00307     int readPacket (int packetSize, char *buffer, int bufferSize);
00308     int getPacketNum (const unsigned char *buf, unsigned int bufferSize);
00309     int getAnswer ();
00310     int checkReception (int packet_code, char *packet_data);
00311     int pingServer ();
00312     int checkPacketsQueue (int packet = 0);
00313 
00314     #ifdef __arduino__W5100__
00315     char *_readbuf;
00316     int _readbufSize;
00317     int _readLen;
00318     SOCKET _s;
00319 
00320     uint8_t  _si_recvIP[4];
00321     uint16_t _si_recvPort;
00322                 
00323     int _Udp_endPacket();
00324     int parsePacket ();
00325     #endif
00326 
00327     #ifdef __arduino__
00328     void  softReset ();
00329     #endif
00330 };
00331 
00332 
00347 class KSduinoParameter
00348 {
00349   public:
00350     
00367     KSduinoParameter (KSduino *ksd, char *parameter_name, float parameter_accuracy = 0.00, uint16_t rt = 0, byte parameter_sendall = 0)
00368     {
00369       ksduino = ksd;
00370       
00371       sendall = parameter_sendall;
00372                         repeattime = rt;
00373       
00374       int len = strlen (parameter_name);
00375       _name = (char*) malloc (len+1);
00376       strcpy (_name, parameter_name);
00377       
00378       if (!sendall)
00379       {
00380         #define casebodyst(type) \
00381         { \
00382           lastvalue = malloc (sizeof (type)); \
00383           accuracy = malloc (sizeof (type)); \
00384           type *ac = (type *) accuracy; \
00385           *ac = (type) parameter_accuracy; \
00386         }
00387       
00388         switch ( toupper(_name[0]) )
00389         {
00390           case 'D':
00391           case 'B':
00392             casebodyst(byte);
00393               break;
00394           case 'I':
00395               casebodyst(int);
00396               break;
00397           case 'U':
00398               casebodyst(unsigned int);
00399               break;
00400           case 'L':
00401               casebodyst(unsigned long);
00402               break;
00403           case 'F':
00404               casebodyst(float);
00405               break;
00406         }
00407       }      
00408     }
00409     
00410 
00427     template <typename ValueType>
00428     int addParameter (ValueType value)
00429     {
00430       int added;
00431       if ( (added=check (value)) ) ksduino->addParameter (name(), value);
00432       return added;
00433     }
00434 
00435     
00442     char *name (void) 
00443                 { 
00444                         return _name; 
00445                 }
00446 
00447     
00456     template <typename ValueType>
00457     int check (ValueType value) 
00458     {
00459       if (sendall) return 1;
00460       
00461       byte show = 0;
00462       unsigned long tm;
00463       
00464       if (repeattime)
00465       {
00466         tm = millis();
00467         if ( (tm - lastaccess) > (unsigned long) repeattime*1000) show = 1; 
00468       }
00469       
00470       #define casebody(type) \
00471       { \
00472         type *lastval = (type*) lastvalue; \
00473         type *acc = (type*) accuracy; \        
00474         if ( (abs( (type) value - (type)*lastval ) > (type)*acc)  || show) \
00475         { \
00476           *lastval = (type) value; \
00477           show = 1; \
00478         } \
00479       }
00480       
00481       switch ( toupper(_name[0]) )
00482       {
00483         case 'D':
00484         case 'B':
00485             casebody(byte);
00486             break;
00487         case 'I':
00488             casebody(int);
00489             break;
00490         case 'U':
00491             casebody(unsigned int);
00492             break;
00493         case 'L':
00494             casebody(unsigned long);
00495             break;
00496         case 'F':
00497             casebody(float);
00498             break;
00499       }
00500       if (show && repeattime) lastaccess = tm; 
00501       
00502       return show;
00503     }
00504     
00505 
00506     ~KSduinoParameter ()
00507     {
00508       if (!sendall)
00509       {
00510         free(_name);
00511         free(lastvalue);
00512         free(accuracy);
00513       }
00514     }
00515     
00516   private:
00517   
00518     byte sendall;
00519     char *_name;
00520     void *lastvalue;
00521     void *accuracy; 
00522     KSduino *ksduino;
00523     uint16_t repeattime;
00524     unsigned long lastaccess;    
00525 };
00526 
00527 #endif
Created with KSNews

HOME | CONTACTS | INFORMER | SHORTENER | DOCUMENTATION | HELP

Copyright © 2012-2013, The KSduino Project