Wataru Kunino / xbee

Dependents:   sample02_sw xbee_aging sample01_led sample04_sens

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xbee.h Source File

xbee.h

00001 #define ARM_MBED
00002 /*********************************************************************
00003 本ソースリストおよびソフトウェアは、ライセンスフリーです。
00004 利用、編集、再配布等が自由に行えますが、著作権表示の改変は禁止します。
00005 
00006                                Copyright (c) 2010-2014 Wataru KUNINO
00007                                http://www.geocities.jp/bokunimowakaru/
00008 *********************************************************************/
00009 
00010 /*********************************************************************
00011 インポート処理                  ※ライブラリ xbee.inoに必要な宣言です
00012 *********************************************************************/
00013 #ifndef XBEE_H
00014 #define XBEE_H
00015 
00016 #ifndef LCD_TYPE_H
00017     #define LCD_TYPE_H
00018     #ifdef ARDUINO
00019         #if defined(__AVR_ATmega32U4__)
00020             #define LEONARDO    // Arduino LEONARDを使用する場合に自動的に定義されます。
00021         #endif
00022     //  #define LCD_H       // 本ライブラリ動作を液晶表示する場合に定義する
00023     //  #define ADAFRUIT    // 使用する液晶が Adafruit LCD Sheild の時に定義する
00024     #else // H8 or ARM or PC
00025         #ifndef ARM_MBED    // H8 or PC
00026             #define LCD_H
00027         #endif
00028     #endif
00029 //  #define XBEE_WIFI   // XBee Wi-Fiを使用するときに定義する
00030 //  #define XBEE_WIFI_DEBUG
00031 //  #define LITE        // ライトモード(メモリ節約・機能制限)の時に定義する
00032     #define EASY_GPI    // xbee_gpi関数(リモートGPIO)を使用するときに定義する
00033     #define EASY_ADC    // xbee_adc関数(リモートADC)を使用するときに定義する
00034 #endif
00035 
00036 #ifndef XB_IMPORT_H
00037     #ifdef H3694
00038         #ifndef __3694_H__
00039             #include <3694.h>
00040         #endif
00041         #ifdef LCD_H
00042             #ifndef LCD_ROW_1
00043                 #include "lcd_h8.c"
00044             #endif
00045         #endif
00046         /*  // lcd_h8ライブラリの関数
00047         void lcd_delay(unsigned int data);
00048         void lcd_toggle_E(void);
00049         void lcd_cls(void);
00050         void lcd_home(void);
00051         void lcd_control(unsigned char disonoff, unsigned char curonoff, unsigned char curblink);
00052         void lcd_goto(unsigned char mesto);
00053         void lcd_shift(unsigned char data);
00054         void lcd_putch(char data);
00055         void lcd_putstr(const char *data);
00056         void lcd_disp_bin(unsigned char x);
00057         void lcd_disp_hex(unsigned char i);
00058         void lcd_disp_1(unsigned int x);
00059         void lcd_disp_2(unsigned int x);
00060         void lcd_disp_3(unsigned int x);
00061         void lcd_disp_5(unsigned int x);
00062         void lcd_init(void);
00063         */
00064     #elif ARDUINO
00065         //  #include <inttypes.h>
00066         #include "Arduino.h"
00067         //  #include "WProgram.h"   // #if ARDUINO < 100
00068         #ifdef LCD_H
00069             #ifndef ADAFRUIT
00070                 // 通常のキャラクタLCDの場合
00071                 #include <LiquidCrystal.h>
00072             #else
00073                 // Adafruit I2C接続LCDの場合(Adafruit_RGBLCDShieldライブラリが必要)
00074                 #include <Wire.h>
00075                 #include <Adafruit_MCP23017.h>
00076                 #include <Adafruit_RGBLCDShield.h>
00077             #endif
00078         #endif
00079         #ifdef XBEE_WIFI
00080             #include <SPI.h>
00081             #include <Ethernet.h>
00082             #include <EthernetUdp.h>
00083         #endif
00084         #ifndef LCD_ROW_1
00085             #define     LCD_ROW_1       0x00    //1行目先頭アドレス
00086             #define     LCD_ROW_2       0x40    //2行目先頭アドレス
00087             #define     LCD_ROW_3       0x14    //3行目先頭アドレス
00088             #define     LCD_ROW_4       0x54    //4行目先頭アドレス
00089         #endif
00090     #else
00091         #ifdef ARM_MBED // ARM
00092             #include "mbed.h"
00093             #ifdef DEBUG
00094                 #define LCD_H
00095                 #define ERRLOG
00096                 #define     LCD_ROW_1       0x00    //1行目先頭アドレス
00097                 #define     LCD_ROW_2       0x40    //2行目先頭アドレス
00098                 #define     LCD_ROW_3       0x14    //3行目先頭アドレス
00099                 #define     LCD_ROW_4       0x54    //4行目先頭アドレス
00100             #endif
00101         #else // PC
00102             #include <stdio.h>
00103             #include <stdlib.h>
00104             #include <string.h>
00105             #include <termios.h>
00106             #include <sys/signal.h>
00107             #include <sys/time.h>
00108             #include <fcntl.h>
00109             #include <unistd.h>
00110             #include <arpa/inet.h>
00111             #ifdef XBEE_WIFI
00112                 #include <sys/types.h>
00113                 #include <sys/socket.h>
00114                 #include <netinet/in.h>
00115                 #include <ctype.h>
00116                 #include <sys/ioctl.h>
00117                 #include <string.h>
00118             #endif
00119             #define BAUDRATE B9600
00120             #ifndef LCD_ROW_1
00121                 #include "lcd_pc.c"
00122             #endif
00123             #include <time.h>           // クロックタイマー用
00124         #endif
00125     #endif
00126     #define xbee_set_myaddress(adr) xbee_ratd_myaddress(adr)
00127 #endif
00128 
00129 /*********************************************************************
00130 定数の設定                      ※ライブラリ xbee.inoに必要な宣言です
00131 *********************************************************************/
00132 #ifndef XB_DEFINE_H
00133     #define XB_DEFINE_H
00134     #ifndef NAME
00135         #ifdef LITE // BeeBee Lite by 蘭
00136             #define     NAME        "BeeBee Lite"
00137             #define     COPYRIGHT   "by Wataru & Ran"
00138         #else
00139             #define     NAME        "ZB Coord"
00140             #define     COPYRIGHT   "by Wataru KUNINO"
00141         #endif
00142     #endif
00143 
00144     #ifdef H3694
00145         #define ERRLOG
00146         #define LED1_OUT    IO.PDR8.BIT.B0  // LED赤の接続ポート(エラー用)
00147         #define LED2_OUT    IO.PDR8.BIT.B1  // LED緑の接続ポート(動作確認用)
00148         #define LCD_EN      IO.PDR8.BIT.B6  // 液晶用電源
00149         #define BUTN        IO.PDR8.BIT.B2  // ボタンの接続ポート
00150         #define SCI_SIZE    256             // シリアルデータ長(大きすぎるとRAMが不足する)
00151         #define API_SIZE    128             // 受信用APIデータ長(32~255)
00152         #define API_TXSIZE  64              // 送信用APIデータ長(32~255)兼シリアル送信最大長
00153         #define CALL_SIZE   32              // xbee_rx_call用戻りデータ(10~256)
00154         #define XB_AT_SIZE  32              // ATコマンドの最大長
00155     #elif ARDUINO
00156     //  #define LED1_OUT    15              // 赤色LED(エラー用)用デジタルポート(15=analog 1)
00157     //  #define LED2_OUT    16              // 緑色LED(動作確認用)用デジタルポート(16=analog 2)
00158     //  #define LCD_EN       6              // 液晶用電源デジタルポート
00159     //  #define BUTN        14              // ボタンの接続ポート(14 = analog 0)
00160         #ifdef LITE
00161             #define     API_SIZE    48      // 受信用APIデータ長(32~255)
00162             #define     API_TXSIZE  34      // 送信用APIデータ長(32~255)
00163             #define     CALL_SIZE   16      // xbee_rx_call用戻りデータ(10~256)
00164             #define     XB_AT_SIZE  16      // ATコマンドの最大長
00165         #else
00166             #define     CACHE_RES   2       // 応答時のキャッシュ数(無効にするには定義を消す)
00167             #define     API_SIZE    64      // 受信用APIデータ長(32~255)
00168             #define     API_TXSIZE  64      // 送信用APIデータ長(32~255)
00169             #define     CALL_SIZE   32      // xbee_rx_call用戻りデータ(10~256)
00170             #define     XB_AT_SIZE  32      // ATコマンドの最大長
00171         #endif
00172         #define LIGHT           1
00173         #define TEMP            2
00174         #define HUMIDITY        3
00175         #define WATT            4
00176         #define BATT            5
00177         #define DISABLE         0
00178         #define VENDER          1
00179         #define AIN             2
00180         #define DIN             3
00181         #define DOUT_L          4
00182         #define DOUT_H          5
00183     #else
00184         #ifdef ARM_MBED // ARM      <条件は調整していない。ほぼArduinoのまま>
00185             #define     CACHE_RES   3       // 応答時のキャッシュ数(無効にするには定義を消す)
00186             #define     API_SIZE    64      // 受信用APIデータ長(32~255)
00187             #define     API_TXSIZE  64      // 送信用APIデータ長(32~255)
00188             #define     CALL_SIZE   32      // xbee_rx_call用戻りデータ(10~256)
00189             #define     XB_AT_SIZE  32      // ATコマンドの最大長
00190             #define     delay(ms)       wait_millisec(ms)   // 関数名の複製
00191             #define LIGHT           1
00192             #define TEMP            2
00193             #define HUMIDITY        3
00194             #define WATT            4
00195             #define BATT            5
00196             #define DISABLE         0
00197             #define VENDER          1
00198             #define AIN             2
00199             #define DIN             3
00200             #define DOUT_L          4
00201             #define DOUT_H          5
00202         #else // PC
00203             // #define DEBUG                    // デバッグモード
00204             // #define DEBUG_TX                 // 送信パケットの表示
00205             // #define DEBUG_RX                 // 受信パケットの表示
00206             #define     ERRLOG                  // エラー時にログを出力
00207         //  #define     XBEE_ERROR_TIME         // エラー時のログに日時を付与
00208             #ifdef LITE
00209                 #define     API_SIZE    48      // 受信用APIデータ長(32~255)
00210                 #define     API_TXSIZE  34      // 送信用APIデータ長(32~255)
00211                 #define     CALL_SIZE   16      // xbee_rx_call用戻りデータ(10~256)
00212                 #define     XB_AT_SIZE  16      // ATコマンドの最大長
00213             #else
00214                 #define     CACHE_RES   5           // 応答時のキャッシュ数(無効にするには定義を消す)
00215                 #define     API_SIZE    128         // 受信用APIデータ長(32~255)
00216                 #define     API_TXSIZE  64          // 送信用APIデータ長(32~255)
00217                 #define     CALL_SIZE   64          // xbee_rx_call用戻りデータ(10~256)
00218                 #define     XB_AT_SIZE  32          // ATコマンドの最大長
00219                 #define     delay(ms)       wait_millisec(ms)   // 関数名の複製
00220             #endif
00221         #endif
00222     #endif
00223     #define     TIME_DEL    3               // デリミタ検出のタイムアウト時間(秒)
00224     #define     MODE_AUTO   0x00            // 自動受信モード
00225     #define     MODE_GPIN   0x92            // GPI data を受信するモード
00226     #define     MODE_UART   0x90            // UART data を受信するモード
00227     #define     MODE_UAR2   0x91            // UART data を受信するモード2(AO=1)
00228     #define     MODE_SENS   0x94            // XB Sensorを受信するモード(1wire専用→通常のSensorはATISを使用)
00229     #define     MODE_IDNT   0x95            // Node Identifyを受信するモード
00230     #define     MODE_RES    0x88            // ローカルATコマンドの結果を受信
00231     #define     MODE_RESP   0x97            // リモートATコマンドの結果を受信(仕様書はATNDで説明ATISもこれ)
00232     #define     MODE_MODM   0x8A            // Modem Statusを受信
00233     #define     MODE_TXST   0x8B            // UART Transmit Status を受信
00234     #define     MODE_BATT   0xE1            // (独自定義)バッテリステータス RAT%Vの応答時
00235                 // XBeeのFrame Typeに準拠する。
00236                 // 注意:モードを増やしたときはxbee_from_acumとxbee_rx_call内の対応を追加すること
00237                 // 独自定義の時はxbee_rx_call内のみ。
00238                 // MODE値はenum定義にしない。
00239                 
00240     #define     STATUS_OK       0x00        // ATコマンドの結果がOK
00241     #define     STATUS_ERR      0x01        // ATコマンドの結果がERROR
00242     #define     STATUS_ERR_AT   0x02        // 指定されたATコマンドに誤りがある
00243     #define     STATUS_ERR_PARM 0x03        // 指定されたパラメータに誤りがある
00244     #define     STATUS_ERR_AIR  0x04        // リモートATコマンドの送信の失敗(相手が応答しない)
00245     
00246     #define     MODM_RESET      0x01        // ローカルのXBeeがリセットした
00247     #define     MODM_WATCHDOG   0x02        // ローカルのXBeeがWatch dogタイマーによってリセットした
00248     #define     MODM_JOINED     0x03        // (RouterまたはEnd Deviceで使用しているときに)ネットワークJoinした
00249     #define     MODM_LEFT       0x04        // ネットワークからdis_assosiateした
00250     #define     MODM_STARTED    0x06        // (coordinatorで使用しているときに)Coordinatorを開始した
00251     
00252     #define     DEV_TYPE_XBEE   0x00        // XBeeモジュール
00253     #define     DEV_TYPE_RS232  0x05        // RS-232Cアダプタ
00254     #define     DEV_TYPE_SENS   0x07        // Sensor (1wire専用)
00255     #define     DEV_TYPE_WALL   0x08        // Wall Router
00256     #define     DEV_TYPE_SEN_LT     0x0E        // Sensor (照度・温度)
00257     #define     DEV_TYPE_SEN_LTH    0x0D        // Sensor (照度・温度・湿度)
00258     #define     DEV_TYPE_PLUG   0x0F        // Smart Plug
00259     
00260     #define     ZB_TYPE_COORD   0x21        // ZigBee Coordinator
00261     #define     ZB_TYPE_ROUTER  0x23        // ZigBee Router
00262     #define     ZB_TYPE_ENDDEV  0x29        // ZigBee End Device
00263     #define     XB_TYPE_NULL    0x00        // XBee Wi-Fi バージョン未取得
00264     #define     XB_TYPE_WIFI10  0x10        // XBee Wi-Fi Ver. 10xx
00265     #define     XB_TYPE_WIFI20  0x20        // XBee Wi-Fi Ver. 20xx
00266 
00267     #define     NET_ADR     FFFE            // ネットワークアドレス
00268 #endif
00269 
00270 
00271 /*********************************************************************
00272 型の定義
00273 *********************************************************************/
00274 
00275 #ifdef ARDUINO
00276 typedef struct{
00277 public:
00278     byte MODE;              // 受信モード(Frame Type)
00279     byte FROM[8];           // 送信元IEEEアドレス
00280     byte AT[2];             // ATコマンド
00281     byte ID;                // 応答パケットID(Frame ID)
00282     byte STATUS;            // 応答結果(0:OK 1:ERROR)/AT結果/UART状態
00283     union {                 // GPIOデータ
00284         byte BYTE[2];
00285         struct {            // バイト毎に下位ビットから代入(リトルエンディアン)
00286             byte D0 :1; byte D1 :1; byte D2 :1; byte D3 :1; // BYTE[1]
00287             byte D4 :1; byte D5 :1; byte D6 :1; byte D7 :1;
00288             byte    :1; byte    :1; byte D10:1; byte D11:1; // BYTE[0]
00289             byte D12:1; byte    :1; byte    :1; byte    :1;
00290         } PORT;
00291     } GPI;
00292     //  byte GPI[2];            // GPIOデータ
00293     unsigned int ADCIN[4];  // ADCデータ
00294     byte DATA[CALL_SIZE];   // ペイロードデータ/ZCLヘッダ+受信データ
00295 } XBEE_RESULT;              // 構造体の型名
00296 #endif
00297 #ifdef ARM_MBED
00298 typedef unsigned char byte;
00299 typedef struct{
00300 public:
00301     byte MODE;              // 受信モード(Frame Type)
00302     byte FROM[8];           // 送信元IEEEアドレス
00303     byte AT[2];             // ATコマンド
00304     byte ID;                // 応答パケットID(Frame ID)
00305     byte STATUS;            // 応答結果(0:OK 1:ERROR)/AT結果/UART状態
00306     union {                 // GPIOデータ
00307         byte BYTE[2];
00308         struct {            // バイト毎に下位ビットから代入(リトルエンディアン)
00309             byte D0 :1; byte D1 :1; byte D2 :1; byte D3 :1; // BYTE[1]
00310             byte D4 :1; byte D5 :1; byte D6 :1; byte D7 :1;
00311             byte    :1; byte    :1; byte D10:1; byte D11:1; // BYTE[0]
00312             byte D12:1; byte    :1; byte    :1; byte    :1;
00313         } PORT;
00314     } GPI;
00315     //  byte GPI[2];            // GPIOデータ
00316     unsigned int ADCIN[4];  // ADCデータ
00317     byte DATA[CALL_SIZE];   // 受信データ
00318 } XBEE_RESULT;              // 構造体の型名
00319 #endif
00320 
00321 /*********************************************************************
00322 アプリ向け hardware 提供関数
00323 *********************************************************************/
00324 
00325 /* GPIO */
00326     #ifndef ARDUINO
00327     #ifndef ARM_MBED
00328     byte led_red( const byte in );
00329     byte led_green( const byte in );
00330     byte button( void );
00331     #endif
00332     #endif
00333 
00334 /* Timer */
00335     #ifndef ARDUINO
00336     #ifndef ARM_MBED
00337     void wait_millisec( const int ms );
00338     #endif
00339     #endif
00340 
00341 /* misc */
00342     byte bytecmp(byte *s1, const byte *s2, byte size);
00343     #ifndef ARDUINO
00344     #ifndef ARM_MBED
00345         void strcopy(char *s1, const char *s2);
00346         void bytecpy(byte *s1, const byte *s2, byte size);
00347     #endif
00348     #endif
00349 
00350 /* LCD */
00351     #ifdef LCD_H
00352         void lcd_cls(void);
00353         void lcd_home(void);
00354         void lcd_control(byte disonoff, byte curonoff, byte curblink);
00355         void lcd_goto(const byte mesto);
00356         void lcd_shift(const byte data);
00357         void lcd_putch(const char data);
00358         void lcd_putstr(const char *data);
00359         void lcd_disp_bin(const byte x);
00360         void lcd_disp_hex(const byte i);
00361         void lcd_disp_1(const unsigned int x);
00362         void lcd_disp_2(unsigned int x);
00363         void lcd_disp_3(unsigned int x);
00364         void lcd_disp_5(unsigned int x);
00365         void lcd_init(void);
00366         void lcd_disp( const char *s );
00367     #endif
00368     
00369 /*********************************************************************
00370 private xbee ドライバ関数
00371 *********************************************************************/
00372 
00373 /* XBeeドライバ関数 */
00374 /*  byte xbee_at_tx(const char *at, const byte *value, const byte value_len);
00375     byte xbee_at_rx(byte *data);
00376     byte xbee_from_acum( const byte *data );
00377     byte xbee_gpi_acum( byte *data );
00378     byte xbee_uart_acum( byte *data );
00379     #ifdef EASY
00380         float xbee_sensor_acum( const byte *data, const enum xbee_sensor_type type );
00381     #endif
00382     byte xbee_tx_rx(const char *at, byte *data, byte len);
00383     void xbee_reset( void );
00384 */
00385 
00386 /* XBee用 UART出力API */
00387     #ifndef LITE
00388         byte xbee_putch( const char c );
00389     #endif
00390     byte xbee_putstr( const char *s );
00391     #ifndef LITE
00392         void xbee_disp_hex( const unsigned char i );
00393         void xbee_disp_1( const unsigned int x );
00394         void xbee_disp_2( unsigned int x );
00395         void xbee_disp_3(unsigned int x);
00396         void xbee_disp_5(unsigned int x);
00397     #endif
00398     void xbee_log( byte level, const char *err , const byte x );
00399 
00400 /*********************************************************************
00401 アプリ向け xbee 提供関数
00402 *********************************************************************/
00403 
00404     byte xbee_myaddress( byte *address );
00405     void xbee_address( const byte *address );
00406     byte xbee_atd( const byte *address );
00407     byte xbee_ratd(const byte *dev_address, const byte *set_address );
00408     byte xbee_ratd_myaddress(const byte *address);
00409     #define xbee_set_myaddress(adr) xbee_ratd_myaddress(adr)
00410     void xbee_from( byte *address );
00411     byte xbee_at(const char *in);
00412     byte xbee_rat(const byte *address, const char *in);
00413     byte xbee_rat_force(const byte *address, const char *in);
00414     byte xbee_uart(const byte *address, const char *in);
00415     byte xbee_bytes(const byte *address, const byte *in, byte len);
00416     byte xbee_atvr( void );
00417     byte xbee_atai( void );
00418     byte xbee_atcb( byte cb );
00419     unsigned short xbee_atop(byte *pan_id);
00420     byte xbee_atee_on(const char *key );
00421     byte xbee_atee_off( void );
00422     byte xbee_atnc( void );
00423     byte xbee_ratnc( const byte *address );
00424     byte xbee_atnj( const byte timeout );
00425     byte xbee_ratnj( const byte *address, const byte timeout );
00426     byte xbee_ping( const byte *address );
00427     int xbee_batt(const byte *address );
00428     byte xbee_batt_force(const byte *address );
00429     #ifdef ARDUINO  // Arduino用
00430         byte xbee_gpio_config(const byte *address, const byte port, const byte type );
00431     #else
00432         #ifdef ARM_MBED
00433             byte xbee_gpio_config(const byte *address, const byte port, const byte type );
00434         #else // PC/H8用
00435             byte xbee_gpio_config(const byte *address, const byte port, const enum xbee_port_type type );
00436         #endif
00437     #endif
00438     byte xbee_gpio_init( const byte *address );
00439     byte xbee_gpo( const byte *address, const byte port,const byte out );
00440     #ifdef EASY_GPI
00441         byte xbee_gpi(const byte *address, const byte port);
00442     #endif
00443     #ifdef EASY_ADC
00444         unsigned int xbee_adc(const byte *address, const byte port);
00445     #endif
00446     byte xbee_force( const byte *address );
00447     #ifdef ARDUINO  // Arduino用
00448         float xbee_sensor_result( XBEE_RESULT *xbee_result, const byte type);
00449     #else
00450         #ifdef ARM_MBED
00451             float xbee_sensor_result( XBEE_RESULT *xbee_result, const byte type);
00452         #else // PC/H8用
00453             float xbee_sensor_result( XBEE_RESULT *xbee_result, const enum xbee_sensor_type type );
00454             #ifdef EASY_SENSOR
00455                 xbee_sensor(const byte *address, const enum xbee_sensor_type type );
00456             #endif
00457         #endif
00458     #endif
00459     byte xbee_rx_call( XBEE_RESULT *xbee_result );
00460     void xbee_clear_cache(void);
00461     byte xbee_init( const byte port );
00462     byte xbee_reset( void );
00463     byte xbee_end_device(const byte *address, byte sp, byte ir, const byte pin);
00464     #ifndef LITE
00465         byte xbee_i2c_init(const byte *address);
00466         byte xbee_i2c_read(const byte *address, byte adr, byte *rx, byte len);
00467         byte xbee_i2c_write(const byte *address, byte adr, byte *rx, byte len);
00468     #endif
00469     byte xbee_delay(unsigned int ms);
00470 #endif // XBEE_H
00471