大 田澤 / Group_Control_No2_3actions

Dependencies:   mbed

Committer:
Jumpei
Date:
Thu Mar 18 08:39:47 2021 +0000
Revision:
1:12c5a8179ad2
Parent:
0:65f9be2858c9
for Tazawa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tukudani 0:65f9be2858c9 1 // for ARM mbed Compiler
tukudani 0:65f9be2858c9 2 #define ARM_MBED
tukudani 0:65f9be2858c9 3 #include <mbed.h>
tukudani 0:65f9be2858c9 4 RawSerial _xbee_serial(/*SERIAL_TX*/p9, /*SERIAL_RX*/p10); // for STM32 Nucleo
tukudani 0:65f9be2858c9 5 //Serial _xbee_serial(/*SERIAL_TX*/p9, /*SERIAL_RX*/p10);
tukudani 0:65f9be2858c9 6 // Please set serial ports in conformity to your microcomputer board.
tukudani 0:65f9be2858c9 7 // 上記の括弧内をお手持ちのマイコンボードのシリアルピン名に書き換えてください。
tukudani 0:65f9be2858c9 8
tukudani 0:65f9be2858c9 9 /*********************************************************************
tukudani 0:65f9be2858c9 10 本ソースリストおよびソフトウェアは、ライセンスフリーです。(詳細は別記)
tukudani 0:65f9be2858c9 11 利用、編集、再配布等が自由に行えますが、著作権表示の改変は禁止します。
tukudani 0:65f9be2858c9 12
tukudani 0:65f9be2858c9 13 ZB Coord API for XBee
tukudani 0:65f9be2858c9 14
tukudani 0:65f9be2858c9 15 This XBee library drives Digi XBee ZB Modules in API Operation mode.
tukudani 0:65f9be2858c9 16 Most commands include remote communication's are supported by the
tukudani 0:65f9be2858c9 17 functions of this library. You can control XBee devices through the
tukudani 0:65f9be2858c9 18 API software modules in this, without using any AT commands.
tukudani 0:65f9be2858c9 19
tukudani 0:65f9be2858c9 20 Copyright (c) 2010-2014 Wataru KUNINO
tukudani 0:65f9be2858c9 21 http://www.geocities.jp/bokunimowakaru/
tukudani 0:65f9be2858c9 22 *********************************************************************/
tukudani 0:65f9be2858c9 23
tukudani 0:65f9be2858c9 24 /*
tukudani 0:65f9be2858c9 25 既知のバグ
tukudani 0:65f9be2858c9 26 [PC用/H8用/Arduino共通]
tukudani 0:65f9be2858c9 27 ・xbee_force/ATISでERRORもしくは正しい値が得られない場合がある。
tukudani 0:65f9be2858c9 28  一度、xbee_gpo/ATP104などで子機のGPIOなどに出力すると治る。
tukudani 0:65f9be2858c9 29   (XBeeモジュールの不具合と思われる)
tukudani 0:65f9be2858c9 30 ・XBee Smart Plug/XBee Sensor等でネットワーク参加直後、
tukudani 0:65f9be2858c9 31  正しい値を応答しない場合がある。上記と似た症状かもしれない。
tukudani 0:65f9be2858c9 32  一度、XBee Smart PlugのACコンセントを抜いて挿しなおせば治る。
tukudani 0:65f9be2858c9 33   (Smart Plug側のファームウェア「XBP24-ZB_2264」の問題と思われる)
tukudani 0:65f9be2858c9 34 ・Ver 1.90でLITEモードの仕様を変更。(濱崎氏によるBeeBee Lite)
tukudani 0:65f9be2858c9 35
tukudani 0:65f9be2858c9 36 ★XBee Wi-FiでのRAT実行時にXBeeから応答のパケットが無い場合がある。
tukudani 0:65f9be2858c9 37 IP_PINGは通る。
tukudani 0:65f9be2858c9 38 再現方法、動作中にXBeeをリセットし、その後にArduinoをリセット。
tukudani 0:65f9be2858c9 39 XBeeモジュールをリセットすると治る。
tukudani 0:65f9be2858c9 40 Arduino版で発覚。PC版は未確認。
tukudani 0:65f9be2858c9 41
tukudani 0:65f9be2858c9 42 [パソコン用]
tukudani 0:65f9be2858c9 43 ・おかげさまで特記事項なし。安定して動作します。
tukudani 0:65f9be2858c9 44
tukudani 0:65f9be2858c9 45 [Arduino用]
tukudani 0:65f9be2858c9 46 ・標準SDライブラリ使用時にメモリ不足になる場合があります。
tukudani 0:65f9be2858c9 47  そのような場合はxbee_liteをincludeし、LITEモードにしてください。
tukudani 0:65f9be2858c9 48
tukudani 0:65f9be2858c9 49 [ARM mbed用]
tukudani 0:65f9be2858c9 50 ・試作品です。バグ多数かもしれません。
tukudani 0:65f9be2858c9 51 ・STマイクロ製 Nucleo Board STM32F401で動作確認しています。
tukudani 0:65f9be2858c9 52 ・XBeeとの接続にはArduino用Wireless SD Shieldを使用します。
tukudani 0:65f9be2858c9 53  ※Nucleoボードの裏側のジャンパー抵抗の変更が必要です。
tukudani 0:65f9be2858c9 54 「SB14」をオープンにします(抵抗を取り外します。)
tukudani 0:65f9be2858c9 55 「SB62」をショートします。(半田で接続します。)
tukudani 0:65f9be2858c9 56 「SB63」をショートします。(半田で接続します。)
tukudani 0:65f9be2858c9 57 ・液晶はDF ROBOT製 LCD Keypad Shieldで動作確認しています。
tukudani 0:65f9be2858c9 58
tukudani 0:65f9be2858c9 59 [H8マイコン用]
tukudani 0:65f9be2858c9 60 ・Ver1.52以降は動作確認をしておらず、動作しない恐れがあります。
tukudani 0:65f9be2858c9 61  お使いになる方は連絡ください。動作確認版を個別リリースいたします。
tukudani 0:65f9be2858c9 62
tukudani 0:65f9be2858c9 63 制限事項
tukudani 0:65f9be2858c9 64 ・ソースはPC/H8/Arduino/ARM mbed用で共通ですが拡張子が異なります。
tukudani 0:65f9be2858c9 65  PC/H8「xbee.c」をArduino用は「xbee_arduino」フォルダ内のライブラリ
tukudani 0:65f9be2858c9 66  「xbee.h」をincludeしてください。
tukudani 0:65f9be2858c9 67 ・xbee_forceで指示を出してxbee_rx_callで待ち受ける用法を推奨します。
tukudani 0:65f9be2858c9 68 ・受信パケットの最大APIフレーム長は255Bytes等に制限されます。(64KB)
tukudani 0:65f9be2858c9 69 ・複数のXBeeデバイスの選択はアプリでIEEEアドレスを指定してください。
tukudani 0:65f9be2858c9 70 ・ショートアドレスは使っていません(IEEEアドレスで指定してください)
tukudani 0:65f9be2858c9 71 ・温度測定を行う場合は内部発熱分を補正(減算)する必要があります。
tukudani 0:65f9be2858c9 72  Digi純正のWall RouterやSmart Plugの場合は内部発熱の4℃を考慮して
tukudani 0:65f9be2858c9 73  測定値から減算必要があります。
tukudani 0:65f9be2858c9 74 ・送信者アドレスを得る関数xbee_fromは、xbee_rx_callで受信した場合に
tukudani 0:65f9be2858c9 75  正しく得られない場合があります。(受信キャッシュが保持されている時)
tukudani 0:65f9be2858c9 76  xbee_rx_callに渡したxbee_result.FROM[8]から送信者を得てください。
tukudani 0:65f9be2858c9 77
tukudani 0:65f9be2858c9 78 アプリ向け提供関数
tukudani 0:65f9be2858c9 79
tukudani 0:65f9be2858c9 80 byte xbee_myaddress( byte *address );
tukudani 0:65f9be2858c9 81 void xbee_address( const byte *address );
tukudani 0:65f9be2858c9 82 byte xbee_atd( const byte *address );
tukudani 0:65f9be2858c9 83 byte xbee_ratd(const byte *dev_address, const byte *set_address );
tukudani 0:65f9be2858c9 84 byte xbee_ratd_myaddress(const byte *address);
tukudani 0:65f9be2858c9 85 void xbee_from( byte *address );
tukudani 0:65f9be2858c9 86 byte xbee_at(const char *in);
tukudani 0:65f9be2858c9 87 byte xbee_rat(const byte *address, const char *in);
tukudani 0:65f9be2858c9 88 byte xbee_rat_force(const byte *address, const char *in);
tukudani 0:65f9be2858c9 89 byte xbee_uart(const byte *address, const char *in);
tukudani 0:65f9be2858c9 90 byte xbee_bytes(const byte *address, const byte *in, byte len);
tukudani 0:65f9be2858c9 91 byte xbee_atvr( void );
tukudani 0:65f9be2858c9 92 byte xbee_atai( void );
tukudani 0:65f9be2858c9 93 byte xbee_atcb( byte cb );
tukudani 0:65f9be2858c9 94 unsigned short xbee_atop(byte *pan_id);
tukudani 0:65f9be2858c9 95 byte xbee_atee_on(const char *key );
tukudani 0:65f9be2858c9 96 byte xbee_atee_off( void );
tukudani 0:65f9be2858c9 97 byte xbee_atnc( void );
tukudani 0:65f9be2858c9 98 byte xbee_ratnc( const byte *address );
tukudani 0:65f9be2858c9 99 byte xbee_atnj( const byte timeout );
tukudani 0:65f9be2858c9 100 byte xbee_ratnj( const byte *address, const byte timeout );
tukudani 0:65f9be2858c9 101 byte xbee_ping( const byte *address );
tukudani 0:65f9be2858c9 102 int xbee_batt(const byte *address );
tukudani 0:65f9be2858c9 103 byte xbee_batt_force(const byte *address );
tukudani 0:65f9be2858c9 104 byte xbee_gpio_config(const byte *address, const byte port, const enum xbee_port_type type ); //(PC/H8)
tukudani 0:65f9be2858c9 105 byte xbee_gpio_config(const byte *address, const byte port, const byte type ); //(arduino)
tukudani 0:65f9be2858c9 106 byte xbee_gpio_init( const byte *address );
tukudani 0:65f9be2858c9 107 byte xbee_gpo( const byte *address, const byte port,const byte out );
tukudani 0:65f9be2858c9 108 byte xbee_gpi(const byte *address, const byte port); //(EASY)
tukudani 0:65f9be2858c9 109 unsigned int xbee_adc(const byte *address, const byte port); //(EASY)
tukudani 0:65f9be2858c9 110 byte xbee_force( const byte *address );
tukudani 0:65f9be2858c9 111 float xbee_sensor_result( XBEE_RESULT *xbee_result, const enum xbee_sensor_type type ); //(PC/H8)
tukudani 0:65f9be2858c9 112 float xbee_sensor_result( XBEE_RESULT *xbee_result, const byte type); //(arduino)
tukudani 0:65f9be2858c9 113 byte xbee_rx_call( XBEE_RESULT *xbee_result );
tukudani 0:65f9be2858c9 114 void xbee_clear_cache(void);
tukudani 0:65f9be2858c9 115 xbee_sensor(const byte *address, const enum xbee_sensor_type type ); //(EASY)
tukudani 0:65f9be2858c9 116 byte xbee_init( const byte port );
tukudani 0:65f9be2858c9 117 byte xbee_reset( void );
tukudani 0:65f9be2858c9 118 byte xbee_end_device(const byte *address, byte sp, byte ir, const byte pin);
tukudani 0:65f9be2858c9 119 byte xbee_i2c_read(const byte *address, byte adr, byte *rx, byte len);
tukudani 0:65f9be2858c9 120 byte xbee_i2c_write(const byte *address, byte adr, byte *rx, byte len);
tukudani 0:65f9be2858c9 121 byte xbee_delay(unsigned int ms);
tukudani 0:65f9be2858c9 122 void lcd_disp( char *s );
tukudani 0:65f9be2858c9 123
tukudani 0:65f9be2858c9 124 主要な変更履歴
tukudani 0:65f9be2858c9 125 2010/08/15 0.10 - 初回の公開版をリリース 基本動作部分の実験用
tukudani 0:65f9be2858c9 126 2011/08/14 0.30 - ライブラリ化のためにAPI関数の内容を全面見直し
tukudani 0:65f9be2858c9 127 2012/04/07 0.80 - 総合試験の実施とバグ修正、仕様見直しなど
tukudani 0:65f9be2858c9 128 2012/04/22 0.91 - パソコン(cygwin)への移植
tukudani 0:65f9be2858c9 129 2012/07/15 1.00 - 基本機能(sample1~8)のデバッグ完了。正式版
tukudani 0:65f9be2858c9 130 2012/08/25 1.15 - Arduino用へ移植
tukudani 0:65f9be2858c9 131 - enum xbee_port_typeのADCをAINに変更
tukudani 0:65f9be2858c9 132 - 起動時にシリアル速度が設定されない不具合を修正
tukudani 0:65f9be2858c9 133 2012/11/25 1.20 - 電池電圧確認用コマンドを非同期型に変更
tukudani 0:65f9be2858c9 134 xbee_batt⇒xbee_batt_force。応答はxbee_rx_call
tukudani 0:65f9be2858c9 135 2012/12/09 1.30 - 液晶なしオプション(#define LCD_H)の追加
tukudani 0:65f9be2858c9 136 - Liteモード(#define LITE)の追加。ARDUINO版のみ
tukudani 0:65f9be2858c9 137 - PC版シリアルポートをcom1~com10に拡張
tukudani 0:65f9be2858c9 138 - グローバル変数アドレスのADR_MYとSADR_MYを廃止
tukudani 0:65f9be2858c9 139 2012/12/16 1.50 - Arduino版のライブラリ化
tukudani 0:65f9be2858c9 140 - コマンド応答待ち時に他のパケットをキャッシュ
tukudani 0:65f9be2858c9 141 (#define CACHE_RES 10でキャッシュが有効になる)
tukudani 0:65f9be2858c9 142 2012/12/17 1.51 - リモートATの応答確認にAT値からMODE値に変更
tukudani 0:65f9be2858c9 143 - H8のバグ対応(lcd_h8.cの2重呼び出し対策等)
tukudani 0:65f9be2858c9 144 2012/12/18 1.52 - [重要] 関数lcd()をlcd_disp()に変更。
tukudani 0:65f9be2858c9 145 - Arduino版ライブラリ側ハードLCD,LED,SWの無効化
tukudani 0:65f9be2858c9 146 2012/12/22 1.53 - [重要] atnjとratnjの戻り値を失敗時255に変更
tukudani 0:65f9be2858c9 147 - ATコマンドを送信する関数を追加
tukudani 0:65f9be2858c9 148 2012/12/23 1.54 - 1.53のエンバグの修正(テスト用printfの残留)
tukudani 0:65f9be2858c9 149 2012/12/30 1.55 - Arduino LEONARDOへの対応
tukudani 0:65f9be2858c9 150 - Arduino標準LCDの配線変更(DF ROBOT互換に)
tukudani 0:65f9be2858c9 151 2013/01/12 1.56 - xbee_result.GPI[0]とGPI[1]の戻り値を入れ替え
tukudani 0:65f9be2858c9 152 2013/01/14 1.57 - xbee_gpio_configでアドレス取得失敗時のバグ修正
tukudani 0:65f9be2858c9 153 2013/01/20 1.58 - xbee状態取得 xbee_ataiとxbee_atopコマンド追加
tukudani 0:65f9be2858c9 154 2013/04/03 1.60 - xbee_initによるXBeeリセット時の応答の廃棄処理
tukudani 0:65f9be2858c9 155 - エラー出力を stderrに変更PC用(のみ)
tukudani 0:65f9be2858c9 156 - xbee_result.GPI.PORT.Dnのエンバグ(1.56~)修正
tukudani 0:65f9be2858c9 157 2013/04/08 1.71 - [重要] Arduino xbee_rx_callを構造体受取に変更
tukudani 0:65f9be2858c9 158 - [重要] Arduino xbee_sensor_resultの引数の同上
tukudani 0:65f9be2858c9 159 - [重要] xbee_rx_callパケット選択受信機能を削除
tukudani 0:65f9be2858c9 160 - [重要] PC/H8 xbee_result.ADCをADCINに変更
tukudani 0:65f9be2858c9 161 - xbee_atに16進数のテキスト引数の処理のバグ修正
tukudani 0:65f9be2858c9 162 - xbee_atnjでjoin後にatnj=5を設定(5秒Join有効)
tukudani 0:65f9be2858c9 163 - PC/H8/Arduinoでライブラリを共通ソースに統合
tukudani 0:65f9be2858c9 164 2013/04/21 1.72 - コマンド応答をxbee_rx_callで得るxbee_rat_force
tukudani 0:65f9be2858c9 165 - テキストをシリアル送信するxbee_uartの追加
tukudani 0:65f9be2858c9 166 - ZigBeeデバイスタイプを取得するxbee_atvrの追加
tukudani 0:65f9be2858c9 167 - ネットワーク参加状況を確認するxbee_ataiの追加
tukudani 0:65f9be2858c9 168 - コミッショニング操作を行うxbee_atcbの追加
tukudani 0:65f9be2858c9 169 - ネットワークIDを取得するxbee_atopの追加
tukudani 0:65f9be2858c9 170 - 暗号化xbee_atee_onと解除xbee_atee_offの追加
tukudani 0:65f9be2858c9 171 2013/04/30 1.73 - PC版&Arduino版リリース
tukudani 0:65f9be2858c9 172 - 受信キャッシュオーバー時パケット廃棄(仕様変更)
tukudani 0:65f9be2858c9 173 2013/05/09 1.74 - 初期化時にAPモード1の設定を追加
tukudani 0:65f9be2858c9 174 - #define LCD_H有効時以外のログ呼び出し停止
tukudani 0:65f9be2858c9 175 - 子機に本機アドレスを設定するxbee_set_myaddress
tukudani 0:65f9be2858c9 176 2013/05/10 1.75 - End Deviceにスリープを設定するxbee_end_device
tukudani 0:65f9be2858c9 177 2013/05/18 1.76 - [重要] 戻り値を送信パケット番号PACKET_IDに変更
tukudani 0:65f9be2858c9 178 xbee_gpo,同_force,同_batt,同_rat_force,同_uart
tukudani 0:65f9be2858c9 179 - [重要] Arduino xbee_resultにxbee_result.ID追加
tukudani 0:65f9be2858c9 180 2013/05/25 1.77 - XBee Wi-Fi XBee IPによるリモートAT制御に対応
tukudani 0:65f9be2858c9 181 2013/06/30 1.78 - 全マスクGPIO入力時xbee_result.GPI.BYTEを0xFFFF
tukudani 0:65f9be2858c9 182 - GPIO出力(xbee_gpo)の利用可能ポート範囲を拡大
tukudani 0:65f9be2858c9 183 2013/08/28 1.79 - XBee Sensorのデバイス名(define)の追加
tukudani 0:65f9be2858c9 184 - xbee_end_deviceの親機のSP値を28秒固定に変更
tukudani 0:65f9be2858c9 185 2013/09/28 1.80 - xbee_end_device Router時の自動送信設定を可能に
tukudani 0:65f9be2858c9 186 - xbee_resultのGPINに電池電圧ADC[0]にmV値を応答
tukudani 0:65f9be2858c9 187 - xbee_atnjでjoin後のatnj=5設定をatnj=0に変更
tukudani 0:65f9be2858c9 188 - リモートAT応答の送信元が取得できないバグ修正
tukudani 0:65f9be2858c9 189 - XBee Wi-Fi xbee_gpoを送信時の応答バグ修正
tukudani 0:65f9be2858c9 190 2013/10/14 1.81 - Arduino版XBee Wi-Fi対応。XBee ZBは未テスト
tukudani 0:65f9be2858c9 191 - xbee_atd追加(End Device時に宛先アドレスを保存)
tukudani 0:65f9be2858c9 192 - xbee_ratd追加(親機アドレス以外も設定可能に)
tukudani 0:65f9be2858c9 193 - xbee_set_myaddress⇒xbee_ratd_myaddress名変更
tukudani 0:65f9be2858c9 194 - XBee Wi-Fi 3バイト以下UART受信不具合の修正
tukudani 0:65f9be2858c9 195 - XBee Wi-Fi ATDDで不適切な値を応答するバグ対策
tukudani 0:65f9be2858c9 196 - XBee Wi-Fiのフレーム受信でメモリーリーク対策
tukudani 0:65f9be2858c9 197 2014/01/14 1.82 - ATmega32U4が使われている時はLEONARDOを自動定義
tukudani 0:65f9be2858c9 198 - シリアルポート異常時のSegFault不具合の修正
tukudani 0:65f9be2858c9 199 - 日時付エラーログ出力(PC用XBEE_ERROR_TIME定義)
tukudani 0:65f9be2858c9 200 2014/02/17 1.83 - XBee Wi-Fi サンプルの各種不具合修正
tukudani 0:65f9be2858c9 201 - XBee Wi-Fi S2B対応(机上検討による実装)
tukudani 0:65f9be2858c9 202 - バイナリデータを子機UARTへ送信するxbee_bytes
tukudani 0:65f9be2858c9 203 - I2Cインタフェースを読み取るxbee_i2c_readの追加
tukudani 0:65f9be2858c9 204 - I2Cインタフェースで書き込むxbee_i2c_write追加
tukudani 0:65f9be2858c9 205 - 受信キャッシュをクリアするxbee_clear_cache追加
tukudani 0:65f9be2858c9 206 - 特定者向けリリース(I2C接続LCD,XBee Wi-Fi S2B)
tukudani 0:65f9be2858c9 207 2014/02/23 1.84 - 無駄使いメモリの修正、表示の調整、累積バグ修正
tukudani 0:65f9be2858c9 208 - ATコマンド解析ツールxbee_test.cのhelpコマンド
tukudani 0:65f9be2858c9 209 - README内のバージョン齟齬に関する修正
tukudani 0:65f9be2858c9 210 2014/03/01 1.85 - XBee WiFi S6B Ver.20xx対応(Ver.10xxと自動切替)
tukudani 0:65f9be2858c9 211  対応モジュール XBee WiFi S6 Ver.102D
tukudani 0:65f9be2858c9 212          XBee WiFi S6B Ver.2021
tukudani 0:65f9be2858c9 213 2014/03/17 1.86 - CQ出版様向けサンプルソフトウェアの正式リリース
tukudani 0:65f9be2858c9 214 2014/06/21 1.87 - ARM mbed対応(試作品)
tukudani 0:65f9be2858c9 215 2014/08/02 1.88 - シリアルCOMポート拡張(ポート番号1~64に対応)
tukudani 0:65f9be2858c9 216 - Arduino xbee_initでリトライ回数指定に。0で永久
tukudani 0:65f9be2858c9 217 - xbee_initに戻り値。異常時0。正常時リトライ数
tukudani 0:65f9be2858c9 218 - 受信しながら時間待ちするxbee_delay関数の追加
tukudani 0:65f9be2858c9 219 - 宛先ショートアドレス指定関数xbee_short_address
tukudani 0:65f9be2858c9 220 - ZigBee ZCL使用時(PC用)に関するバグ修正
tukudani 0:65f9be2858c9 221 2014/08/24 1.89 - 内部関数xbee_resetの公開(ヘッダファイルへ追加)
tukudani 0:65f9be2858c9 222 - xbee_init 失敗時の戻り値の不具合を修正
tukudani 0:65f9be2858c9 223 - PC版 GCC Ver 4.7対応,最適化オプション-O1の付与
tukudani 0:65f9be2858c9 224 - 戻り値が不定になる内部関数の不具合修正
tukudani 0:65f9be2858c9 225 2014/09/15 1.90 - 有志によるライブラリBeeBee Liteの一部機能採用
tukudani 0:65f9be2858c9 226  ・試験ツールxbee_agingを通過しない項目の調整
tukudani 0:65f9be2858c9 227  ・効果はArduino上でSD使用時のメモリ確保など
tukudani 0:65f9be2858c9 228 - xbee_bytesで規定サイズを超えた場合のリーク対策
tukudani 0:65f9be2858c9 229 - 下位マスク0x00のGPIN受信時にエラー出力バグ修正
tukudani 0:65f9be2858c9 230 2014/10/25 1.91 - CQ出版 ZigBee Arduinoプログラム全集 第2版用
tukudani 0:65f9be2858c9 231 - ARM mbedでのシリアル受信時のロックアップ対策
tukudani 0:65f9be2858c9 232 - ARM mbedを用いた時のシリアル送信タイミング調整
tukudani 0:65f9be2858c9 233 - xbee_end_deviceでRouterへ送信時にIR値を設定
tukudani 0:65f9be2858c9 234  (従来はエラー応答だったが、設定後に正常応答)
tukudani 0:65f9be2858c9 235 2014/10/31 1.92 - ARM mbed対応
tukudani 0:65f9be2858c9 236
tukudani 0:65f9be2858c9 237 *********************************************************************/
tukudani 0:65f9be2858c9 238 /*
tukudani 0:65f9be2858c9 239 本ライブラリのバージョン
tukudani 0:65f9be2858c9 240 */
tukudani 0:65f9be2858c9 241 #ifndef VERSION
tukudani 0:65f9be2858c9 242
tukudani 0:65f9be2858c9 243 #define VERSION "1.91" // 1.XX 4バイト形式 XXは半角文字
tukudani 0:65f9be2858c9 244
tukudani 0:65f9be2858c9 245 #endif
tukudani 0:65f9be2858c9 246 /*
tukudani 0:65f9be2858c9 247 参考文献
tukudani 0:65f9be2858c9 248
tukudani 0:65f9be2858c9 249 (1) Digi International Inc.
tukudani 0:65f9be2858c9 250 XBee ZB RF Modules 90000976_D 8/18/2009
tukudani 0:65f9be2858c9 251 XBee Wi-Fi RF Module 90002124_F 2011
tukudani 0:65f9be2858c9 252 (2) Massimo Banzi著 船田功訳
tukudani 0:65f9be2858c9 253 Arduinoをはじめよう (オライリージャパン)
tukudani 0:65f9be2858c9 254 (3) BestTechnology CO.,LTD.
tukudani 0:65f9be2858c9 255 H8/3664F I/O address definition Release 3.5 2005-11-08 (3694.h)
tukudani 0:65f9be2858c9 256 (4) 粕谷友章
tukudani 0:65f9be2858c9 257 PC-UNIX関連ドキュメント「シリアルポートプログラミング」
tukudani 0:65f9be2858c9 258 www006.upp.so-net.ne.jp/kasuya/linux/serial_io_programing.html
tukudani 0:65f9be2858c9 259 (5) JM Project Linux Programmer's Manual
tukudani 0:65f9be2858c9 260 Linux man-pages プロジェクト リリース 3.52 (GPL V2)
tukudani 0:65f9be2858c9 261 linuxjm.sourceforge.jp/html/LDP_man-pages/man2/select.2.html
tukudani 0:65f9be2858c9 262 (6) mbed Handbook
tukudani 0:65f9be2858c9 263 mbed.org/handbook/Homepage
tukudani 0:65f9be2858c9 264
tukudani 0:65f9be2858c9 265 ライセンスについて
tukudani 0:65f9be2858c9 266
tukudani 0:65f9be2858c9 267 本ソースコードはライセンスフリーですが、参考文献の情報については
tukudani 0:65f9be2858c9 268 それぞれの権利者にお問い合わせください。
tukudani 0:65f9be2858c9 269
tukudani 0:65f9be2858c9 270 注意点
tukudani 0:65f9be2858c9 271 本ソフトの利用による損害について当方は一切の補償をいたしません。
tukudani 0:65f9be2858c9 272 全て自己責任で利用ください。
tukudani 0:65f9be2858c9 273 */
tukudani 0:65f9be2858c9 274
tukudani 0:65f9be2858c9 275 /*********************************************************************
tukudani 0:65f9be2858c9 276 ライブラリ使用説明書
tukudani 0:65f9be2858c9 277
tukudani 0:65f9be2858c9 278 ハードウェア
tukudani 0:65f9be2858c9 279
tukudani 0:65f9be2858c9 280 PC用 以下のハードが必要です。
tukudani 0:65f9be2858c9 281 ・USBを搭載したIBM PC/AT互換機(通常のWindows PC)
tukudani 0:65f9be2858c9 282 ・XBee USBエクスプローラ、XBee-Fly USB または 純正XBIB-U-DEVなど
tukudani 0:65f9be2858c9 283  (http://www.geocities.jp/bokunimowakaru/diy/xbee/xbee-usb.html)
tukudani 0:65f9be2858c9 284 ・XBee Series 2 (ZB) モジュール
tukudani 0:65f9be2858c9 285
tukudani 0:65f9be2858c9 286 Arduino 以下のハードが必要です。
tukudani 0:65f9be2858c9 287 ・Arduinoマイコンボード Arduino UNO Arduino Leonardo等
tukudani 0:65f9be2858c9 288 ・XBee Shield (Arduino Wireless SD Shieldなど)
tukudani 0:65f9be2858c9 289 ・XBee Series 2 (ZB) モジュール
tukudani 0:65f9be2858c9 290 ・LCDキャラクタディスプレイモジュール[20×4行] SC2004CS-B
tukudani 0:65f9be2858c9 291
tukudani 0:65f9be2858c9 292  キャラクタ液晶は以下のように接続する
tukudani 0:65f9be2858c9 293
tukudani 0:65f9be2858c9 294   rs rw en d0 d1 d2 d3
tukudani 0:65f9be2858c9 295  // LiquidCrystal xbee_ardlcd( 8, 7, 6, 5, 4, 3, 2)
tukudani 0:65f9be2858c9 296  
tukudani 0:65f9be2858c9 297 ARM mbed用
tukudani 0:65f9be2858c9 298 ・STマイクロ製 Nucleo Board STM32F401で動作確認しています。
tukudani 0:65f9be2858c9 299 ・XBeeとの接続にはArduino用Wireless SD Shieldを使用します。
tukudani 0:65f9be2858c9 300  ※Nucleoボードの裏側のジャンパー抵抗の変更が必要です。
tukudani 0:65f9be2858c9 301 「SB14」をオープンにします(抵抗を取り外します。)
tukudani 0:65f9be2858c9 302 「SB62」をショートします。(半田で接続します。)
tukudani 0:65f9be2858c9 303 「SB63」をショートします。(半田で接続します。)
tukudani 0:65f9be2858c9 304 ・液晶を使用したサンプルはDF ROBOT製 LCD Keypad Shieldで
tukudani 0:65f9be2858c9 305  動作確認しています。
tukudani 0:65f9be2858c9 306
tukudani 0:65f9be2858c9 307 H3694用 秋月電子通商で販売されている以下のハードウェアが必要です。
tukudani 0:65f9be2858c9 308 ・AKI-H8/3694F(QFP) タイニーマイコンキット
tukudani 0:65f9be2858c9 309 ・H8タイニーI/O(アイ・オー)ボード
tukudani 0:65f9be2858c9 310 ・小型スライドスイッチ 1回路2接点 SS12D01G4
tukudani 0:65f9be2858c9 311 ・LCDキャラクタディスプレイモジュール[20×4行] SC2004CS-B
tukudani 0:65f9be2858c9 312 ・XBeeモジュールをRS-232Cに接続する基板(純正XBIB-R-DEVなど)
tukudani 0:65f9be2858c9 313  (http://www.geocities.jp/bokunimowakaru/pict/xbee-cord_sch.gif)
tukudani 0:65f9be2858c9 314 ・RS-232Cケーブル
tukudani 0:65f9be2858c9 315 ・XBee Series 2 (ZB) モジュール
tukudani 0:65f9be2858c9 316
tukudani 0:65f9be2858c9 317  キャラクタ液晶はH8/3694のCN1の14~19ピンの各出力と電源、
tukudani 0:65f9be2858c9 318  GNDを液晶モジュールのDB4~7およびE、RSの各入力、および
tukudani 0:65f9be2858c9 319  電源に接続して製作します(下図参照)。
tukudani 0:65f9be2858c9 320
tukudani 0:65f9be2858c9 321 AKI-H8      液晶モジュール
tukudani 0:65f9be2858c9 322 (CN1)       (HD44780搭載)
tukudani 0:65f9be2858c9 323 ━━┓      ┏━━━━━┓    ┯ 5V
tukudani 0:65f9be2858c9 324   ┃P50     ┃     ┃    |
tukudani 0:65f9be2858c9 325  14┠──────┨DB4  Vdd┠───●┘
tukudani 0:65f9be2858c9 326   ┃P51     ┃     ┃   |
tukudani 0:65f9be2858c9 327  15┠──────┨DB5    ┃   <
tukudani 0:65f9be2858c9 328   ┃P52     ┃     ┃ ┌→>
tukudani 0:65f9be2858c9 329  16┠──────┨DB6  VLC┠─┘ < 10k
tukudani 0:65f9be2858c9 330   ┃P53     ┃     ┃   |
tukudani 0:65f9be2858c9 331  17┠──────┨DB7 DB0-3┠─┐ |
tukudani 0:65f9be2858c9 332   ┃P54     ┃     ┃ | |
tukudani 0:65f9be2858c9 333  18┠──────┨E   R/W┠─● |
tukudani 0:65f9be2858c9 334   ┃P55     ┃     ┃ | |
tukudani 0:65f9be2858c9 335  19┠──────┨RS   Vss┠─●─●┐
tukudani 0:65f9be2858c9 336   ┃      ┗━━━━━┛   ┯┿┯
tukudani 0:65f9be2858c9 337   
tukudani 0:65f9be2858c9 338
tukudani 0:65f9be2858c9 339 port: port指定 IO名 ピン番号 共用 主な用途
tukudani 0:65f9be2858c9 340 port= 0 DIO0 XBee_pin 20 (Cms) Commision コミッションボタン
tukudani 0:65f9be2858c9 341 port= 1 DIO1 XBee_pin 19 (AD1) 汎用入力用(DIN or AIN)
tukudani 0:65f9be2858c9 342 port= 2 DIO2 XBee_pin 18 (AD2) 汎用入力用(DIN or AIN)
tukudani 0:65f9be2858c9 343 port= 3 DIO3 XBee_pin 17 (AD3) 汎用入力用(DIN or AIN)
tukudani 0:65f9be2858c9 344 port= 4 DIO4 XBee_pin 11 汎用出力用
tukudani 0:65f9be2858c9 345 port= 5 DIO5 XBee_pin 15 (Ass) ASSOSIATE indication
tukudani 0:65f9be2858c9 346 port= 6 DIO6 XBee_pin 16 (RTS) 汎用出力用
tukudani 0:65f9be2858c9 347 port=10 DIO10 XBee_pin 6 (RSSI) RSSI indication (PWM)
tukudani 0:65f9be2858c9 348 port=11 DIO11 XBee_pin 7 汎用出力用
tukudani 0:65f9be2858c9 349 port=12 DIO12 XBee_pin 4 汎用出力用
tukudani 0:65f9be2858c9 350
tukudani 0:65f9be2858c9 351 ソフト開発環境
tukudani 0:65f9be2858c9 352
tukudani 0:65f9be2858c9 353 共通
tukudani 0:65f9be2858c9 354 ・X-CTU (Digi社)
tukudani 0:65f9be2858c9 355  PCもしくはH3694、Arduinoに接続する親機側のXBeeモジュールへ
tukudani 0:65f9be2858c9 356  Coordinatorのファームウェアを書き込むのに必要です。
tukudani 0:65f9be2858c9 357
tukudani 0:65f9be2858c9 358 Device Type XBee Firmware Ver. ハードウェア
tukudani 0:65f9be2858c9 359 -----------------------------------------------------
tukudani 0:65f9be2858c9 360 Coordinator COORDINATOR API 2141 パソコンのシリアル端子
tukudani 0:65f9be2858c9 361 または秋月H8Tiny IO BOARD
tukudani 0:65f9be2858c9 362 またはArduino側に接続
tukudani 0:65f9be2858c9 363 End Device END DEVICE AT 2841 単体で駆動
tukudani 0:65f9be2858c9 364
tukudani 0:65f9be2858c9 365 Arduino用
tukudani 0:65f9be2858c9 366 ・Arduino IDE
tukudani 0:65f9be2858c9 367
tukudani 0:65f9be2858c9 368 ARM mbed用
tukudani 0:65f9be2858c9 369 ・https://mbed.org/compiler/
tukudani 0:65f9be2858c9 370
tukudani 0:65f9be2858c9 371 PC用
tukudani 0:65f9be2858c9 372 ・cygwin (http://www.cygwin.com/)
tukudani 0:65f9be2858c9 373 ・インストール時にdevl内のgcc coreとmakeを選択すること
tukudani 0:65f9be2858c9 374
tukudani 0:65f9be2858c9 375 H3694用
tukudani 0:65f9be2858c9 376 ・GCC Developer Lite(株式会社ベストテクノロジー)
tukudani 0:65f9be2858c9 377  http://www.besttechnology.co.jp/modules/knowledge/
tukudani 0:65f9be2858c9 378
tukudani 0:65f9be2858c9 379 上記ホームページの「ナレッジベース」より、技術情報/ソフト
tukudani 0:65f9be2858c9 380 ウェア/GCC Developer Liteからダウンロードできます。
tukudani 0:65f9be2858c9 381 この開発環境に含まれるH3694用のターゲットファイルを使用し
tukudani 0:65f9be2858c9 382 ていますので、他の環境で開発する場合は注意が必要です。
tukudani 0:65f9be2858c9 383 */
tukudani 0:65f9be2858c9 384
tukudani 0:65f9be2858c9 385 /*********************************************************************
tukudani 0:65f9be2858c9 386 インポート処理
tukudani 0:65f9be2858c9 387 *********************************************************************/
tukudani 0:65f9be2858c9 388
tukudani 0:65f9be2858c9 389 #ifndef LCD_TYPE_H
tukudani 0:65f9be2858c9 390 #define LCD_TYPE_H
tukudani 0:65f9be2858c9 391 #ifdef ARDUINO
tukudani 0:65f9be2858c9 392 #if defined(__AVR_ATmega32U4__)
tukudani 0:65f9be2858c9 393 #define LEONARDO // Arduino LEONARDを使用する場合に自動的に定義されます。
tukudani 0:65f9be2858c9 394 #endif
tukudani 0:65f9be2858c9 395 // #define LCD_H // 本ライブラリ動作を液晶表示する場合に定義する
tukudani 0:65f9be2858c9 396 // #define ADAFRUIT // 使用する液晶が Adafruit LCD Sheild の時に定義する
tukudani 0:65f9be2858c9 397 #else // H8 or ARM or PC
tukudani 0:65f9be2858c9 398 #ifndef ARM_MBED // H8 or PC
tukudani 0:65f9be2858c9 399 #define LCD_H
tukudani 0:65f9be2858c9 400 #endif
tukudani 0:65f9be2858c9 401 #endif
tukudani 0:65f9be2858c9 402 // #define XBEE_WIFI // XBee Wi-Fiを使用するときに定義する
tukudani 0:65f9be2858c9 403 // #define XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 404 // #define LITE // ライトモード(メモリ節約・機能制限)の時に定義する
tukudani 0:65f9be2858c9 405 #define EASY_GPI // xbee_gpi関数(リモートGPIO)を使用するときに定義する
tukudani 0:65f9be2858c9 406 #define EASY_ADC // xbee_adc関数(リモートADC)を使用するときに定義する
tukudani 0:65f9be2858c9 407 // ##define EASY_SENSOR
tukudani 0:65f9be2858c9 408 #endif
tukudani 0:65f9be2858c9 409
tukudani 0:65f9be2858c9 410 #ifndef XB_IMPORT_H
tukudani 0:65f9be2858c9 411 #ifdef H3694
tukudani 0:65f9be2858c9 412 #ifndef __3694_H__
tukudani 0:65f9be2858c9 413 #include <3694.h>
tukudani 0:65f9be2858c9 414 #endif
tukudani 0:65f9be2858c9 415 #ifdef LCD_H
tukudani 0:65f9be2858c9 416 #ifndef LCD_ROW_1
tukudani 0:65f9be2858c9 417 #include "lcd_h8.c"
tukudani 0:65f9be2858c9 418 #endif
tukudani 0:65f9be2858c9 419 #endif
tukudani 0:65f9be2858c9 420 /* // lcd_h8ライブラリの関数
tukudani 0:65f9be2858c9 421 void lcd_delay(unsigned int data);
tukudani 0:65f9be2858c9 422 void lcd_toggle_E(void);
tukudani 0:65f9be2858c9 423 void lcd_cls(void);
tukudani 0:65f9be2858c9 424 void lcd_home(void);
tukudani 0:65f9be2858c9 425 void lcd_control(unsigned char disonoff, unsigned char curonoff, unsigned char curblink);
tukudani 0:65f9be2858c9 426 void lcd_goto(unsigned char mesto);
tukudani 0:65f9be2858c9 427 void lcd_shift(unsigned char data);
tukudani 0:65f9be2858c9 428 void lcd_putch(char data);
tukudani 0:65f9be2858c9 429 void lcd_putstr(const char *data);
tukudani 0:65f9be2858c9 430 void lcd_disp_bin(unsigned char x);
tukudani 0:65f9be2858c9 431 void lcd_disp_hex(unsigned char i);
tukudani 0:65f9be2858c9 432 void lcd_disp_1(unsigned int x);
tukudani 0:65f9be2858c9 433 void lcd_disp_2(unsigned int x);
tukudani 0:65f9be2858c9 434 void lcd_disp_3(unsigned int x);
tukudani 0:65f9be2858c9 435 void lcd_disp_5(unsigned int x);
tukudani 0:65f9be2858c9 436 void lcd_init(void);
tukudani 0:65f9be2858c9 437 */
tukudani 0:65f9be2858c9 438 #elif ARDUINO
tukudani 0:65f9be2858c9 439 // #include <inttypes.h>
tukudani 0:65f9be2858c9 440 #include "Arduino.h"
tukudani 0:65f9be2858c9 441 // #include "WProgram.h" // #if ARDUINO < 100
tukudani 0:65f9be2858c9 442 #ifdef LCD_H
tukudani 0:65f9be2858c9 443 #ifndef ADAFRUIT
tukudani 0:65f9be2858c9 444 // 通常のキャラクタLCDの場合
tukudani 0:65f9be2858c9 445 #include <LiquidCrystal.h>
tukudani 0:65f9be2858c9 446 #else
tukudani 0:65f9be2858c9 447 // Adafruit I2C接続LCDの場合(Adafruit_RGBLCDShieldライブラリが必要)
tukudani 0:65f9be2858c9 448 #include <Wire.h>
tukudani 0:65f9be2858c9 449 #include <Adafruit_MCP23017.h>
tukudani 0:65f9be2858c9 450 #include <Adafruit_RGBLCDShield.h>
tukudani 0:65f9be2858c9 451 #endif
tukudani 0:65f9be2858c9 452 #endif
tukudani 0:65f9be2858c9 453 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 454 #include <SPI.h>
tukudani 0:65f9be2858c9 455 #include <Ethernet.h>
tukudani 0:65f9be2858c9 456 #include <EthernetUdp.h>
tukudani 0:65f9be2858c9 457 #endif
tukudani 0:65f9be2858c9 458 #ifndef LCD_ROW_1
tukudani 0:65f9be2858c9 459 #define LCD_ROW_1 0x00 //1行目先頭アドレス
tukudani 0:65f9be2858c9 460 #define LCD_ROW_2 0x40 //2行目先頭アドレス
tukudani 0:65f9be2858c9 461 #define LCD_ROW_3 0x14 //3行目先頭アドレス
tukudani 0:65f9be2858c9 462 #define LCD_ROW_4 0x54 //4行目先頭アドレス
tukudani 0:65f9be2858c9 463 #endif
tukudani 0:65f9be2858c9 464 #else
tukudani 0:65f9be2858c9 465 #ifdef ARM_MBED // ARM
tukudani 0:65f9be2858c9 466 // #include "mbed.h"
tukudani 0:65f9be2858c9 467 #ifdef DEBUG
tukudani 0:65f9be2858c9 468 #define LCD_H
tukudani 0:65f9be2858c9 469 #define ERRLOG
tukudani 0:65f9be2858c9 470 #define LCD_ROW_1 0x00 //1行目先頭アドレス
tukudani 0:65f9be2858c9 471 #define LCD_ROW_2 0x40 //2行目先頭アドレス
tukudani 0:65f9be2858c9 472 #define LCD_ROW_3 0x14 //3行目先頭アドレス
tukudani 0:65f9be2858c9 473 #define LCD_ROW_4 0x54 //4行目先頭アドレス
tukudani 0:65f9be2858c9 474 #endif
tukudani 0:65f9be2858c9 475 #else // PC
tukudani 0:65f9be2858c9 476 #include <stdio.h>
tukudani 0:65f9be2858c9 477 #include <stdlib.h>
tukudani 0:65f9be2858c9 478 #include <string.h>
tukudani 0:65f9be2858c9 479 #include <termios.h>
tukudani 0:65f9be2858c9 480 #include <sys/signal.h>
tukudani 0:65f9be2858c9 481 #include <sys/time.h>
tukudani 0:65f9be2858c9 482 #include <fcntl.h>
tukudani 0:65f9be2858c9 483 #include <unistd.h>
tukudani 0:65f9be2858c9 484 #include <arpa/inet.h>
tukudani 0:65f9be2858c9 485 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 486 #include <sys/types.h>
tukudani 0:65f9be2858c9 487 #include <sys/socket.h>
tukudani 0:65f9be2858c9 488 #include <netinet/in.h>
tukudani 0:65f9be2858c9 489 #include <ctype.h>
tukudani 0:65f9be2858c9 490 #include <sys/ioctl.h>
tukudani 0:65f9be2858c9 491 #include <string.h>
tukudani 0:65f9be2858c9 492 #endif
tukudani 0:65f9be2858c9 493 #define BAUDRATE B9600
tukudani 0:65f9be2858c9 494 #ifndef LCD_ROW_1
tukudani 0:65f9be2858c9 495 #include "lcd_pc.c"
tukudani 0:65f9be2858c9 496 #endif
tukudani 0:65f9be2858c9 497 #include <time.h> // クロックタイマー用
tukudani 0:65f9be2858c9 498 #endif
tukudani 0:65f9be2858c9 499 #endif
tukudani 0:65f9be2858c9 500 #define xbee_set_myaddress(adr) xbee_ratd_myaddress(adr)
tukudani 0:65f9be2858c9 501 #endif
tukudani 0:65f9be2858c9 502
tukudani 0:65f9be2858c9 503 /*********************************************************************
tukudani 0:65f9be2858c9 504 定数の設定
tukudani 0:65f9be2858c9 505 *********************************************************************/
tukudani 0:65f9be2858c9 506 #ifndef XB_DEFINE_H
tukudani 0:65f9be2858c9 507 #define XB_DEFINE_H
tukudani 0:65f9be2858c9 508 #ifndef NAME
tukudani 0:65f9be2858c9 509 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 510 #define NAME "BeeBee Lite"
tukudani 0:65f9be2858c9 511 #define COPYRIGHT "by Wataru & Ran"
tukudani 0:65f9be2858c9 512 #else
tukudani 0:65f9be2858c9 513 #define NAME "ZB Coord"
tukudani 0:65f9be2858c9 514 #define COPYRIGHT "by Wataru KUNINO"
tukudani 0:65f9be2858c9 515 #endif
tukudani 0:65f9be2858c9 516 #endif
tukudani 0:65f9be2858c9 517
tukudani 0:65f9be2858c9 518 #ifdef H3694
tukudani 0:65f9be2858c9 519 #define ERRLOG
tukudani 0:65f9be2858c9 520 #define LED1_OUT IO.PDR8.BIT.B0 // LED赤の接続ポート(エラー用)
tukudani 0:65f9be2858c9 521 #define LED2_OUT IO.PDR8.BIT.B1 // LED緑の接続ポート(動作確認用)
tukudani 0:65f9be2858c9 522 #define LCD_EN IO.PDR8.BIT.B6 // 液晶用電源
tukudani 0:65f9be2858c9 523 #define BUTN IO.PDR8.BIT.B2 // ボタンの接続ポート
tukudani 0:65f9be2858c9 524 #define SCI_SIZE 256 // シリアルデータ長(大きすぎるとRAMが不足する)
tukudani 0:65f9be2858c9 525 #define API_SIZE 128 // 受信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 526 #define API_TXSIZE 64 // 送信用APIデータ長(32~255) シリアル送信最大長=API_TXSIZE-18バイト
tukudani 0:65f9be2858c9 527 #define CALL_SIZE 32 // xbee_rx_call用戻りデータ(10~256)
tukudani 0:65f9be2858c9 528 #define XB_AT_SIZE 32 // ATコマンドの最大長
tukudani 0:65f9be2858c9 529 #elif ARDUINO
tukudani 0:65f9be2858c9 530 // #define LED1_OUT 15 // 赤色LED(エラー用)用デジタルポート(15=analog 1)
tukudani 0:65f9be2858c9 531 // #define LED2_OUT 16 // 緑色LED(動作確認用)用デジタルポート(16=analog 2)
tukudani 0:65f9be2858c9 532 // #define LCD_EN 6 // 液晶用電源デジタルポート
tukudani 0:65f9be2858c9 533 // #define BUTN 14 // ボタンの接続ポート(14 = analog 0)
tukudani 0:65f9be2858c9 534 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 535 #define API_SIZE 48 // 受信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 536 #define API_TXSIZE 34 // 送信用APIデータ長(32~255) シリアル送信最大長=API_TXSIZE-18バイト
tukudani 0:65f9be2858c9 537 #define CALL_SIZE 16 // xbee_rx_call用戻りデータ(10~256)
tukudani 0:65f9be2858c9 538 #define XB_AT_SIZE 16 // ATコマンドの最大長
tukudani 0:65f9be2858c9 539 #else
tukudani 0:65f9be2858c9 540 #define CACHE_RES 2 // 応答時のキャッシュ数(無効にするには定義を消す)
tukudani 0:65f9be2858c9 541 #define API_SIZE 64 // 受信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 542 #define API_TXSIZE 64 // 送信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 543 #define CALL_SIZE 32 // xbee_rx_call用戻りデータ(10~256)
tukudani 0:65f9be2858c9 544 #define XB_AT_SIZE 32 // ATコマンドの最大長
tukudani 0:65f9be2858c9 545 #endif
tukudani 0:65f9be2858c9 546 #define LIGHT 1
tukudani 0:65f9be2858c9 547 #define TEMP 2
tukudani 0:65f9be2858c9 548 #define HUMIDITY 3
tukudani 0:65f9be2858c9 549 #define WATT 4
tukudani 0:65f9be2858c9 550 #define BATT 5
tukudani 0:65f9be2858c9 551 #define DISABLE 0
tukudani 0:65f9be2858c9 552 #define VENDER 1
tukudani 0:65f9be2858c9 553 #define AIN 2
tukudani 0:65f9be2858c9 554 #define DIN 3
tukudani 0:65f9be2858c9 555 #define DOUT_L 4
tukudani 0:65f9be2858c9 556 #define DOUT_H 5
tukudani 0:65f9be2858c9 557 #else
tukudani 0:65f9be2858c9 558 #ifdef ARM_MBED // ARM <条件は調整していない。ほぼArduinoのまま>
tukudani 0:65f9be2858c9 559 #define CACHE_RES 3 // 応答時のキャッシュ数(無効にするには定義を消す)
tukudani 0:65f9be2858c9 560 #define API_SIZE 255 // 受信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 561 #define API_TXSIZE 255 // 送信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 562 #define CALL_SIZE 32 // xbee_rx_call用戻りデータ(10~256)
tukudani 0:65f9be2858c9 563 #define XB_AT_SIZE 32 // ATコマンドの最大長
tukudani 0:65f9be2858c9 564 #define delay(ms) wait_millisec(ms) // 関数名の複製
tukudani 0:65f9be2858c9 565 #define LIGHT 1
tukudani 0:65f9be2858c9 566 #define TEMP 2
tukudani 0:65f9be2858c9 567 #define HUMIDITY 3
tukudani 0:65f9be2858c9 568 #define WATT 4
tukudani 0:65f9be2858c9 569 #define BATT 5
tukudani 0:65f9be2858c9 570 #define DISABLE 0
tukudani 0:65f9be2858c9 571 #define VENDER 1
tukudani 0:65f9be2858c9 572 #define AIN 2
tukudani 0:65f9be2858c9 573 #define DIN 3
tukudani 0:65f9be2858c9 574 #define DOUT_L 4
tukudani 0:65f9be2858c9 575 #define DOUT_H 5
tukudani 0:65f9be2858c9 576 #else // PC
tukudani 0:65f9be2858c9 577 // #define DEBUG // デバッグモード
tukudani 0:65f9be2858c9 578 // #define DEBUG_TX // 送信パケットの表示
tukudani 0:65f9be2858c9 579 // #define DEBUG_RX // 受信パケットの表示
tukudani 0:65f9be2858c9 580 #define ERRLOG // エラー時にログを出力
tukudani 0:65f9be2858c9 581 // #define XBEE_ERROR_TIME // エラー時のログに日時を付与
tukudani 0:65f9be2858c9 582 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 583 #define API_SIZE 48 // 受信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 584 #define API_TXSIZE 34 // 送信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 585 #define CALL_SIZE 16 // xbee_rx_call用戻りデータ(10~256)
tukudani 0:65f9be2858c9 586 #define XB_AT_SIZE 16 // ATコマンドの最大長
tukudani 0:65f9be2858c9 587 #else
tukudani 0:65f9be2858c9 588 #define CACHE_RES 5 // 応答時のキャッシュ数(無効にするには定義を消す)
tukudani 0:65f9be2858c9 589 #define API_SIZE 128 // 受信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 590 #define API_TXSIZE 64 // 送信用APIデータ長(32~255)
tukudani 0:65f9be2858c9 591 #define CALL_SIZE 64 // xbee_rx_call用戻りデータ(10~256)
tukudani 0:65f9be2858c9 592 #define XB_AT_SIZE 32 // ATコマンドの最大長
tukudani 0:65f9be2858c9 593 #define delay(ms) wait_millisec(ms) // 関数名の複製
tukudani 0:65f9be2858c9 594 #endif
tukudani 0:65f9be2858c9 595 #endif
tukudani 0:65f9be2858c9 596 #endif
tukudani 0:65f9be2858c9 597 #define TIME_DEL 3 // デリミタ検出のタイムアウト時間(秒)
tukudani 0:65f9be2858c9 598 #define MODE_AUTO 0x00 // 自動受信モード
tukudani 0:65f9be2858c9 599 #define MODE_GPIN 0x92 // GPI data を受信するモード
tukudani 0:65f9be2858c9 600 #define MODE_UART 0x90 // UART data を受信するモード
tukudani 0:65f9be2858c9 601 #define MODE_UAR2 0x91 // UART data を受信するモード2(AO=1)
tukudani 0:65f9be2858c9 602 #define MODE_SENS 0x94 // XB Sensorを受信するモード(1wire専用→通常のSensorはATISを使用)
tukudani 0:65f9be2858c9 603 #define MODE_IDNT 0x95 // Node Identifyを受信するモード
tukudani 0:65f9be2858c9 604 #define MODE_RES 0x88 // ローカルATコマンドの結果を受信
tukudani 0:65f9be2858c9 605 #define MODE_RESP 0x97 // リモートATコマンドの結果を受信(仕様書はATNDで説明ATISもこれ)
tukudani 0:65f9be2858c9 606 #define MODE_MODM 0x8A // Modem Statusを受信
tukudani 0:65f9be2858c9 607 #define MODE_TXST 0x8B // UART Transmit Status を受信
tukudani 0:65f9be2858c9 608 #define MODE_BATT 0xE1 // (独自定義)バッテリステータス RAT%Vの応答時
tukudani 0:65f9be2858c9 609 // XBeeのFrame Typeに準拠する。
tukudani 0:65f9be2858c9 610 // 注意:モードを増やしたときはxbee_from_acumとxbee_rx_call内の対応を追加すること
tukudani 0:65f9be2858c9 611 // 独自定義の時はxbee_rx_call内のみ。
tukudani 0:65f9be2858c9 612 // MODE値はenum定義にしない。
tukudani 0:65f9be2858c9 613
tukudani 0:65f9be2858c9 614 #define STATUS_OK 0x00 // ATコマンドの結果がOK
tukudani 0:65f9be2858c9 615 #define STATUS_ERR 0x01 // ATコマンドの結果がERROR
tukudani 0:65f9be2858c9 616 #define STATUS_ERR_AT 0x02 // 指定されたATコマンドに誤りがある
tukudani 0:65f9be2858c9 617 #define STATUS_ERR_PARM 0x03 // 指定されたパラメータに誤りがある
tukudani 0:65f9be2858c9 618 #define STATUS_ERR_AIR 0x04 // リモートATコマンドの送信の失敗(相手が応答しない)
tukudani 0:65f9be2858c9 619
tukudani 0:65f9be2858c9 620 #define MODM_RESET 0x01 // ローカルのXBeeがリセットした
tukudani 0:65f9be2858c9 621 #define MODM_WATCHDOG 0x02 // ローカルのXBeeがWatch dogタイマーによってリセットした
tukudani 0:65f9be2858c9 622 #define MODM_JOINED 0x03 // (RouterまたはEnd Deviceで使用しているときに)ネットワークJoinした
tukudani 0:65f9be2858c9 623 #define MODM_LEFT 0x04 // ネットワークからdis_assosiateした
tukudani 0:65f9be2858c9 624 #define MODM_STARTED 0x06 // (coordinatorで使用しているときに)Coordinatorを開始した
tukudani 0:65f9be2858c9 625
tukudani 0:65f9be2858c9 626 #define DEV_TYPE_XBEE 0x00 // XBeeモジュール
tukudani 0:65f9be2858c9 627 #define DEV_TYPE_RS232 0x05 // RS-232Cアダプタ
tukudani 0:65f9be2858c9 628 #define DEV_TYPE_SENS 0x07 // Sensor (1wire専用)
tukudani 0:65f9be2858c9 629 #define DEV_TYPE_WALL 0x08 // Wall Router
tukudani 0:65f9be2858c9 630 #define DEV_TYPE_SEN_LT 0x0E // Sensor (照度・温度)
tukudani 0:65f9be2858c9 631 #define DEV_TYPE_SEN_LTH 0x0D // Sensor (照度・温度・湿度)
tukudani 0:65f9be2858c9 632 #define DEV_TYPE_PLUG 0x0F // Smart Plug
tukudani 0:65f9be2858c9 633
tukudani 0:65f9be2858c9 634 #define ZB_TYPE_COORD 0x21 // ZigBee Coordinator
tukudani 0:65f9be2858c9 635 #define ZB_TYPE_ROUTER 0x23 // ZigBee Router
tukudani 0:65f9be2858c9 636 #define ZB_TYPE_ENDDEV 0x29 // ZigBee End Device
tukudani 0:65f9be2858c9 637 #define XB_TYPE_NULL 0x00 // XBee Wi-Fi バージョン未取得
tukudani 0:65f9be2858c9 638 #define XB_TYPE_WIFI10 0x10 // XBee Wi-Fi Ver. 10xx
tukudani 0:65f9be2858c9 639 #define XB_TYPE_WIFI20 0x20 // XBee Wi-Fi Ver. 20xx
tukudani 0:65f9be2858c9 640
tukudani 0:65f9be2858c9 641 #define NET_ADR FFFE // ネットワークアドレス
tukudani 0:65f9be2858c9 642 #endif
tukudani 0:65f9be2858c9 643
tukudani 0:65f9be2858c9 644 /*********************************************************************
tukudani 0:65f9be2858c9 645 型の定義とグローバル変数の宣言
tukudani 0:65f9be2858c9 646 *********************************************************************/
tukudani 0:65f9be2858c9 647 #ifndef XB_GLOBAL_H
tukudani 0:65f9be2858c9 648 #define XB_GLOBAL_H
tukudani 0:65f9be2858c9 649 typedef unsigned char byte; // Arduinoでも必要
tukudani 0:65f9be2858c9 650 #ifdef H3694
tukudani 0:65f9be2858c9 651 byte TIMER_SEC = 0 ; //RTCカウント用1秒単位
tukudani 0:65f9be2858c9 652 char sci_tx[SCI_SIZE]; // シリアル用
tukudani 0:65f9be2858c9 653 char sci_rx[SCI_SIZE];
tukudani 0:65f9be2858c9 654 #elif ARDUINO
tukudani 0:65f9be2858c9 655 #define TIMER_SEC time1s256()
tukudani 0:65f9be2858c9 656 #else
tukudani 0:65f9be2858c9 657 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 658 #define TIMER_SEC time1s256()
tukudani 0:65f9be2858c9 659 #else // PC
tukudani 0:65f9be2858c9 660 #define TIMER_SEC time1s256() // TIMER_SECのカウントアップの代わり
tukudani 0:65f9be2858c9 661 volatile byte LED1_OUT;
tukudani 0:65f9be2858c9 662 volatile byte LED2_OUT;
tukudani 0:65f9be2858c9 663 volatile byte LCD_EN;
tukudani 0:65f9be2858c9 664 #endif
tukudani 0:65f9be2858c9 665 #endif
tukudani 0:65f9be2858c9 666
tukudani 0:65f9be2858c9 667 #ifdef ARDUINO
tukudani 0:65f9be2858c9 668 #ifdef LCD_H
tukudani 0:65f9be2858c9 669 #ifndef ADAFRUIT
tukudani 0:65f9be2858c9 670 // 通常のキャラクタLCDの場合
tukudani 0:65f9be2858c9 671 LiquidCrystal xbee_ardlcd(8, 9, 4, 5, 6, 7);
tukudani 0:65f9be2858c9 672 //LCD設定 rs en d0 d1 d2 d3
tukudani 0:65f9be2858c9 673 #else
tukudani 0:65f9be2858c9 674 // Adafruit I2C接続LCDの場合(Adafruit_RGBLCDShieldライブラリが必要)
tukudani 0:65f9be2858c9 675 Adafruit_RGBLCDShield xbee_ardlcd = Adafruit_RGBLCDShield();
tukudani 0:65f9be2858c9 676 #endif
tukudani 0:65f9be2858c9 677 #endif
tukudani 0:65f9be2858c9 678
tukudani 0:65f9be2858c9 679 // 構造体の宣言
tukudani 0:65f9be2858c9 680 typedef struct{
tukudani 0:65f9be2858c9 681 byte MODE; // 受信モード(Frame Type)
tukudani 0:65f9be2858c9 682 byte FROM[8]; // 送信元IEEEアドレス
tukudani 0:65f9be2858c9 683 byte AT[2]; // ATコマンド
tukudani 0:65f9be2858c9 684 byte ID; // 応答パケットID(Frame ID)
tukudani 0:65f9be2858c9 685 byte STATUS; // 応答結果(0:OK 1:ERROR)/AT結果/UART状態
tukudani 0:65f9be2858c9 686 union { // GPIOデータ
tukudani 0:65f9be2858c9 687 byte BYTE[2];
tukudani 0:65f9be2858c9 688 struct { // バイト毎に下位ビットから代入(リトルエンディアン)
tukudani 0:65f9be2858c9 689 byte D0 :1; byte D1 :1; byte D2 :1; byte D3 :1; // BYTE[1]
tukudani 0:65f9be2858c9 690 byte D4 :1; byte D5 :1; byte D6 :1; byte D7 :1;
tukudani 0:65f9be2858c9 691 byte :1; byte :1; byte D10:1; byte D11:1; // BYTE[0]
tukudani 0:65f9be2858c9 692 byte D12:1; byte :1; byte :1; byte :1;
tukudani 0:65f9be2858c9 693 } PORT;
tukudani 0:65f9be2858c9 694 } GPI;
tukudani 0:65f9be2858c9 695 // byte GPI[2]; // GPIOデータ
tukudani 0:65f9be2858c9 696 unsigned int ADCIN[4]; // ADCデータ
tukudani 0:65f9be2858c9 697 byte DATA[CALL_SIZE]; // 受信データ
tukudani 0:65f9be2858c9 698 } XBEE_RESULT; // 構造体の型名
tukudani 0:65f9be2858c9 699 #else
tukudani 0:65f9be2858c9 700 #ifdef ARM_MBED // ARM
tukudani 0:65f9be2858c9 701 // 構造体の宣言
tukudani 0:65f9be2858c9 702 typedef struct{
tukudani 0:65f9be2858c9 703 byte MODE; // 受信モード(Frame Type)
tukudani 0:65f9be2858c9 704 byte FROM[8]; // 送信元IEEEアドレス
tukudani 0:65f9be2858c9 705 byte AT[2]; // ATコマンド
tukudani 0:65f9be2858c9 706 byte ID; // 応答パケットID(Frame ID)
tukudani 0:65f9be2858c9 707 byte STATUS; // 応答結果(0:OK 1:ERROR)/AT結果/UART状態
tukudani 0:65f9be2858c9 708 union { // GPIOデータ
tukudani 0:65f9be2858c9 709 byte BYTE[2];
tukudani 0:65f9be2858c9 710 struct { // バイト毎に下位ビットから代入(リトルエンディアン)
tukudani 0:65f9be2858c9 711 byte D0 :1; byte D1 :1; byte D2 :1; byte D3 :1; // BYTE[1]
tukudani 0:65f9be2858c9 712 byte D4 :1; byte D5 :1; byte D6 :1; byte D7 :1;
tukudani 0:65f9be2858c9 713 byte :1; byte :1; byte D10:1; byte D11:1; // BYTE[0]
tukudani 0:65f9be2858c9 714 byte D12:1; byte :1; byte :1; byte :1;
tukudani 0:65f9be2858c9 715 } PORT;
tukudani 0:65f9be2858c9 716 } GPI;
tukudani 0:65f9be2858c9 717 // byte GPI[2]; // GPIOデータ
tukudani 0:65f9be2858c9 718 unsigned int ADCIN[4]; // ADCデータ
tukudani 0:65f9be2858c9 719 byte DATA[CALL_SIZE]; // 受信データ
tukudani 0:65f9be2858c9 720 } XBEE_RESULT; // 構造体の型名
tukudani 0:65f9be2858c9 721 #else // PC
tukudani 0:65f9be2858c9 722 enum xbee_sensor_type{ LIGHT,TEMP,HUMIDITY,WATT,BATT,PRESS,VALUE,TIMES,NA }; // センサタイプの型
tukudani 0:65f9be2858c9 723 enum xbee_port_type{ DISABLE=0, VENDER=1, AIN=2, DIN=3, DOUT_L=4, DOUT_H=5 };
tukudani 0:65f9be2858c9 724 // GPIOの設定の型
tukudani 0:65f9be2858c9 725 typedef struct{
tukudani 0:65f9be2858c9 726 byte MODE; // 受信モード(Frame Type)
tukudani 0:65f9be2858c9 727 byte FROM[8]; // 送信元IEEEアドレス
tukudani 0:65f9be2858c9 728 byte SHORT[2]; // 送信元ショートアドレス
tukudani 0:65f9be2858c9 729 byte AT[2]; // ATコマンド
tukudani 0:65f9be2858c9 730 byte ID; // 応答パケットID(Frame ID)
tukudani 0:65f9be2858c9 731 byte STATUS; // 応答結果(0:OK 1:ERROR)/AT結果/UART状態
tukudani 0:65f9be2858c9 732 union { // GPIOデータ
tukudani 0:65f9be2858c9 733 byte BYTE[2];
tukudani 0:65f9be2858c9 734 struct {
tukudani 0:65f9be2858c9 735 #ifdef H3694 // H8ではバイト毎に上位ビットから代入(ビッグエンディアン)
tukudani 0:65f9be2858c9 736 byte D7 :1; byte D6 :1; byte D5 :1; byte D4 :1; // BYTE[1]
tukudani 0:65f9be2858c9 737 byte D3 :1; byte D2 :1; byte D1 :1; byte D0 :1;
tukudani 0:65f9be2858c9 738 byte :1; byte :1; byte :1; byte D12:1; // BYTE[0]
tukudani 0:65f9be2858c9 739 byte D11:1; byte D10:1; byte :1; byte :1;
tukudani 0:65f9be2858c9 740 #else // PCではバイト毎に下位ビットから代入(リトルエンディアン)
tukudani 0:65f9be2858c9 741 byte D0 :1; byte D1 :1; byte D2 :1; byte D3 :1; // BYTE[1]
tukudani 0:65f9be2858c9 742 byte D4 :1; byte D5 :1; byte D6 :1; byte D7 :1;
tukudani 0:65f9be2858c9 743 byte :1; byte :1; byte D10:1; byte D11:1; // BYTE[0]
tukudani 0:65f9be2858c9 744 byte D12:1; byte :1; byte :1; byte :1;
tukudani 0:65f9be2858c9 745 #endif
tukudani 0:65f9be2858c9 746 } PORT;
tukudani 0:65f9be2858c9 747 } GPI;
tukudani 0:65f9be2858c9 748 unsigned int ADCIN[4]; // ADCデータ
tukudani 0:65f9be2858c9 749 byte ZCL[6]; // [0]送信元EndPoint, [1]宛先EndPoint, [2-3]クラスタID, [4-5]プロファイルID
tukudani 0:65f9be2858c9 750 byte DATA[CALL_SIZE]; // ペイロードデータ/ZCLヘッダ+受信データ
tukudani 0:65f9be2858c9 751 } XBEE_RESULT;
tukudani 0:65f9be2858c9 752 #endif
tukudani 0:65f9be2858c9 753 #endif
tukudani 0:65f9be2858c9 754 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 755 byte CACHE_MEM[CACHE_RES][API_SIZE];
tukudani 0:65f9be2858c9 756 byte CACHE_COUNTER = 0;
tukudani 0:65f9be2858c9 757 #endif
tukudani 0:65f9be2858c9 758 #endif
tukudani 0:65f9be2858c9 759
tukudani 0:65f9be2858c9 760 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 761 byte ADR_MY[] = {0xFF,0xFF,0xFF,0xFF};
tukudani 0:65f9be2858c9 762 #ifdef ARDUINO
tukudani 0:65f9be2858c9 763 extern byte mac[6];
tukudani 0:65f9be2858c9 764 EthernetUDP UdpXBeeR;
tukudani 0:65f9be2858c9 765 EthernetUDP UdpXBeeT;
tukudani 0:65f9be2858c9 766 EthernetUDP UdpXBeeU;
tukudani 0:65f9be2858c9 767 #endif
tukudani 0:65f9be2858c9 768 #endif
tukudani 0:65f9be2858c9 769
tukudani 0:65f9be2858c9 770 /*********************************************************************
tukudani 0:65f9be2858c9 771 スタティック変数の宣言
tukudani 0:65f9be2858c9 772 *********************************************************************/
tukudani 0:65f9be2858c9 773
tukudani 0:65f9be2858c9 774 /* エラーログ用 */
tukudani 0:65f9be2858c9 775 #ifdef ERRLOG
tukudani 0:65f9be2858c9 776 static byte TIMER_ERR = 0 ; //エラー経過時間1秒単位
tukudani 0:65f9be2858c9 777 static char ERR_LOG[API_TXSIZE-18];
tukudani 0:65f9be2858c9 778 static byte ERR_CODE=0x00;
tukudani 0:65f9be2858c9 779 #endif
tukudani 0:65f9be2858c9 780
tukudani 0:65f9be2858c9 781 /* IEEEアドレス(最小限の通信対象をライブラリ側で保持する)/複数のデバイスへの対応はアプリ側で行う*/
tukudani 0:65f9be2858c9 782 static byte ADR_FROM[]= {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF}; //差出人のIEEEアドレス(変更可)
tukudani 0:65f9be2858c9 783
tukudani 0:65f9be2858c9 784 // 以下にお手持ちのXBeeのアドレスを予め設定しておくと毎回の検索が不要です。
tukudani 0:65f9be2858c9 785 static byte ADR_DEST[]= {0x00,0x13,0xA2,0x00,0x00,0x00,0x00,0x00}; //宛先のIEEEアドレス(変更可)
tukudani 0:65f9be2858c9 786
tukudani 0:65f9be2858c9 787 /* ショートアドレス/本ライブラリでの宛先指定はIEEEのみを使う */
tukudani 0:65f9be2858c9 788 static byte SADR_DEST[]= {0xFF,0xFE}; //ブロード(ショート)アドレス
tukudani 0:65f9be2858c9 789 static byte PACKET_ID = 0; //送信パケット番号
tukudani 0:65f9be2858c9 790
tukudani 0:65f9be2858c9 791 /* XBeeのデバイスタイプ ATVRの上2ケタ */
tukudani 0:65f9be2858c9 792 static byte DEVICE_TYPE = ZB_TYPE_COORD; // Coord=0x21 Router=23 ED=29 Wi-Fi=10
tukudani 0:65f9be2858c9 793
tukudani 0:65f9be2858c9 794 #ifndef H3694
tukudani 0:65f9be2858c9 795 #ifndef ARDUINO
tukudani 0:65f9be2858c9 796 #ifndef ARM_MBED // PC
tukudani 0:65f9be2858c9 797 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 798 static byte xbee_com_port;
tukudani 0:65f9be2858c9 799 static int xbeeComFd;
tukudani 0:65f9be2858c9 800 static struct termios oldtio; /* current serial port settings (現在のシリアルポートの設定を待避)*/
tukudani 0:65f9be2858c9 801 // static byte *receive_buffer;
tukudani 0:65f9be2858c9 802 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 803 int xbeeTSFd; // XBee Wi-Fi 送信ソケット用 ディスクリプタ
tukudani 0:65f9be2858c9 804 int xbeeRSFd; // XBee Wi-Fi 受信ソケット用 ディスクリプタ
tukudani 0:65f9be2858c9 805 int xbeeUSFd; // XBee Wi-Fi UARTソケット用 ディスクリプタ
tukudani 0:65f9be2858c9 806 struct sockaddr_in xbeeT_addr; // XBee Wi-Fi 送信アドレス構造体変数
tukudani 0:65f9be2858c9 807 struct sockaddr_in xbeeR_addr; // XBee Wi-Fi 受信アドレス構造体変数
tukudani 0:65f9be2858c9 808 struct sockaddr_in xbeeU_addr; // XBee Wi-Fi UARTアドレス構造体変数
tukudani 0:65f9be2858c9 809 #endif
tukudani 0:65f9be2858c9 810 #endif
tukudani 0:65f9be2858c9 811 #endif
tukudani 0:65f9be2858c9 812 #endif
tukudani 0:65f9be2858c9 813
tukudani 0:65f9be2858c9 814 /*********************************************************************
tukudani 0:65f9be2858c9 815 ハードウェア
tukudani 0:65f9be2858c9 816 *********************************************************************/
tukudani 0:65f9be2858c9 817
tukudani 0:65f9be2858c9 818 /* H8タイニーマイコン H3694用のタイマー設定、GPIOポート設定 */
tukudani 0:65f9be2858c9 819 #ifdef H3694
tukudani 0:65f9be2858c9 820 void port_init(void){
tukudani 0:65f9be2858c9 821
tukudani 0:65f9be2858c9 822 /*ポート1 76543210*/
tukudani 0:65f9be2858c9 823 IO.PMR1.BYTE = 0b00000000; // モード(P1,5) 入出力=0 その他=1
tukudani 0:65f9be2858c9 824 IO.PCR1 = 0b11110110; // 入出力設定 入力 =0 出力 =1 B3=リザーブ
tukudani 0:65f9be2858c9 825 IO.PUCR1.BYTE = 0b00000000; // プルアップ(P1,5) しない=0 する =1
tukudani 0:65f9be2858c9 826 IO.PDR1.BYTE = 0b00000000; // アクセス L出力=0 H出力=1
tukudani 0:65f9be2858c9 827
tukudani 0:65f9be2858c9 828 /*ポート8 76543210*/ // 秋月 H8 Tiny I/O BOARD TERA2 [K-00207]用
tukudani 0:65f9be2858c9 829 IO.PCR8 = 0b11111011; // 入出力設定 入力 =0 出力 =1
tukudani 0:65f9be2858c9 830 IO.PDR8.BYTE = 0b00000000; // アクセス L出力=0 H出力=1
tukudani 0:65f9be2858c9 831 }
tukudani 0:65f9be2858c9 832
tukudani 0:65f9be2858c9 833 /* タイマー初期化 */
tukudani 0:65f9be2858c9 834 void timer_sec_init(void){
tukudani 0:65f9be2858c9 835 DI;
tukudani 0:65f9be2858c9 836 TA.TMA.BYTE =0b00001000; /* 時計用クロック */
tukudani 0:65f9be2858c9 837 /* | |||-|__ 分周比 000~111(高速時) 000~011(低即時)
tukudani 0:65f9be2858c9 838 固定値 ___|--|| 000:0.5120 sec. 000 1000 msec.
tukudani 0:65f9be2858c9 839 | 001:0.2560 sec. 001 500 msec.
tukudani 0:65f9be2858c9 840 高速=0, 低速=1 _______| 100:0.0160 sec. 010 250 msec.
tukudani 0:65f9be2858c9 841 111:0.0005 sec. 011 31.25 msec.*/
tukudani 0:65f9be2858c9 842
tukudani 0:65f9be2858c9 843 IRR1.BIT.IRRTA =0; /* タイマーA割込みフラグのリセット */
tukudani 0:65f9be2858c9 844 IENR1.BIT.IENTA =1; /* タイマーA割込みを利用可能にする */
tukudani 0:65f9be2858c9 845 EI;
tukudani 0:65f9be2858c9 846 }
tukudani 0:65f9be2858c9 847
tukudani 0:65f9be2858c9 848 /* AKI H8 TINY IO BOARD TERA2 用/PC用の接続デバイス設定*/
tukudani 0:65f9be2858c9 849 byte led_red( const byte in ){
tukudani 0:65f9be2858c9 850 if ( in == 0 ) LED1_OUT=0;
tukudani 0:65f9be2858c9 851 else if ( in == 1 ) LED1_OUT=1;
tukudani 0:65f9be2858c9 852 return( LED1_OUT );
tukudani 0:65f9be2858c9 853 }
tukudani 0:65f9be2858c9 854 byte led_green( const byte in ){
tukudani 0:65f9be2858c9 855 if ( in == 0 ) LED2_OUT=0;
tukudani 0:65f9be2858c9 856 else if ( in == 1 ) LED2_OUT=1;
tukudani 0:65f9be2858c9 857 return( LED2_OUT );
tukudani 0:65f9be2858c9 858 }
tukudani 0:65f9be2858c9 859 byte button( void ){
tukudani 0:65f9be2858c9 860 return( BUTN );
tukudani 0:65f9be2858c9 861 }
tukudani 0:65f9be2858c9 862 void lcd_enable( const byte in ){
tukudani 0:65f9be2858c9 863 LCD_EN = in;
tukudani 0:65f9be2858c9 864 }
tukudani 0:65f9be2858c9 865 #endif
tukudani 0:65f9be2858c9 866
tukudani 0:65f9be2858c9 867 /* ARM mbed 用 のタイマー設定 */
tukudani 0:65f9be2858c9 868 #ifdef ARM_MBED // http://mbed.org/handbook/Timer
tukudani 0:65f9be2858c9 869 Timer _xbee_time; // http://mbed.org/teams/Aerodyne/code/Timer/file/1d3fd5616c0a/Timer.cpp
tukudani 0:65f9be2858c9 870 void _xbee_time_init(){
tukudani 0:65f9be2858c9 871 _xbee_time.start();
tukudani 0:65f9be2858c9 872 }
tukudani 0:65f9be2858c9 873 #endif
tukudani 0:65f9be2858c9 874
tukudani 0:65f9be2858c9 875 /* RTC使用1秒カウント */
tukudani 0:65f9be2858c9 876 byte time1s256(){
tukudani 0:65f9be2858c9 877 #ifdef H3694
tukudani 0:65f9be2858c9 878 return(0x00);
tukudani 0:65f9be2858c9 879 #elif ARDUINO
tukudani 0:65f9be2858c9 880 return( (byte)(millis()/1000) );
tukudani 0:65f9be2858c9 881 #else
tukudani 0:65f9be2858c9 882 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 883 // return( (byte)(_xbee_time.read_ms()/1024) );
tukudani 0:65f9be2858c9 884 if(_xbee_time.read()>=262144.0f)_xbee_time.reset();
tukudani 0:65f9be2858c9 885 return( (byte)(_xbee_time.read()) );
tukudani 0:65f9be2858c9 886 // return(0);
tukudani 0:65f9be2858c9 887 #else //PC
tukudani 0:65f9be2858c9 888 // char c='\0';
tukudani 0:65f9be2858c9 889 // if (kbhit()) c = getch();
tukudani 0:65f9be2858c9 890 time_t sec;
tukudani 0:65f9be2858c9 891 time( &sec );
tukudani 0:65f9be2858c9 892 return( (byte)sec );
tukudani 0:65f9be2858c9 893 #endif
tukudani 0:65f9be2858c9 894 #endif
tukudani 0:65f9be2858c9 895 }
tukudani 0:65f9be2858c9 896
tukudani 0:65f9be2858c9 897 /* RTCによる割り込み処理(1秒毎) */
tukudani 0:65f9be2858c9 898 #ifdef H3694
tukudani 0:65f9be2858c9 899 void int_timera(void){
tukudani 0:65f9be2858c9 900 led_green( TIMER_SEC & 0x01 ); // LEDの正常点滅
tukudani 0:65f9be2858c9 901 #ifdef ERRLOG
tukudani 0:65f9be2858c9 902 if( led_red( 0xFF ) == 1 ) TIMER_ERR++; // エラー秒数(LED_赤でエラー検出)
tukudani 0:65f9be2858c9 903 #endif
tukudani 0:65f9be2858c9 904 TIMER_SEC++; // タイマーのカウントアップ
tukudani 0:65f9be2858c9 905 IRR1.BIT.IRRTA=0; // タイマーA割込フラグのリセット
tukudani 0:65f9be2858c9 906 }
tukudani 0:65f9be2858c9 907 #endif
tukudani 0:65f9be2858c9 908
tukudani 0:65f9be2858c9 909 /* シリアル待ち時間の時間カウント用、RTC使用1秒間に256カウント */
tukudani 0:65f9be2858c9 910 byte timera(void){
tukudani 0:65f9be2858c9 911 #ifdef H3694
tukudani 0:65f9be2858c9 912 return( (byte)TA.TCA );
tukudani 0:65f9be2858c9 913 #elif ARDUINO
tukudani 0:65f9be2858c9 914 return( (byte)( (millis()/4)%256 ) );
tukudani 0:65f9be2858c9 915 #else
tukudani 0:65f9be2858c9 916 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 917 return( (byte)((_xbee_time.read_ms()/4)%256) );
tukudani 0:65f9be2858c9 918 // return( (byte)(_xbee_time.read()*256) );
tukudani 0:65f9be2858c9 919 // return(0);
tukudani 0:65f9be2858c9 920 #else //PC
tukudani 0:65f9be2858c9 921 return( (byte)(clock()/(CLOCKS_PER_SEC/256)) );
tukudani 0:65f9be2858c9 922 #endif
tukudani 0:65f9be2858c9 923 #endif
tukudani 0:65f9be2858c9 924 }
tukudani 0:65f9be2858c9 925
tukudani 0:65f9be2858c9 926 /* ミリ秒待ち(250ms以下の高精度用) 入力範囲=4~250 ms */
tukudani 0:65f9be2858c9 927 #ifndef ARDUINO // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 928 void wait_millisec_250( byte ms ){
tukudani 0:65f9be2858c9 929 #ifdef H3694
tukudani 0:65f9be2858c9 930 byte counter;
tukudani 0:65f9be2858c9 931
tukudani 0:65f9be2858c9 932 if( ms < 0x04 ) ms = 0x04;
tukudani 0:65f9be2858c9 933 counter = timera() + (byte)( ms>>2 );
tukudani 0:65f9be2858c9 934 if( counter == timera() ) counter++;
tukudani 0:65f9be2858c9 935 while( counter != timera() );
tukudani 0:65f9be2858c9 936 #elif ARDUINO
tukudani 0:65f9be2858c9 937 delay( (unsigned long) ms );
tukudani 0:65f9be2858c9 938 #else
tukudani 0:65f9be2858c9 939 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 940 wait((float)ms/1000);
tukudani 0:65f9be2858c9 941 #endif
tukudani 0:65f9be2858c9 942 #endif
tukudani 0:65f9be2858c9 943 }
tukudani 0:65f9be2858c9 944 #endif
tukudani 0:65f9be2858c9 945
tukudani 0:65f9be2858c9 946 /* ミリ秒待ち(30秒までの広範囲版) 入力範囲=4~30,000 ms */
tukudani 0:65f9be2858c9 947 void wait_millisec( const unsigned int ms ){
tukudani 0:65f9be2858c9 948 #ifdef H3694
tukudani 0:65f9be2858c9 949 byte loops;
tukudani 0:65f9be2858c9 950 if( ms < 250 ){
tukudani 0:65f9be2858c9 951 wait_millisec_250( (byte)ms );
tukudani 0:65f9be2858c9 952 }else{
tukudani 0:65f9be2858c9 953 loops = (byte)( ms / 250);
tukudani 0:65f9be2858c9 954 wait_millisec_250( (byte)(ms-loops*250) );
tukudani 0:65f9be2858c9 955 while( loops > 0x00 ){
tukudani 0:65f9be2858c9 956 wait_millisec_250( 0xFA );
tukudani 0:65f9be2858c9 957 loops--;
tukudani 0:65f9be2858c9 958 }
tukudani 0:65f9be2858c9 959 }
tukudani 0:65f9be2858c9 960 #elif ARDUINO
tukudani 0:65f9be2858c9 961 delay( (unsigned long) ms );
tukudani 0:65f9be2858c9 962 #else
tukudani 0:65f9be2858c9 963 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 964 wait((float)ms/1000.0f);
tukudani 0:65f9be2858c9 965 #else // PC
tukudani 0:65f9be2858c9 966 time_t target;
tukudani 0:65f9be2858c9 967
tukudani 0:65f9be2858c9 968 target = (time_t)(clock()/(CLOCKS_PER_SEC/1000)) + (time_t)ms;
tukudani 0:65f9be2858c9 969 if( target >= (time_t)ms ){
tukudani 0:65f9be2858c9 970 while( (time_t)(clock()/(CLOCKS_PER_SEC/1000)) <= target );
tukudani 0:65f9be2858c9 971 }else{
tukudani 0:65f9be2858c9 972 while( (time_t)(clock()/(CLOCKS_PER_SEC/1000)) > (time_t)ms ); // クロックがリセットされるまで待つ
tukudani 0:65f9be2858c9 973 while( (time_t)(clock()/(CLOCKS_PER_SEC/1000)) <= target );
tukudani 0:65f9be2858c9 974 }
tukudani 0:65f9be2858c9 975 #endif
tukudani 0:65f9be2858c9 976 #endif
tukudani 0:65f9be2858c9 977 }
tukudani 0:65f9be2858c9 978
tukudani 0:65f9be2858c9 979 /* 経過時間測定=4~1000 ms 以内 単位は1/256秒毎 */
tukudani 0:65f9be2858c9 980 byte past_time(const byte time_from){
tukudani 0:65f9be2858c9 981 return( timera() - time_from );
tukudani 0:65f9be2858c9 982 }
tukudani 0:65f9be2858c9 983
tukudani 0:65f9be2858c9 984 /* XBeeのADCの有効ポート数を調べる ADC1~3のみ 入力=バイトデータ 応答0~3個 */
tukudani 0:65f9be2858c9 985 byte xbee_adc_count( byte d ){
tukudani 0:65f9be2858c9 986 return( ((d>>1)&0x01)+((d>>2)&0x01)+((d>>3)&0x01) );
tukudani 0:65f9be2858c9 987 }
tukudani 0:65f9be2858c9 988
tukudani 0:65f9be2858c9 989 /* XBee用シリアル通信ドライバ */
tukudani 0:65f9be2858c9 990 #ifndef ARDUINO
tukudani 0:65f9be2858c9 991 #ifndef H3694
tukudani 0:65f9be2858c9 992 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 993 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 994 int open_serial_port(char *modem_dev){
tukudani 0:65f9be2858c9 995 /*
tukudani 0:65f9be2858c9 996 PC-UNIX関連ドキュメント
tukudani 0:65f9be2858c9 997 「シリアルポートプログラミング」
tukudani 0:65f9be2858c9 998 http://www006.upp.so-net.ne.jp/kasuya/linux/serial_io_programing.html
tukudani 0:65f9be2858c9 999 writer:粕谷友章 kasuya@pd5.so-net.ne.jp
tukudani 0:65f9be2858c9 1000 */
tukudani 0:65f9be2858c9 1001 struct termios newtio;
tukudani 0:65f9be2858c9 1002 speed_t speed = B9600;
tukudani 0:65f9be2858c9 1003 xbeeComFd=open(modem_dev,O_RDWR);
tukudani 0:65f9be2858c9 1004 if(xbeeComFd < 0){
tukudani 0:65f9be2858c9 1005 perror(modem_dev);
tukudani 0:65f9be2858c9 1006 return -1;
tukudani 0:65f9be2858c9 1007 }else{
tukudani 0:65f9be2858c9 1008 tcgetattr(xbeeComFd,&oldtio);
tukudani 0:65f9be2858c9 1009 newtio.c_iflag = 0;
tukudani 0:65f9be2858c9 1010 newtio.c_oflag = 0;
tukudani 0:65f9be2858c9 1011 newtio.c_cflag = 0;
tukudani 0:65f9be2858c9 1012 newtio.c_lflag = 0;
tukudani 0:65f9be2858c9 1013 newtio.c_line = 0;
tukudani 0:65f9be2858c9 1014 bzero(newtio.c_cc,sizeof(newtio.c_cc));
tukudani 0:65f9be2858c9 1015 cfsetispeed( &newtio, speed );
tukudani 0:65f9be2858c9 1016 cfsetospeed( &newtio, speed );
tukudani 0:65f9be2858c9 1017 newtio.c_cflag = BAUDRATE | CLOCAL | CREAD ;
tukudani 0:65f9be2858c9 1018 newtio.c_cflag &= ~CSIZE;
tukudani 0:65f9be2858c9 1019 newtio.c_cflag |= CS8;
tukudani 0:65f9be2858c9 1020 newtio.c_oflag = 0;
tukudani 0:65f9be2858c9 1021 newtio.c_lflag = 0;
tukudani 0:65f9be2858c9 1022 newtio.c_cc[VMIN] = 0;
tukudani 0:65f9be2858c9 1023 newtio.c_cc[VTIME] = 0;
tukudani 0:65f9be2858c9 1024 tcflush(xbeeComFd,TCIFLUSH);
tukudani 0:65f9be2858c9 1025 tcsetattr(xbeeComFd,TCSANOW,&newtio);
tukudani 0:65f9be2858c9 1026 }
tukudani 0:65f9be2858c9 1027 return 0;
tukudani 0:65f9be2858c9 1028 }
tukudani 0:65f9be2858c9 1029 void close_serial_port(void){
tukudani 0:65f9be2858c9 1030 tcsetattr(xbeeComFd,TCSANOW,&oldtio);
tukudani 0:65f9be2858c9 1031 close(xbeeComFd);
tukudani 0:65f9be2858c9 1032 }
tukudani 0:65f9be2858c9 1033 #else // XBEE_WIFI(arduino除く)
tukudani 0:65f9be2858c9 1034 int open_serial_port_tx(const byte *address){ // modem_dev=IPアドレスのポインタ
tukudani 0:65f9be2858c9 1035 byte i;
tukudani 0:65f9be2858c9 1036 in_addr_t ip=0; // 送信アドレス
tukudani 0:65f9be2858c9 1037
tukudani 0:65f9be2858c9 1038 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 1039 ip *= 256;
tukudani 0:65f9be2858c9 1040 ip += (in_addr_t)(byte)address[3-i]; // アドレス順序は反転する
tukudani 0:65f9be2858c9 1041 }
tukudani 0:65f9be2858c9 1042 /* 送信アドレス設定 */
tukudani 0:65f9be2858c9 1043 memset(&xbeeT_addr, 0, sizeof(xbeeT_addr)); // xbeeT_addrの初期化
tukudani 0:65f9be2858c9 1044 xbeeT_addr.sin_family = AF_INET; // アドレスファミリー AF_INET
tukudani 0:65f9be2858c9 1045 xbeeT_addr.sin_port = htons( 0xBEE ); // 送信ポート番号
tukudani 0:65f9be2858c9 1046 xbeeT_addr.sin_addr.s_addr = ip; // 送信IPアドレス
tukudani 0:65f9be2858c9 1047 /* ソケット生成 */
tukudani 0:65f9be2858c9 1048 xbeeTSFd = socket(AF_INET, SOCK_DGRAM, 0); // 送信用ソケットの生成
tukudani 0:65f9be2858c9 1049 #ifdef DEBUG
tukudani 0:65f9be2858c9 1050 printf("IP(TX)=%s\n", inet_ntoa( xbeeT_addr.sin_addr ) );
tukudani 0:65f9be2858c9 1051 #endif
tukudani 0:65f9be2858c9 1052 return 0;
tukudani 0:65f9be2858c9 1053 }
tukudani 0:65f9be2858c9 1054 int open_serial_port_rx( void ){ // modem_dev=IPアドレスのポインタ
tukudani 0:65f9be2858c9 1055 /* 受信アドレス設定 */
tukudani 0:65f9be2858c9 1056 memset(&xbeeR_addr, 0, sizeof(xbeeR_addr)); // xbeeR_addrの初期化
tukudani 0:65f9be2858c9 1057 memset(&xbeeU_addr, 0, sizeof(xbeeU_addr)); // xbeeU_addrの初期化
tukudani 0:65f9be2858c9 1058 xbeeR_addr.sin_family = AF_INET; // アドレスファミリー AF_INET
tukudani 0:65f9be2858c9 1059 xbeeU_addr.sin_family = AF_INET; // アドレスファミリー AF_INET
tukudani 0:65f9be2858c9 1060 xbeeR_addr.sin_port = htons( 0xBEE ); // 受信ポート番号(3054)
tukudani 0:65f9be2858c9 1061 xbeeU_addr.sin_port = htons( 0x2616 ); // 受信ポート番号(9750 board-voip)
tukudani 0:65f9be2858c9 1062 xbeeR_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 受信IPアドレス(ANY)
tukudani 0:65f9be2858c9 1063 xbeeU_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 受信IPアドレス(ANY)
tukudani 0:65f9be2858c9 1064 /* ソケット生成 */
tukudani 0:65f9be2858c9 1065 xbeeRSFd = socket(AF_INET, SOCK_DGRAM, 0); // 受信用ソケットの生成
tukudani 0:65f9be2858c9 1066 xbeeUSFd = socket(AF_INET, SOCK_DGRAM, 0); // 受信用ソケットの生成
tukudani 0:65f9be2858c9 1067 /* バインド */
tukudani 0:65f9be2858c9 1068 return(
tukudani 0:65f9be2858c9 1069 bind(xbeeRSFd, (struct sockaddr *)&xbeeR_addr, sizeof(xbeeR_addr)) +
tukudani 0:65f9be2858c9 1070 bind(xbeeUSFd, (struct sockaddr *)&xbeeU_addr, sizeof(xbeeU_addr))
tukudani 0:65f9be2858c9 1071 );
tukudani 0:65f9be2858c9 1072 }
tukudani 0:65f9be2858c9 1073 int open_serial_port(const byte *modem_dev){ // modem_dev=IPアドレスのポインタ
tukudani 0:65f9be2858c9 1074 /* UDPソケットを生成する。戻り値:0=成功、-1=エラー
tukudani 0:65f9be2858c9 1075 入力はcharだけど中ではbyteとして扱う。byteアドレス→char入力→byte使用なので変換は不要 */
tukudani 0:65f9be2858c9 1076 open_serial_port_tx( modem_dev );
tukudani 0:65f9be2858c9 1077 return( open_serial_port_rx( ) );
tukudani 0:65f9be2858c9 1078 }
tukudani 0:65f9be2858c9 1079 void close_serial_port_tx(void){
tukudani 0:65f9be2858c9 1080 close(xbeeTSFd);
tukudani 0:65f9be2858c9 1081 #ifdef DEBUG
tukudani 0:65f9be2858c9 1082 printf("DEBUG:close xbee tx sock\n");
tukudani 0:65f9be2858c9 1083 #endif
tukudani 0:65f9be2858c9 1084 }
tukudani 0:65f9be2858c9 1085 void close_serial_port(void){
tukudani 0:65f9be2858c9 1086 close(xbeeTSFd);
tukudani 0:65f9be2858c9 1087 close(xbeeRSFd);
tukudani 0:65f9be2858c9 1088 close(xbeeUSFd);
tukudani 0:65f9be2858c9 1089 #ifdef DEBUG
tukudani 0:65f9be2858c9 1090 printf("DEBUG:close xbee tx and rx sock\n");
tukudani 0:65f9be2858c9 1091 #endif
tukudani 0:65f9be2858c9 1092 }
tukudani 0:65f9be2858c9 1093 #endif
tukudani 0:65f9be2858c9 1094 #endif // not ARM
tukudani 0:65f9be2858c9 1095 #endif // not H3694
tukudani 0:65f9be2858c9 1096 #endif // not ARDUINO
tukudani 0:65f9be2858c9 1097
tukudani 0:65f9be2858c9 1098 #ifdef ARDUINO
tukudani 0:65f9be2858c9 1099 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 1100 int open_serial_port(void){
tukudani 0:65f9be2858c9 1101 Ethernet.begin(mac);
tukudani 0:65f9be2858c9 1102 delay(1000);
tukudani 0:65f9be2858c9 1103 UdpXBeeU.begin(0x2616); // UART受信用のUDP開始
tukudani 0:65f9be2858c9 1104 UdpXBeeR.begin(0xBEE); // URAT リモートATコマンド受信用のUDPの開始
tukudani 0:65f9be2858c9 1105 UdpXBeeT.begin(0xBEE); // URAT リモートATコマンド送信用のUDPの開始
tukudani 0:65f9be2858c9 1106 return( 0 );
tukudani 0:65f9be2858c9 1107 }
tukudani 0:65f9be2858c9 1108 #endif // XBEE_WIFI
tukudani 0:65f9be2858c9 1109 #endif // ARDUINO
tukudani 0:65f9be2858c9 1110
tukudani 0:65f9be2858c9 1111 /* シリアルの初期化 */
tukudani 0:65f9be2858c9 1112 /*
tukudani 0:65f9be2858c9 1113 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1114 RawSerial _xbee_serial(SERIAL_TX, SERIAL_RX); // USART2
tukudani 0:65f9be2858c9 1115 // RawSerial _xbee_serial(D1, D0); //
tukudani 0:65f9be2858c9 1116 #ifdef DEBUG
tukudani 0:65f9be2858c9 1117 RawSerial _xbee_debug(PA_9, NC); // USART1
tukudani 0:65f9be2858c9 1118 // RawSerial _xbee_debug(PA_9, PA_10); // USART1
tukudani 0:65f9be2858c9 1119 #endif
tukudani 0:65f9be2858c9 1120 #endif
tukudani 0:65f9be2858c9 1121 */
tukudani 0:65f9be2858c9 1122
tukudani 0:65f9be2858c9 1123 /* シリアル受信用コールバック(ARM専用) */
tukudani 0:65f9be2858c9 1124 /* Serial.readableで確認してからSerial.getcする方法だと、連続した2バイト以上の
tukudani 0:65f9be2858c9 1125  データ列を受信できなかった。STマイクロの問題なのかmbedの問題かは不明だが、
tukudani 0:65f9be2858c9 1126  ハードウェアの受信バッファ内のデータをうまく読めないものと思う。
tukudani 0:65f9be2858c9 1127  したがって、割込みで取り込んでソフト受信バッファを実装することにした。
tukudani 0:65f9be2858c9 1128  バッファサイズはとりあえず128バイト */
tukudani 0:65f9be2858c9 1129 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1130 #define ARM_RX_BUFFER 128
tukudani 0:65f9be2858c9 1131 volatile byte _xbee_serial_buf[ARM_RX_BUFFER];
tukudani 0:65f9be2858c9 1132 volatile byte _xbee_serial_bp=0;
tukudani 0:65f9be2858c9 1133 volatile byte _xbee_serial_rp=0;
tukudani 0:65f9be2858c9 1134 void _xbee_serial_callback(){
tukudani 0:65f9be2858c9 1135 _xbee_serial_buf[_xbee_serial_bp] = _xbee_serial.getc();
tukudani 0:65f9be2858c9 1136 if(_xbee_serial_bp < (ARM_RX_BUFFER-1))_xbee_serial_bp++;
tukudani 0:65f9be2858c9 1137 else{
tukudani 0:65f9be2858c9 1138 #ifdef DEBUG
tukudani 0:65f9be2858c9 1139 _xbee_debug.printf("ERR:Buffer Full(%d)\r\n",_xbee_serial_bp);
tukudani 0:65f9be2858c9 1140 #endif
tukudani 0:65f9be2858c9 1141 }
tukudani 0:65f9be2858c9 1142 }
tukudani 0:65f9be2858c9 1143 #endif
tukudani 0:65f9be2858c9 1144
tukudani 0:65f9be2858c9 1145 /* シリアルの初期化 */
tukudani 0:65f9be2858c9 1146 byte sci_init( byte port ){
tukudani 0:65f9be2858c9 1147 #ifdef H3694
tukudani 0:65f9be2858c9 1148 SCI3_INIT(br9600, sci_tx, SCI_SIZE, sci_tx, SCI_SIZE);
tukudani 0:65f9be2858c9 1149 EI; // 割り込み許可
tukudani 0:65f9be2858c9 1150 return(1);
tukudani 0:65f9be2858c9 1151 #elif ARDUINO
tukudani 0:65f9be2858c9 1152 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1153 #ifdef LEONARDO
tukudani 0:65f9be2858c9 1154 Serial1.begin(9600);
tukudani 0:65f9be2858c9 1155 #else
tukudani 0:65f9be2858c9 1156 Serial.begin(9600);
tukudani 0:65f9be2858c9 1157 #endif
tukudani 0:65f9be2858c9 1158 return(1);
tukudani 0:65f9be2858c9 1159 #else // Arduino XBee_WIFI
tukudani 0:65f9be2858c9 1160 byte i;
tukudani 0:65f9be2858c9 1161 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 1162 Serial.begin(9600); // TEST用
tukudani 0:65f9be2858c9 1163 Serial.println("Hello!");
tukudani 0:65f9be2858c9 1164 #endif
tukudani 0:65f9be2858c9 1165 open_serial_port();
tukudani 0:65f9be2858c9 1166 return(1);
tukudani 0:65f9be2858c9 1167 #endif
tukudani 0:65f9be2858c9 1168 #else
tukudani 0:65f9be2858c9 1169 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1170 _xbee_serial.baud(57600);
Jumpei 1:12c5a8179ad2 1171 //_xbee_serial.baud(38400);
tukudani 0:65f9be2858c9 1172 _xbee_serial.attach(&_xbee_serial_callback);
tukudani 0:65f9be2858c9 1173 #ifdef DEBUG
tukudani 0:65f9be2858c9 1174 _xbee_debug.baud(38400);
tukudani 0:65f9be2858c9 1175 #endif
tukudani 0:65f9be2858c9 1176 return(1);
tukudani 0:65f9be2858c9 1177 #else // PC の時(ZigBeeシリアル or WiFi_LAN)
tukudani 0:65f9be2858c9 1178 #ifndef XBEE_WIFI // ZigBeeシリアル
tukudani 0:65f9be2858c9 1179 /* tasasaki様よりポート11~64の拡張対応方法を教えていただいて追加した。*/
tukudani 0:65f9be2858c9 1180 char modem_dev[12] = "/dev/ttyS00";
tukudani 0:65f9be2858c9 1181
tukudani 0:65f9be2858c9 1182 if( port <= 10){
tukudani 0:65f9be2858c9 1183 modem_dev[9] = (char)( port - 1 + (byte)'0' );
tukudani 0:65f9be2858c9 1184 modem_dev[10]= '\0';
tukudani 0:65f9be2858c9 1185 }else if( port <= 64 ){
tukudani 0:65f9be2858c9 1186 snprintf(&modem_dev[9], 3, "%d", port - 1);
tukudani 0:65f9be2858c9 1187 }else{
tukudani 0:65f9be2858c9 1188 fprintf(stderr,"ERR:sci_init port=%d\n",port);
tukudani 0:65f9be2858c9 1189 return(0);
tukudani 0:65f9be2858c9 1190 }
tukudani 0:65f9be2858c9 1191 if( open_serial_port( modem_dev ) ){
tukudani 0:65f9be2858c9 1192 wait_millisec( 100 );
tukudani 0:65f9be2858c9 1193 close_serial_port(); // open出来ていないが念のために閉じる
tukudani 0:65f9be2858c9 1194 wait_millisec( 100 );
tukudani 0:65f9be2858c9 1195 fprintf(stderr,"Failed serial COM%d (%s)\n",port,modem_dev);
tukudani 0:65f9be2858c9 1196 port = 0;
tukudani 0:65f9be2858c9 1197 }else{
tukudani 0:65f9be2858c9 1198 fprintf(stderr,"Serial port = COM%d (%s)\n",port,modem_dev);
tukudani 0:65f9be2858c9 1199 xbee_com_port = port;
tukudani 0:65f9be2858c9 1200 }
tukudani 0:65f9be2858c9 1201 return( port );
tukudani 0:65f9be2858c9 1202 #else // XBEE_WIFI PC用
tukudani 0:65f9be2858c9 1203 byte i,j;
tukudani 0:65f9be2858c9 1204 for(i=0;i<3;i++){
tukudani 0:65f9be2858c9 1205 if( open_serial_port( ADR_DEST ) < 0 ){
tukudani 0:65f9be2858c9 1206 fprintf(stderr,"Failed UDP[%d/3]:",i+1);
tukudani 0:65f9be2858c9 1207 for(j=0;j<4;j++) fprintf(stderr,"%d.",(int)ADR_DEST[j]);
tukudani 0:65f9be2858c9 1208 fprintf(stderr,"\b\n");
tukudani 0:65f9be2858c9 1209 wait_millisec( 100 );
tukudani 0:65f9be2858c9 1210 close_serial_port(); // open出来ていないが念のために閉じる
tukudani 0:65f9be2858c9 1211 wait_millisec( 900 );
tukudani 0:65f9be2858c9 1212 }else break;
tukudani 0:65f9be2858c9 1213 }
tukudani 0:65f9be2858c9 1214 #ifdef DEBUG
tukudani 0:65f9be2858c9 1215 printf("DEBUG:SCI init for WiFi done\n");
tukudani 0:65f9be2858c9 1216 #endif
tukudani 0:65f9be2858c9 1217 return( 3-i );
tukudani 0:65f9be2858c9 1218 #endif // Wifi
tukudani 0:65f9be2858c9 1219 #endif // ARM/PC
tukudani 0:65f9be2858c9 1220 #endif
tukudani 0:65f9be2858c9 1221 }
tukudani 0:65f9be2858c9 1222
tukudani 0:65f9be2858c9 1223 /* シリアル送受信バッファクリア */
tukudani 0:65f9be2858c9 1224 void sci_clear(void){
tukudani 0:65f9be2858c9 1225 #ifdef H3694
tukudani 0:65f9be2858c9 1226 SCI3_IN_DATA_CLEAR();
tukudani 0:65f9be2858c9 1227 SCI3_OUT_DATA_CLEAR();
tukudani 0:65f9be2858c9 1228 #elif ARDUINO
tukudani 0:65f9be2858c9 1229 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1230 #ifdef LEONARDO
tukudani 0:65f9be2858c9 1231 Serial1.flush();
tukudani 0:65f9be2858c9 1232 #else
tukudani 0:65f9be2858c9 1233 Serial.flush();
tukudani 0:65f9be2858c9 1234 #endif
tukudani 0:65f9be2858c9 1235 #endif
tukudani 0:65f9be2858c9 1236 #else
tukudani 0:65f9be2858c9 1237 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1238 while( _xbee_serial.readable() ){
tukudani 0:65f9be2858c9 1239 _xbee_serial.getc();
tukudani 0:65f9be2858c9 1240 wait(0.002);
tukudani 0:65f9be2858c9 1241 }
tukudani 0:65f9be2858c9 1242 while( !_xbee_serial.writeable() ){
tukudani 0:65f9be2858c9 1243 _xbee_serial.send_break();
tukudani 0:65f9be2858c9 1244 wait(1);
tukudani 0:65f9be2858c9 1245 }
tukudani 0:65f9be2858c9 1246 #else //PC
tukudani 0:65f9be2858c9 1247 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 1248 tcflush(xbeeComFd,TCIOFLUSH);
tukudani 0:65f9be2858c9 1249 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 1250 close_serial_port();
tukudani 0:65f9be2858c9 1251 sci_init( 0 );
tukudani 0:65f9be2858c9 1252 #endif
tukudani 0:65f9be2858c9 1253 #endif
tukudani 0:65f9be2858c9 1254 #endif
tukudani 0:65f9be2858c9 1255 }
tukudani 0:65f9be2858c9 1256
tukudani 0:65f9be2858c9 1257 /* シリアル受信(1バイト) */
tukudani 0:65f9be2858c9 1258 byte sci_read(byte timeout){
tukudani 0:65f9be2858c9 1259 #ifdef H3694
tukudani 0:65f9be2858c9 1260 byte timer;
tukudani 0:65f9be2858c9 1261 byte ret=0;
tukudani 0:65f9be2858c9 1262 timer = timera() + (timeout)+1; // timeout[ms] = timer/256*1000
tukudani 0:65f9be2858c9 1263 while( timer != timera() && SCI3_IN_DATA_CHECK() < 1 );
tukudani 0:65f9be2858c9 1264 if( SCI3_IN_DATA_CHECK() ) ret=(byte)SCI3_IN_DATA();
tukudani 0:65f9be2858c9 1265 return( ret );
tukudani 0:65f9be2858c9 1266 #elif ARDUINO // 蘭様による改良あり
tukudani 0:65f9be2858c9 1267 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1268 byte timer;
tukudani 0:65f9be2858c9 1269 timer = timera() + (timeout)+1; // timeout[ms] = timer/256*1000
tukudani 0:65f9be2858c9 1270 // led_green(0);
tukudani 0:65f9be2858c9 1271 #ifdef LEONARDO
tukudani 0:65f9be2858c9 1272 while( timer != timera() && Serial1.available() <= 0 );
tukudani 0:65f9be2858c9 1273 // led_green(1);
tukudani 0:65f9be2858c9 1274 if( Serial1.available() > 0 ) return( (byte)Serial1.read() );
tukudani 0:65f9be2858c9 1275 #else
tukudani 0:65f9be2858c9 1276 while( timer != timera() && Serial.available() <= 0 );
tukudani 0:65f9be2858c9 1277 // led_green(1);
tukudani 0:65f9be2858c9 1278 if( Serial.available() > 0 ) return( (byte)Serial.read() );
tukudani 0:65f9be2858c9 1279 #endif
tukudani 0:65f9be2858c9 1280 #else
tukudani 0:65f9be2858c9 1281 return(0x00); // フレームで受信するのでバイト毎受信は不要
tukudani 0:65f9be2858c9 1282 #endif
tukudani 0:65f9be2858c9 1283 #else
tukudani 0:65f9be2858c9 1284 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1285 // http://mbed.org/users/mbed_official/code/mbed-src/docs/e8b66477f5bf/classmbed_1_1RawSerial.html
tukudani 0:65f9be2858c9 1286 int value = 0, i = 0;
tukudani 0:65f9be2858c9 1287 volatile byte timer;
tukudani 0:65f9be2858c9 1288
tukudani 0:65f9be2858c9 1289 timer = timera() + (timeout)+1;
tukudani 0:65f9be2858c9 1290
tukudani 0:65f9be2858c9 1291 //while( timer != timera() && i == 0 ) i=_xbee_serial.readable();
tukudani 0:65f9be2858c9 1292 //while( timer != timera() && i == 0 ) i=_xbee_serial_bp;
tukudani 0:65f9be2858c9 1293 i = _xbee_serial_bp;
tukudani 0:65f9be2858c9 1294 if( i > 0 ){
tukudani 0:65f9be2858c9 1295 //value = _xbee_serial.getc();
tukudani 0:65f9be2858c9 1296 value = _xbee_serial_buf[_xbee_serial_rp];
tukudani 0:65f9be2858c9 1297 if(_xbee_serial_rp < (ARM_RX_BUFFER-1))_xbee_serial_rp++;
tukudani 0:65f9be2858c9 1298 if(_xbee_serial_bp <= _xbee_serial_rp ){
tukudani 0:65f9be2858c9 1299 _xbee_serial_bp=0;
tukudani 0:65f9be2858c9 1300 _xbee_serial_rp=0;
tukudani 0:65f9be2858c9 1301 }
tukudani 0:65f9be2858c9 1302 // #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 1303 // _xbee_debug.printf("%c(%02X) ",value,value);
tukudani 0:65f9be2858c9 1304 // #endif
tukudani 0:65f9be2858c9 1305 }
tukudani 0:65f9be2858c9 1306 if(value<0 || value>255) value=0;
tukudani 0:65f9be2858c9 1307 return( (byte)value );
tukudani 0:65f9be2858c9 1308 #else // PC
tukudani 0:65f9be2858c9 1309 /* 受信の有無の判断にFDの待ち受け関数selectを使用する。
tukudani 0:65f9be2858c9 1310 参考文献
tukudani 0:65f9be2858c9 1311 http://linuxjm.sourceforge.jp/html/LDP_man-pages/man2/select.2.html
tukudani 0:65f9be2858c9 1312 */
tukudani 0:65f9be2858c9 1313 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1314 byte c;
tukudani 0:65f9be2858c9 1315 struct timeval tv;
tukudani 0:65f9be2858c9 1316 fd_set readfds;
tukudani 0:65f9be2858c9 1317
tukudani 0:65f9be2858c9 1318 FD_ZERO(&readfds);
tukudani 0:65f9be2858c9 1319 FD_SET( xbeeComFd , &readfds);
tukudani 0:65f9be2858c9 1320 tv.tv_sec = 0;
tukudani 0:65f9be2858c9 1321 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 1322 /*
tukudani 0:65f9be2858c9 1323 if( timeout > 50 ){
tukudani 0:65f9be2858c9 1324 tv.tv_usec = timeout*600;
tukudani 0:65f9be2858c9 1325 }else if( timeout > 10 ){
tukudani 0:65f9be2858c9 1326 tv.tv_usec = timeout*750;
tukudani 0:65f9be2858c9 1327 }else{
tukudani 0:65f9be2858c9 1328 tv.tv_usec = 0;
tukudani 0:65f9be2858c9 1329 }
tukudani 0:65f9be2858c9 1330 */
tukudani 0:65f9be2858c9 1331 // 今回は見送ります(本関数を呼び出す前に調整しているので不要)
tukudani 0:65f9be2858c9 1332 tv.tv_usec = timeout*1000;
tukudani 0:65f9be2858c9 1333 #else
tukudani 0:65f9be2858c9 1334 tv.tv_usec = timeout*1000;
tukudani 0:65f9be2858c9 1335 #endif
tukudani 0:65f9be2858c9 1336 if( select( (xbeeComFd+1), &readfds, NULL, NULL ,&tv ) ){
tukudani 0:65f9be2858c9 1337 read(xbeeComFd,(char *)&c,1);
tukudani 0:65f9be2858c9 1338 }else{
tukudani 0:65f9be2858c9 1339 c = 0x00;
tukudani 0:65f9be2858c9 1340 }
tukudani 0:65f9be2858c9 1341 return( c );
tukudani 0:65f9be2858c9 1342 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 1343 return(0); // フレームで受信するのでバイト毎受信は不要
tukudani 0:65f9be2858c9 1344 #endif
tukudani 0:65f9be2858c9 1345 #endif
tukudani 0:65f9be2858c9 1346 #endif
tukudani 0:65f9be2858c9 1347 }
tukudani 0:65f9be2858c9 1348
tukudani 0:65f9be2858c9 1349 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 1350 /* シリアル受信(1フレーム) 始めの4バイトは送信元のアドレス */
tukudani 0:65f9be2858c9 1351 byte sci_read_frame(byte *data){
tukudani 0:65f9be2858c9 1352 #ifndef H3694
tukudani 0:65f9be2858c9 1353 #ifndef ARDUINO // XBEE_WIFI PC受信
tukudani 0:65f9be2858c9 1354 byte i,ret;
tukudani 0:65f9be2858c9 1355 int len=0;
tukudani 0:65f9be2858c9 1356 struct sockaddr_in xbeeF_addr; // FROMアドレス入力用
tukudani 0:65f9be2858c9 1357 struct timeval tv; // タイムアウト用
tukudani 0:65f9be2858c9 1358 fd_set readfds;
tukudani 0:65f9be2858c9 1359
tukudani 0:65f9be2858c9 1360 /* 受信の有無の判断にFDの待ち受け関数selectを使用する。*/
tukudani 0:65f9be2858c9 1361 FD_ZERO(&readfds); // FD初期化
tukudani 0:65f9be2858c9 1362 FD_SET(xbeeRSFd, &readfds); // 待ちソケットの登録
tukudani 0:65f9be2858c9 1363 tv.tv_sec = (long)0;
tukudani 0:65f9be2858c9 1364 tv.tv_usec = (long)9000; // 9ms
tukudani 0:65f9be2858c9 1365 /* データ受信 */
tukudani 0:65f9be2858c9 1366 if( select( (xbeeRSFd+1), &readfds, NULL, NULL, &tv) > 0 ){
tukudani 0:65f9be2858c9 1367 len = sizeof(xbeeF_addr);
tukudani 0:65f9be2858c9 1368 len = recvfrom(xbeeRSFd, &(data[4]), (API_SIZE-14), 0, (struct sockaddr *)&xbeeF_addr, &len );
tukudani 0:65f9be2858c9 1369 if( len > 0 ){
tukudani 0:65f9be2858c9 1370 len += 4;
tukudani 0:65f9be2858c9 1371 #ifdef DEBUG
tukudani 0:65f9be2858c9 1372 printf("IP(RX)=%s\n", inet_ntoa( xbeeF_addr.sin_addr ) );
tukudani 0:65f9be2858c9 1373 #endif
tukudani 0:65f9be2858c9 1374 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 1375 data[i]=(byte)(xbeeF_addr.sin_addr.s_addr % (long)256);
tukudani 0:65f9be2858c9 1376 xbeeF_addr.sin_addr.s_addr /= (long)256;
tukudani 0:65f9be2858c9 1377 }
tukudani 0:65f9be2858c9 1378 }
tukudani 0:65f9be2858c9 1379 }else{ // 受信データが無い時はUARTを受信する
tukudani 0:65f9be2858c9 1380 FD_ZERO(&readfds); // FD初期化
tukudani 0:65f9be2858c9 1381 FD_SET(xbeeUSFd, &readfds); // 待ちソケットの登録
tukudani 0:65f9be2858c9 1382 tv.tv_usec = (long)1000; // 1ms
tukudani 0:65f9be2858c9 1383 if( select( (xbeeUSFd+1), &readfds, NULL, NULL, &tv) > 0 ){
tukudani 0:65f9be2858c9 1384 len = sizeof(xbeeU_addr);
tukudani 0:65f9be2858c9 1385 len = recvfrom(xbeeUSFd, &(data[6]), (API_SIZE-16), 0, (struct sockaddr *)&xbeeU_addr, &len );
tukudani 0:65f9be2858c9 1386 if( len > 0 ){ // データはdata[6]以降に入る
tukudani 0:65f9be2858c9 1387 data[4] = 0x00; // UART受信を示す。
tukudani 0:65f9be2858c9 1388 data[5] = len; // UART長を示す(data[]長では無い)。
tukudani 0:65f9be2858c9 1389 len += 6;
tukudani 0:65f9be2858c9 1390 #ifdef DEBUG
tukudani 0:65f9be2858c9 1391 printf("IP(UART)=%s\n", inet_ntoa( xbeeU_addr.sin_addr ) );
tukudani 0:65f9be2858c9 1392 #endif
tukudani 0:65f9be2858c9 1393 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 1394 data[i]=(byte)(xbeeU_addr.sin_addr.s_addr % (long)256);
tukudani 0:65f9be2858c9 1395 xbeeU_addr.sin_addr.s_addr /= (long)256;
tukudani 0:65f9be2858c9 1396 }
tukudani 0:65f9be2858c9 1397 }
tukudani 0:65f9be2858c9 1398 }
tukudani 0:65f9be2858c9 1399 }
tukudani 0:65f9be2858c9 1400 if( len < 0 ) ret = 0; else if( len>255 ) ret = 0xFF; else ret =(byte)len;
tukudani 0:65f9be2858c9 1401 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 1402 if( ret ){
tukudani 0:65f9be2858c9 1403 printf("Recieved Packet rx[%3d]\nip0,ip1,ip2,ip3,urt,len,", len);
tukudani 0:65f9be2858c9 1404 for(i=6;i<len;i++) printf("%3d,",i);
tukudani 0:65f9be2858c9 1405 printf("\n");
tukudani 0:65f9be2858c9 1406 for(i=0;i<len;i++) printf(" %02X,",data[i]); // dataはbyte型
tukudani 0:65f9be2858c9 1407 printf("\n");
tukudani 0:65f9be2858c9 1408 for(i=0;i<len;i++) if( isgraph(data[i]) ) printf("'%c',",data[i]); else printf("%3d,",(int)data[i]);
tukudani 0:65f9be2858c9 1409 printf("\n");
tukudani 0:65f9be2858c9 1410 }
tukudani 0:65f9be2858c9 1411 #endif
tukudani 0:65f9be2858c9 1412 return( ret );
tukudani 0:65f9be2858c9 1413 #else // XBEE_WIFI ARDUINO受信
tukudani 0:65f9be2858c9 1414 int packetSize;
tukudani 0:65f9be2858c9 1415 IPAddress remote;
tukudani 0:65f9be2858c9 1416 byte i;
tukudani 0:65f9be2858c9 1417 byte ret=0;
tukudani 0:65f9be2858c9 1418
tukudani 0:65f9be2858c9 1419 /* UdpXBeeR受信 */
tukudani 0:65f9be2858c9 1420 packetSize = UdpXBeeR.parsePacket();
tukudani 0:65f9be2858c9 1421 if(packetSize){
tukudani 0:65f9be2858c9 1422 remote = UdpXBeeR.remoteIP();
tukudani 0:65f9be2858c9 1423 for(i=0;i<4;i++) data[i]=(byte)remote[i];
tukudani 0:65f9be2858c9 1424 if( packetSize > (API_SIZE-14) ) ret = API_SIZE-14; //API_SIZEはIPのため-4で良いと思ったが、ZBフォーマット移行の-9-1-4が上限)
tukudani 0:65f9be2858c9 1425 else ret = (byte)packetSize;
tukudani 0:65f9be2858c9 1426 UdpXBeeR.read(&(data[4]), ret);
tukudani 0:65f9be2858c9 1427 ret += 4;
tukudani 0:65f9be2858c9 1428 }else{
tukudani 0:65f9be2858c9 1429 /* UdpXBeeU受信 */
tukudani 0:65f9be2858c9 1430 packetSize = UdpXBeeU.parsePacket();
tukudani 0:65f9be2858c9 1431 if(packetSize){
tukudani 0:65f9be2858c9 1432 remote = UdpXBeeU.remoteIP();
tukudani 0:65f9be2858c9 1433 for(i=0;i<4;i++) data[i]=(byte)remote[i];
tukudani 0:65f9be2858c9 1434 if( packetSize > (API_SIZE-16) ) ret = API_SIZE-16; //API_SIZEはIPのため-6で良いと思ったが、ZBフォーマット移行の-9-1-6が上限)
tukudani 0:65f9be2858c9 1435 else ret = (byte)packetSize; // Ver 1.80で抜けていた(バグ)
tukudani 0:65f9be2858c9 1436 data[4] = 0x00; // UART受信を示す。
tukudani 0:65f9be2858c9 1437 data[5] = ret; // UART長を示す(data[]長では無い)。
tukudani 0:65f9be2858c9 1438 UdpXBeeU.read(&(data[6]), ret);
tukudani 0:65f9be2858c9 1439 ret += 6;
tukudani 0:65f9be2858c9 1440 }
tukudani 0:65f9be2858c9 1441 }
tukudani 0:65f9be2858c9 1442 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 1443 Serial.print("RX udp size=");
tukudani 0:65f9be2858c9 1444 Serial.print(ret);
tukudani 0:65f9be2858c9 1445 Serial.print(", ");
tukudani 0:65f9be2858c9 1446 for(i=0;i<ret;i++){
tukudani 0:65f9be2858c9 1447 Serial.print(data[i],HEX);
tukudani 0:65f9be2858c9 1448 Serial.print(" ");
tukudani 0:65f9be2858c9 1449 }
tukudani 0:65f9be2858c9 1450 Serial.println("");
tukudani 0:65f9be2858c9 1451 #endif
tukudani 0:65f9be2858c9 1452 return( ret );
tukudani 0:65f9be2858c9 1453 #endif
tukudani 0:65f9be2858c9 1454 #endif
tukudani 0:65f9be2858c9 1455 }
tukudani 0:65f9be2858c9 1456 #endif
tukudani 0:65f9be2858c9 1457
tukudani 0:65f9be2858c9 1458 /* シリアル送信バッファが空になるのを待つ */
tukudani 0:65f9be2858c9 1459 #ifndef LITE
tukudani 0:65f9be2858c9 1460 byte sci_write_check(void){
tukudani 0:65f9be2858c9 1461 #ifdef H3694
tukudani 0:65f9be2858c9 1462 byte timer;
tukudani 0:65f9be2858c9 1463 timer = timera() + 0x7F; // timeout = 500ms
tukudani 0:65f9be2858c9 1464 while( (timer != timera()) && ( SCI3_OUT_DATA_CHECK() < 1 ) );
tukudani 0:65f9be2858c9 1465 return( (byte)SCI3_OUT_DATA_CHECK() );
tukudani 0:65f9be2858c9 1466 #elif ARDUINO
tukudani 0:65f9be2858c9 1467 return( 1 ); // 関数があるかもしれないので、そのうち要調査&確認
tukudani 0:65f9be2858c9 1468 #else
tukudani 0:65f9be2858c9 1469 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1470 if( !_xbee_serial.writeable() ){
tukudani 0:65f9be2858c9 1471 wait(0.1);
tukudani 0:65f9be2858c9 1472 while( !_xbee_serial.writeable() ) sci_clear();
tukudani 0:65f9be2858c9 1473 }
tukudani 0:65f9be2858c9 1474 return( 1 );
tukudani 0:65f9be2858c9 1475 #else // PC
tukudani 0:65f9be2858c9 1476 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 1477 tcdrain( xbeeComFd );
tukudani 0:65f9be2858c9 1478 #endif
tukudani 0:65f9be2858c9 1479 return( 1 );
tukudani 0:65f9be2858c9 1480 #endif
tukudani 0:65f9be2858c9 1481 #endif
tukudani 0:65f9be2858c9 1482 }
tukudani 0:65f9be2858c9 1483 #endif // LITE
tukudani 0:65f9be2858c9 1484
tukudani 0:65f9be2858c9 1485 /* シリアル送信 */
tukudani 0:65f9be2858c9 1486 byte sci_write( char *data, byte len ){
tukudani 0:65f9be2858c9 1487 byte ret=1; // 戻り値 0でエラー
tukudani 0:65f9be2858c9 1488
tukudani 0:65f9be2858c9 1489 #ifdef H3694
tukudani 0:65f9be2858c9 1490 SCI3_OUT_STRINGB( data , (short)len ); // 戻り値なし
tukudani 0:65f9be2858c9 1491 #elif ARDUINO
tukudani 0:65f9be2858c9 1492 byte i;
tukudani 0:65f9be2858c9 1493 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 1494 UdpXBeeT.beginPacket(ADR_DEST, 0xBEE );
tukudani 0:65f9be2858c9 1495 #endif
tukudani 0:65f9be2858c9 1496 for(i=0;i<len;i++){
tukudani 0:65f9be2858c9 1497 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1498 #ifdef LEONARDO
tukudani 0:65f9be2858c9 1499 ret = Serial1.write( data[i] ); // 戻り値は書き込みバイト数
tukudani 0:65f9be2858c9 1500 #else
tukudani 0:65f9be2858c9 1501 ret = Serial.write( data[i] ); // 戻り値は書き込みバイト数
tukudani 0:65f9be2858c9 1502 #endif
tukudani 0:65f9be2858c9 1503 #else // XBEE_WIFI ARDUINO
tukudani 0:65f9be2858c9 1504 ret = UdpXBeeT.write( data[i] ); // 戻り値は書き込みバイト数
tukudani 0:65f9be2858c9 1505 /*
tukudani 0:65f9be2858c9 1506 Serial.print("TX udp size=");
tukudani 0:65f9be2858c9 1507 Serial.print(len,DEC);
tukudani 0:65f9be2858c9 1508 Serial.print(", ");
tukudani 0:65f9be2858c9 1509 for(i=0;i<len;i++){
tukudani 0:65f9be2858c9 1510 Serial.print(data[i],HEX);
tukudani 0:65f9be2858c9 1511 Serial.print(" ");
tukudani 0:65f9be2858c9 1512 }
tukudani 0:65f9be2858c9 1513 Serial.println("");
tukudani 0:65f9be2858c9 1514 */
tukudani 0:65f9be2858c9 1515 #endif
tukudani 0:65f9be2858c9 1516 if( ret == 0 ){
tukudani 0:65f9be2858c9 1517 i = len; // break;
tukudani 0:65f9be2858c9 1518 }
tukudani 0:65f9be2858c9 1519 }
tukudani 0:65f9be2858c9 1520 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 1521 UdpXBeeT.endPacket();
tukudani 0:65f9be2858c9 1522 #endif
tukudani 0:65f9be2858c9 1523 #else
tukudani 0:65f9be2858c9 1524 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1525 byte i;
tukudani 0:65f9be2858c9 1526 sci_write_check();
tukudani 0:65f9be2858c9 1527 for(i=0;i<len;i++){
tukudani 0:65f9be2858c9 1528 if( _xbee_serial.putc((int)data[i]) < 0 ) ret=0;
tukudani 0:65f9be2858c9 1529 }
tukudani 0:65f9be2858c9 1530 if(ret) ret=len;
tukudani 0:65f9be2858c9 1531 #else // PC
tukudani 0:65f9be2858c9 1532 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1533 byte i;
tukudani 0:65f9be2858c9 1534 for(i=0;i<len;i++){
tukudani 0:65f9be2858c9 1535 if(write(xbeeComFd,&data[i],1) != 1){
tukudani 0:65f9be2858c9 1536 fprintf(stderr,"sci_write ERR:d[%02d]=0x%02x\n",i,(byte)data[i]);
tukudani 0:65f9be2858c9 1537 ret = 0;
tukudani 0:65f9be2858c9 1538 }
tukudani 0:65f9be2858c9 1539 }
tukudani 0:65f9be2858c9 1540 #ifdef DEBUG_TX
tukudani 0:65f9be2858c9 1541 printf("Transmitted Packet tx[%3d] to ", len);
tukudani 0:65f9be2858c9 1542 for(i=0;i<4;i++) printf("%02X",ADR_DEST[i]);
tukudani 0:65f9be2858c9 1543 printf(" ");
tukudani 0:65f9be2858c9 1544 for(i=4;i<8;i++) printf("%02X",ADR_DEST[i]);
tukudani 0:65f9be2858c9 1545 printf("\n");
tukudani 0:65f9be2858c9 1546 for(i=0;i<len;i++) printf("%3d,",i);
tukudani 0:65f9be2858c9 1547 printf("\n");
tukudani 0:65f9be2858c9 1548 for(i=0;i<len;i++) printf(" %02X,",(byte)data[i]); // dataはchar型
tukudani 0:65f9be2858c9 1549 printf("\n");
tukudani 0:65f9be2858c9 1550 for(i=0;i<len;i++) if( (byte)data[i]>=0x20 ) printf("'%c',",data[i]); else printf("%3d,",(int)((byte)data[i]));
tukudani 0:65f9be2858c9 1551 printf("\n");
tukudani 0:65f9be2858c9 1552 #endif
tukudani 0:65f9be2858c9 1553 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 1554 #ifdef DEBUG_TX
tukudani 0:65f9be2858c9 1555 int i;
tukudani 0:65f9be2858c9 1556 #endif
tukudani 0:65f9be2858c9 1557 int ret_i; // 戻り値 0でエラー
tukudani 0:65f9be2858c9 1558 ret_i = sendto(xbeeTSFd, data, len, 0, (struct sockaddr *)&xbeeT_addr, sizeof(xbeeT_addr));
tukudani 0:65f9be2858c9 1559 if( ret_i<0 ) ret=0; else if( ret_i>255 ) ret = 0xFF; else ret = (byte)ret_i;
tukudani 0:65f9be2858c9 1560 #ifdef DEBUG_TX
tukudani 0:65f9be2858c9 1561 printf("Transmitted Packet tx[%3d] to ", len);
tukudani 0:65f9be2858c9 1562 for(i=0;i<4;i++) printf("%d.",ADR_DEST[i]);
tukudani 0:65f9be2858c9 1563 printf(":0xBEE\n");
tukudani 0:65f9be2858c9 1564 for(i=0;i<len;i++) printf("%3d,",i);
tukudani 0:65f9be2858c9 1565 printf("\n");
tukudani 0:65f9be2858c9 1566 for(i=0;i<len;i++) printf(" %02X,",(byte)data[i]); // dataはchar型
tukudani 0:65f9be2858c9 1567 printf("\n");
tukudani 0:65f9be2858c9 1568 for(i=0;i<len;i++) if( isgraph((byte)data[i]) ) printf("'%c',",data[i]); else printf("%3d,",(int)((byte)data[i]));
tukudani 0:65f9be2858c9 1569 printf("\n");
tukudani 0:65f9be2858c9 1570 #endif
tukudani 0:65f9be2858c9 1571 #endif
tukudani 0:65f9be2858c9 1572 #endif // ARM/PC
tukudani 0:65f9be2858c9 1573 #endif
tukudani 0:65f9be2858c9 1574 #ifdef DEBUG
tukudani 0:65f9be2858c9 1575 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1576 _xbee_debug.printf("DEBUG:TX OUT DONE(%d)\r\n",ret);
tukudani 0:65f9be2858c9 1577 #else
tukudani 0:65f9be2858c9 1578 printf("DEBUG:TX OUT DONE(%d)\n",ret);
tukudani 0:65f9be2858c9 1579 #endif
tukudani 0:65f9be2858c9 1580 #endif
tukudani 0:65f9be2858c9 1581 return( ret );
tukudani 0:65f9be2858c9 1582 }
tukudani 0:65f9be2858c9 1583
tukudani 0:65f9be2858c9 1584 /* string byte操作 */
tukudani 0:65f9be2858c9 1585 void strcopy(char *s1, const char *s2){ // string.hのstrcpyの真似版
tukudani 0:65f9be2858c9 1586 while( *s2 != 0x00 ) *s1++ = *s2++;
tukudani 0:65f9be2858c9 1587 *s1 = 0x00;
tukudani 0:65f9be2858c9 1588 }
tukudani 0:65f9be2858c9 1589 void bytecpy(byte *s1, const byte *s2, byte size){ // strcpyのバイト長可変版
tukudani 0:65f9be2858c9 1590 byte i;
tukudani 0:65f9be2858c9 1591 for(i=0; i< size ;i++ ){
tukudani 0:65f9be2858c9 1592 s1[i] = s2[i];
tukudani 0:65f9be2858c9 1593 }
tukudani 0:65f9be2858c9 1594 }
tukudani 0:65f9be2858c9 1595 byte bytecmp(byte *s1, const byte *s2, byte size){ // strcmpの簡易版(大小比較なし)
tukudani 0:65f9be2858c9 1596 /*バイトデータの比較
tukudani 0:65f9be2858c9 1597 入力:byte *s1 = 比較データ1
tukudani 0:65f9be2858c9 1598 入力:byte *s2 = 比較データ2
tukudani 0:65f9be2858c9 1599 入力:byte size = 比較データ長
tukudani 0:65f9be2858c9 1600 出力:戻り値 = 0:一致 1:不一致
tukudani 0:65f9be2858c9 1601 */
tukudani 0:65f9be2858c9 1602 byte i=0; // 同一なら0を応答
tukudani 0:65f9be2858c9 1603 for(i=0; i< size ;i++ ){
tukudani 0:65f9be2858c9 1604 if( s1[i] != s2[i] ) return(1);
tukudani 0:65f9be2858c9 1605 }
tukudani 0:65f9be2858c9 1606 return(0);
tukudani 0:65f9be2858c9 1607 }
tukudani 0:65f9be2858c9 1608
tukudani 0:65f9be2858c9 1609 /* LCD用関数 for ARDUINO */ // H8版とPC版は別ファイル lcd_h8.c lcd_pc.c
tukudani 0:65f9be2858c9 1610 #ifdef ARDUINO
tukudani 0:65f9be2858c9 1611 #ifdef LCD_H
tukudani 0:65f9be2858c9 1612 void lcd_cls(void){
tukudani 0:65f9be2858c9 1613 xbee_ardlcd.clear();
tukudani 0:65f9be2858c9 1614 }
tukudani 0:65f9be2858c9 1615 void lcd_home(void){
tukudani 0:65f9be2858c9 1616 xbee_ardlcd.home();
tukudani 0:65f9be2858c9 1617 }
tukudani 0:65f9be2858c9 1618 void lcd_control(byte disonoff, byte curonoff, byte curblink){
tukudani 0:65f9be2858c9 1619 }
tukudani 0:65f9be2858c9 1620 void lcd_goto(const byte mesto){
tukudani 0:65f9be2858c9 1621 byte row=0;
tukudani 0:65f9be2858c9 1622
tukudani 0:65f9be2858c9 1623 switch( mesto ){
tukudani 0:65f9be2858c9 1624 case LCD_ROW_1: row=0; break;
tukudani 0:65f9be2858c9 1625 case LCD_ROW_2: row=1; break;
tukudani 0:65f9be2858c9 1626 case LCD_ROW_3: row=2; break;
tukudani 0:65f9be2858c9 1627 case LCD_ROW_4: row=3; break;
tukudani 0:65f9be2858c9 1628 }
tukudani 0:65f9be2858c9 1629 xbee_ardlcd.setCursor(0,(int)row);
tukudani 0:65f9be2858c9 1630 }
tukudani 0:65f9be2858c9 1631 void lcd_shift(const byte data){
tukudani 0:65f9be2858c9 1632 xbee_ardlcd.write(' ');
tukudani 0:65f9be2858c9 1633 }
tukudani 0:65f9be2858c9 1634 void lcd_putch(const char data){
tukudani 0:65f9be2858c9 1635 xbee_ardlcd.write( data );
tukudani 0:65f9be2858c9 1636 }
tukudani 0:65f9be2858c9 1637 void lcd_putstr(const char *data){
tukudani 0:65f9be2858c9 1638 while(*data != 0) {
tukudani 0:65f9be2858c9 1639 xbee_ardlcd.write(*data);
tukudani 0:65f9be2858c9 1640 data++;
tukudani 0:65f9be2858c9 1641 }
tukudani 0:65f9be2858c9 1642 }
tukudani 0:65f9be2858c9 1643 void lcd_disp_bin(const byte x){
tukudani 0:65f9be2858c9 1644 // xbee_ardlcd.print( x, BIN );
tukudani 0:65f9be2858c9 1645 byte i;
tukudani 0:65f9be2858c9 1646 for (i=128;i>0;i>>=1){
tukudani 0:65f9be2858c9 1647 if ((x&i)==0){
tukudani 0:65f9be2858c9 1648 lcd_putch('0');
tukudani 0:65f9be2858c9 1649 }else{
tukudani 0:65f9be2858c9 1650 lcd_putch('1');
tukudani 0:65f9be2858c9 1651 }
tukudani 0:65f9be2858c9 1652 }
tukudani 0:65f9be2858c9 1653 }
tukudani 0:65f9be2858c9 1654 void lcd_disp_hex(const byte i){
tukudani 0:65f9be2858c9 1655 // xbee_ardlcd.print( i, HEX );
tukudani 0:65f9be2858c9 1656 byte hi,lo;
tukudani 0:65f9be2858c9 1657 hi=i&0xF0; // High nibble
tukudani 0:65f9be2858c9 1658 hi=hi>>4;
tukudani 0:65f9be2858c9 1659 hi=hi+'0';
tukudani 0:65f9be2858c9 1660 if (hi>'9'){
tukudani 0:65f9be2858c9 1661 hi=hi+7;
tukudani 0:65f9be2858c9 1662 }
tukudani 0:65f9be2858c9 1663 lo=(i&0x0F)+'0'; // Low nibble
tukudani 0:65f9be2858c9 1664 if (lo>'9'){
tukudani 0:65f9be2858c9 1665 lo=lo+7;
tukudani 0:65f9be2858c9 1666 }
tukudani 0:65f9be2858c9 1667 lcd_putch((char)hi);
tukudani 0:65f9be2858c9 1668 lcd_putch((char)lo);
tukudani 0:65f9be2858c9 1669 }
tukudani 0:65f9be2858c9 1670 void lcd_disp_1(const unsigned int x){
tukudani 0:65f9be2858c9 1671 if (x<10){
tukudani 0:65f9be2858c9 1672 xbee_ardlcd.write((char)(x+0x30));
tukudani 0:65f9be2858c9 1673 }else if (x<16){
tukudani 0:65f9be2858c9 1674 xbee_ardlcd.write((char)(x-10+'A'));
tukudani 0:65f9be2858c9 1675 }else{
tukudani 0:65f9be2858c9 1676 xbee_ardlcd.write('X');
tukudani 0:65f9be2858c9 1677 }
tukudani 0:65f9be2858c9 1678 }
tukudani 0:65f9be2858c9 1679 void lcd_disp_2(unsigned int x){
tukudani 0:65f9be2858c9 1680 unsigned int y;
tukudani 0:65f9be2858c9 1681 if (x<100){
tukudani 0:65f9be2858c9 1682 y=x/10;
tukudani 0:65f9be2858c9 1683 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1684 x-=(y*10);
tukudani 0:65f9be2858c9 1685 xbee_ardlcd.write((char)(x+0x30));
tukudani 0:65f9be2858c9 1686 }else{
tukudani 0:65f9be2858c9 1687 xbee_ardlcd.print("XX");
tukudani 0:65f9be2858c9 1688 }
tukudani 0:65f9be2858c9 1689 }
tukudani 0:65f9be2858c9 1690 void lcd_disp_3(unsigned int x){
tukudani 0:65f9be2858c9 1691 unsigned int y;
tukudani 0:65f9be2858c9 1692 if (x<1000){
tukudani 0:65f9be2858c9 1693 y=x/100;
tukudani 0:65f9be2858c9 1694 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1695 x-=(y*100);
tukudani 0:65f9be2858c9 1696 y=x/10;
tukudani 0:65f9be2858c9 1697 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1698 x-=(y*10);
tukudani 0:65f9be2858c9 1699 xbee_ardlcd.write((char)(x+0x30));
tukudani 0:65f9be2858c9 1700 }else{
tukudani 0:65f9be2858c9 1701 xbee_ardlcd.print("XXX");
tukudani 0:65f9be2858c9 1702 }
tukudani 0:65f9be2858c9 1703 }
tukudani 0:65f9be2858c9 1704 void lcd_disp_5(unsigned int x){
tukudani 0:65f9be2858c9 1705 unsigned int y;
tukudani 0:65f9be2858c9 1706 if (x<=65535){
tukudani 0:65f9be2858c9 1707 y=x/10000;
tukudani 0:65f9be2858c9 1708 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1709 x-=(y*10000);
tukudani 0:65f9be2858c9 1710 y=x/1000;
tukudani 0:65f9be2858c9 1711 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1712 x-=(y*1000);
tukudani 0:65f9be2858c9 1713 y=x/100;
tukudani 0:65f9be2858c9 1714 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1715 x-=(y*100);
tukudani 0:65f9be2858c9 1716 y=x/10;
tukudani 0:65f9be2858c9 1717 xbee_ardlcd.write((char)(y+0x30));
tukudani 0:65f9be2858c9 1718 x-=(y*10);
tukudani 0:65f9be2858c9 1719 xbee_ardlcd.write((char)(x+0x30));
tukudani 0:65f9be2858c9 1720 }else{
tukudani 0:65f9be2858c9 1721 xbee_ardlcd.print("XXXXX");
tukudani 0:65f9be2858c9 1722 }
tukudani 0:65f9be2858c9 1723 }
tukudani 0:65f9be2858c9 1724 void lcd_init(void){
tukudani 0:65f9be2858c9 1725 xbee_ardlcd.begin(20, 4); // 液晶の桁数×行数の設定
tukudani 0:65f9be2858c9 1726 xbee_ardlcd.clear(); // 表示クリア
tukudani 0:65f9be2858c9 1727 }
tukudani 0:65f9be2858c9 1728 #endif
tukudani 0:65f9be2858c9 1729 #endif
tukudani 0:65f9be2858c9 1730
tukudani 0:65f9be2858c9 1731 /* LCD用関数 for ARM_MBED */ // 注意=液晶では無くシリアルに出力する
tukudani 0:65f9be2858c9 1732 #ifdef ARM_MBED // D8(PA_9)からログをUART(38400baud)出力します
tukudani 0:65f9be2858c9 1733 #ifdef DEBUG
tukudani 0:65f9be2858c9 1734 void lcd_cls(void){
tukudani 0:65f9be2858c9 1735 _xbee_debug.printf("----------------\r\n");
tukudani 0:65f9be2858c9 1736 }
tukudani 0:65f9be2858c9 1737 void lcd_home(void){
tukudani 0:65f9be2858c9 1738 _xbee_debug.printf("\r\n");
tukudani 0:65f9be2858c9 1739 }
tukudani 0:65f9be2858c9 1740 void lcd_control(byte disonoff, byte curonoff, byte curblink){
tukudani 0:65f9be2858c9 1741 }
tukudani 0:65f9be2858c9 1742 void lcd_goto(const byte mesto){
tukudani 0:65f9be2858c9 1743 lcd_home();
tukudani 0:65f9be2858c9 1744 }
tukudani 0:65f9be2858c9 1745 void lcd_shift(const byte data){
tukudani 0:65f9be2858c9 1746 _xbee_debug.putc(' ');
tukudani 0:65f9be2858c9 1747 }
tukudani 0:65f9be2858c9 1748 void lcd_putch(const char data){
tukudani 0:65f9be2858c9 1749 _xbee_debug.putc( data );
tukudani 0:65f9be2858c9 1750 }
tukudani 0:65f9be2858c9 1751 void lcd_putstr(const char *data){
tukudani 0:65f9be2858c9 1752 while(*data != 0) {
tukudani 0:65f9be2858c9 1753 _xbee_debug.putc(*data);
tukudani 0:65f9be2858c9 1754 data++;
tukudani 0:65f9be2858c9 1755 }
tukudani 0:65f9be2858c9 1756 }
tukudani 0:65f9be2858c9 1757 void lcd_disp_bin(const byte x){
tukudani 0:65f9be2858c9 1758 byte i;
tukudani 0:65f9be2858c9 1759 for (i=128;i>0;i>>=1){
tukudani 0:65f9be2858c9 1760 if ((x&i)==0){
tukudani 0:65f9be2858c9 1761 lcd_putch('0');
tukudani 0:65f9be2858c9 1762 }else{
tukudani 0:65f9be2858c9 1763 lcd_putch('1');
tukudani 0:65f9be2858c9 1764 }
tukudani 0:65f9be2858c9 1765 }
tukudani 0:65f9be2858c9 1766 }
tukudani 0:65f9be2858c9 1767 void lcd_disp_hex(const byte i){
tukudani 0:65f9be2858c9 1768 byte hi,lo;
tukudani 0:65f9be2858c9 1769 hi=i&0xF0; // High nibble
tukudani 0:65f9be2858c9 1770 hi=hi>>4;
tukudani 0:65f9be2858c9 1771 hi=hi+'0';
tukudani 0:65f9be2858c9 1772 if (hi>'9'){
tukudani 0:65f9be2858c9 1773 hi=hi+7;
tukudani 0:65f9be2858c9 1774 }
tukudani 0:65f9be2858c9 1775 lo=(i&0x0F)+'0'; // Low nibble
tukudani 0:65f9be2858c9 1776 if (lo>'9'){
tukudani 0:65f9be2858c9 1777 lo=lo+7;
tukudani 0:65f9be2858c9 1778 }
tukudani 0:65f9be2858c9 1779 lcd_putch((char)hi);
tukudani 0:65f9be2858c9 1780 lcd_putch((char)lo);
tukudani 0:65f9be2858c9 1781 }
tukudani 0:65f9be2858c9 1782 void lcd_disp_1(const unsigned int x){
tukudani 0:65f9be2858c9 1783 if (x<10){
tukudani 0:65f9be2858c9 1784 _xbee_debug.putc((char)(x+0x30));
tukudani 0:65f9be2858c9 1785 }else if (x<16){
tukudani 0:65f9be2858c9 1786 _xbee_debug.putc((char)(x-10+'A'));
tukudani 0:65f9be2858c9 1787 }else{
tukudani 0:65f9be2858c9 1788 _xbee_debug.putc('X');
tukudani 0:65f9be2858c9 1789 }
tukudani 0:65f9be2858c9 1790 }
tukudani 0:65f9be2858c9 1791 void lcd_disp_2(unsigned int x){
tukudani 0:65f9be2858c9 1792 unsigned int y;
tukudani 0:65f9be2858c9 1793 if (x<100){
tukudani 0:65f9be2858c9 1794 y=x/10;
tukudani 0:65f9be2858c9 1795 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1796 x-=(y*10);
tukudani 0:65f9be2858c9 1797 _xbee_debug.putc((char)(x+0x30));
tukudani 0:65f9be2858c9 1798 }else{
tukudani 0:65f9be2858c9 1799 _xbee_debug.printf("XX");
tukudani 0:65f9be2858c9 1800 }
tukudani 0:65f9be2858c9 1801 }
tukudani 0:65f9be2858c9 1802 void lcd_disp_3(unsigned int x){
tukudani 0:65f9be2858c9 1803 unsigned int y;
tukudani 0:65f9be2858c9 1804 if (x<1000){
tukudani 0:65f9be2858c9 1805 y=x/100;
tukudani 0:65f9be2858c9 1806 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1807 x-=(y*100);
tukudani 0:65f9be2858c9 1808 y=x/10;
tukudani 0:65f9be2858c9 1809 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1810 x-=(y*10);
tukudani 0:65f9be2858c9 1811 _xbee_debug.putc((char)(x+0x30));
tukudani 0:65f9be2858c9 1812 }else{
tukudani 0:65f9be2858c9 1813 _xbee_debug.printf("XXX");
tukudani 0:65f9be2858c9 1814 }
tukudani 0:65f9be2858c9 1815 }
tukudani 0:65f9be2858c9 1816 void lcd_disp_5(unsigned int x){
tukudani 0:65f9be2858c9 1817 unsigned int y;
tukudani 0:65f9be2858c9 1818 if (x<=65535){
tukudani 0:65f9be2858c9 1819 y=x/10000;
tukudani 0:65f9be2858c9 1820 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1821 x-=(y*10000);
tukudani 0:65f9be2858c9 1822 y=x/1000;
tukudani 0:65f9be2858c9 1823 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1824 x-=(y*1000);
tukudani 0:65f9be2858c9 1825 y=x/100;
tukudani 0:65f9be2858c9 1826 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1827 x-=(y*100);
tukudani 0:65f9be2858c9 1828 y=x/10;
tukudani 0:65f9be2858c9 1829 _xbee_debug.putc((char)(y+0x30));
tukudani 0:65f9be2858c9 1830 x-=(y*10);
tukudani 0:65f9be2858c9 1831 _xbee_debug.putc((char)(x+0x30));
tukudani 0:65f9be2858c9 1832 }else{
tukudani 0:65f9be2858c9 1833 _xbee_debug.printf("XXXXX");
tukudani 0:65f9be2858c9 1834 }
tukudani 0:65f9be2858c9 1835 }
tukudani 0:65f9be2858c9 1836 void lcd_init(void){
tukudani 0:65f9be2858c9 1837 _xbee_debug.printf("-----\r\n");
tukudani 0:65f9be2858c9 1838 }
tukudani 0:65f9be2858c9 1839 #endif
tukudani 0:65f9be2858c9 1840 #endif
tukudani 0:65f9be2858c9 1841
tukudani 0:65f9be2858c9 1842 #ifdef LCD_H
tukudani 0:65f9be2858c9 1843 void lcd_disp(const char *s){
tukudani 0:65f9be2858c9 1844 lcd_cls();
tukudani 0:65f9be2858c9 1845 lcd_goto(LCD_ROW_1);
tukudani 0:65f9be2858c9 1846 lcd_putstr( s );
tukudani 0:65f9be2858c9 1847 #ifndef H3694
tukudani 0:65f9be2858c9 1848 #ifndef ARDUINO
tukudani 0:65f9be2858c9 1849 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 1850 _xbee_debug.printf("\r\n");
tukudani 0:65f9be2858c9 1851 #else // PC
tukudani 0:65f9be2858c9 1852 lcd_putch( '\n' );
tukudani 0:65f9be2858c9 1853 #endif
tukudani 0:65f9be2858c9 1854 #endif
tukudani 0:65f9be2858c9 1855 #endif
tukudani 0:65f9be2858c9 1856 }
tukudani 0:65f9be2858c9 1857 #endif
tukudani 0:65f9be2858c9 1858
tukudani 0:65f9be2858c9 1859 /*********************************************************************
tukudani 0:65f9be2858c9 1860 XBeeドライバ関数
tukudani 0:65f9be2858c9 1861 *********************************************************************/
tukudani 0:65f9be2858c9 1862
tukudani 0:65f9be2858c9 1863 /*
tukudani 0:65f9be2858c9 1864 00 01 02 03 04 05 06 07 08....
tukudani 0:65f9be2858c9 1865 ATNJ = 7E 00 05 08 01 4E 4A FF 5F
tukudani 0:65f9be2858c9 1866 | | len |mo|FI| NJ=0xFF|CS|
tukudani 0:65f9be2858c9 1867 | | |de| | |__|___ Check Sum
tukudani 0:65f9be2858c9 1868 | | | | |________|______ AT コマンド
tukudani 0:65f9be2858c9 1869 | | | |__|_______________ Frame ID
tukudani 0:65f9be2858c9 1870 | | |__|__________________ AT=0x08 Remote=0x17
tukudani 0:65f9be2858c9 1871 | |_____|_____________________ Data Lengrh (ATコマンド+2文字)
tukudani 0:65f9be2858c9 1872 |__|___________________________ 固定値
tukudani 0:65f9be2858c9 1873
tukudani 0:65f9be2858c9 1874 char *at "AT" ATコマンド len=4バイト~最大255 (フレーム8バイト~)
tukudani 0:65f9be2858c9 1875 "RAT" リモートATコマンド len=15バイト~最大255 (フレーム19バイト~)
tukudani 0:65f9be2858c9 1876 "TX" データ送信コマンド len=15バイト~最大255 (フレーム19バイト~)
tukudani 0:65f9be2858c9 1877 ※API_SIZE=32の時valueは12バイトまで
tukudani 0:65f9be2858c9 1878 */
tukudani 0:65f9be2858c9 1879
tukudani 0:65f9be2858c9 1880 /* (ドライバ)ATコマンド送信 */
tukudani 0:65f9be2858c9 1881 byte xbee_at_tx(const char *at, const byte *value, const byte value_len){
tukudani 0:65f9be2858c9 1882 /*
tukudani 0:65f9be2858c9 1883 処理:リモートATコマンドの送信を行うXBeeドライバ部
tukudani 0:65f9be2858c9 1884 入力:ATコマンドat[] = "AT**"はローカルATで、"RAT***"がリモートAT
tukudani 0:65f9be2858c9 1885             TXがデータ送信モード
tukudani 0:65f9be2858c9 1886    データvalue[] = 各ATコマンドで引き渡すデータ値
tukudani 0:65f9be2858c9 1887    value_len   = その長さ
tukudani 0:65f9be2858c9 1888 戻り値:送信したAPIサービス長。送信しなかった場合は0
tukudani 0:65f9be2858c9 1889 */
tukudani 0:65f9be2858c9 1890 char data_api[API_TXSIZE];
tukudani 0:65f9be2858c9 1891 byte i;
tukudani 0:65f9be2858c9 1892 byte len; // APIサービス長
tukudani 0:65f9be2858c9 1893 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1894 byte check=0xFF; // チェックサム
tukudani 0:65f9be2858c9 1895 byte data_position=5; // 送信データdata_api[]の何処にvalue[]を入れるか
tukudani 0:65f9be2858c9 1896 #endif
tukudani 0:65f9be2858c9 1897 byte ret=0;
tukudani 0:65f9be2858c9 1898
tukudani 0:65f9be2858c9 1899 if( PACKET_ID == 0xFF ){
tukudani 0:65f9be2858c9 1900 PACKET_ID=0x01;
tukudani 0:65f9be2858c9 1901 }else{
tukudani 0:65f9be2858c9 1902 PACKET_ID++;
tukudani 0:65f9be2858c9 1903 }
tukudani 0:65f9be2858c9 1904 len=0;
tukudani 0:65f9be2858c9 1905 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 1906 switch( at[0] ){
tukudani 0:65f9be2858c9 1907 case 'A':
tukudani 0:65f9be2858c9 1908 case 'F':
tukudani 0:65f9be2858c9 1909 if( at[1]=='T'){
tukudani 0:65f9be2858c9 1910 data_api[3]=(char)0x08; // ATコマンドモード
tukudani 0:65f9be2858c9 1911 if( at[0] == 'A') data_api[4]=(char)PACKET_ID; // フレームID (at="ATxx")
tukudani 0:65f9be2858c9 1912 else data_api[4]=0x00; // IDなし(at="FTxx")
tukudani 0:65f9be2858c9 1913 data_api[5]=(char)at[2]; // ATコマンド上位
tukudani 0:65f9be2858c9 1914 data_api[6]=(char)at[3]; // ATコマンド下位
tukudani 0:65f9be2858c9 1915 data_position=7;
tukudani 0:65f9be2858c9 1916 len=4; // サービスデータにMD+FI+AT(2)バイトが入る
tukudani 0:65f9be2858c9 1917 }
tukudani 0:65f9be2858c9 1918 break;
tukudani 0:65f9be2858c9 1919 case 'R':
tukudani 0:65f9be2858c9 1920 case 'S':
tukudani 0:65f9be2858c9 1921 if( at[1]=='A' && at[2]=='T'){
tukudani 0:65f9be2858c9 1922 data_api[3]=(char)0x17; // リモートATコマンドモード
tukudani 0:65f9be2858c9 1923 if( at[0] == 'R') data_api[4]=(char)PACKET_ID; // フレームID (at="RATxx")
tukudani 0:65f9be2858c9 1924 else data_api[4]=0x00; // IDなし(at="SATxx")
tukudani 0:65f9be2858c9 1925 for( i=5 ; i<=12 ; i++) data_api[i]=(char)ADR_DEST[i-5];
tukudani 0:65f9be2858c9 1926 for( i=13 ; i<=14 ; i++) data_api[i]=(char)SADR_DEST[i-13];
tukudani 0:65f9be2858c9 1927 data_api[15]=(char)0x02; // apply changes
tukudani 0:65f9be2858c9 1928 data_api[16]=(char)at[3]; // ATコマンド上位
tukudani 0:65f9be2858c9 1929 data_api[17]=(char)at[4]; // ATコマンド下位
tukudani 0:65f9be2858c9 1930 data_position=18; // value[]を入れる場所
tukudani 0:65f9be2858c9 1931 len=15; // サービスデータにMD+FI+ADR(8)+SAD(2)+OPT+AT(2)が入る
tukudani 0:65f9be2858c9 1932 }
tukudani 0:65f9be2858c9 1933 break;
tukudani 0:65f9be2858c9 1934 case 'T':
tukudani 0:65f9be2858c9 1935 if( at[1]=='X'){
tukudani 0:65f9be2858c9 1936 data_api[3]=(char)0x10; // TXデータ送信モード
tukudani 0:65f9be2858c9 1937 data_api[4]=(char)0x00; // フレームIDを使用しない(no responce)
tukudani 0:65f9be2858c9 1938 for( i=5 ; i<=12 ; i++) data_api[i]=(char)ADR_DEST[i-5];
tukudani 0:65f9be2858c9 1939 for( i=13 ; i<=14 ; i++) data_api[i]=(char)SADR_DEST[i-13];
tukudani 0:65f9be2858c9 1940 data_api[15]=(char)0x00; // ZigBeeホップ数
tukudani 0:65f9be2858c9 1941 data_api[16]=(char)0x00; // 暗号化=しない
tukudani 0:65f9be2858c9 1942 data_position=17;
tukudani 0:65f9be2858c9 1943 len=14; // サービスデータにMD+FI+ADR(8)+SAD(2)+OPT(2)が入る
tukudani 0:65f9be2858c9 1944 }
tukudani 0:65f9be2858c9 1945 break;
tukudani 0:65f9be2858c9 1946 #ifndef ARDUINO
tukudani 0:65f9be2858c9 1947 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 1948 case 'Z':
tukudani 0:65f9be2858c9 1949 // [0]送信元EndPoint, [1]宛先EndPoint, [2-3]クラスタID, [4-5]プロファイルID
tukudani 0:65f9be2858c9 1950 // [6]Radius=00 [7]Option=00 [8]Fram Ctrl [9]SeqNum [10]Command [11-12] Attribute
tukudani 0:65f9be2858c9 1951 if( at[1]=='C' && value_len>12){
tukudani 0:65f9be2858c9 1952 data_api[3]=(char)0x11; // ZCL送信モード
tukudani 0:65f9be2858c9 1953 data_api[4]=(char)PACKET_ID; // フレームID
tukudani 0:65f9be2858c9 1954 for( i=5 ; i<=12 ; i++) data_api[i]=(char)ADR_DEST[i-5];
tukudani 0:65f9be2858c9 1955 for( i=13 ; i<=14 ; i++) data_api[i]=(char)SADR_DEST[i-13];
tukudani 0:65f9be2858c9 1956 data_position=15;
tukudani 0:65f9be2858c9 1957 len=12;
tukudani 0:65f9be2858c9 1958 }
tukudani 0:65f9be2858c9 1959 break;
tukudani 0:65f9be2858c9 1960 #endif
tukudani 0:65f9be2858c9 1961 #endif
tukudani 0:65f9be2858c9 1962 default:
tukudani 0:65f9be2858c9 1963 break;
tukudani 0:65f9be2858c9 1964 }
tukudani 0:65f9be2858c9 1965 if( len ){
tukudani 0:65f9be2858c9 1966 data_api[0]=(char)0x7E; // デリミタ
tukudani 0:65f9be2858c9 1967 data_api[1]=(char)0x00; // パケット長の上位(送らない前程)
tukudani 0:65f9be2858c9 1968 for( i=3 ; i < data_position ; i++) check -= (byte)data_api[i];
tukudani 0:65f9be2858c9 1969 if( value_len > 0 ){
tukudani 0:65f9be2858c9 1970 for( i=0 ; i<value_len; i++){
tukudani 0:65f9be2858c9 1971 data_api[data_position + i] = (char)value[i];
tukudani 0:65f9be2858c9 1972 check -= (byte)data_api[data_position + i];
tukudani 0:65f9be2858c9 1973 len++;
tukudani 0:65f9be2858c9 1974 }
tukudani 0:65f9be2858c9 1975 }
tukudani 0:65f9be2858c9 1976 data_api[2] =(char)len;
tukudani 0:65f9be2858c9 1977 data_api[len+3]=(char)check;
tukudani 0:65f9be2858c9 1978 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 1979 sci_write( data_api, (byte)(len+4) );
tukudani 0:65f9be2858c9 1980 ret=len+3;
tukudani 0:65f9be2858c9 1981 #else
tukudani 0:65f9be2858c9 1982 check = sci_write_check(); // 以降 checkはシリアルバッファ確認に使用する
tukudani 0:65f9be2858c9 1983 /*シリアルデータ送信 */
tukudani 0:65f9be2858c9 1984 if( check > 0 ){
tukudani 0:65f9be2858c9 1985 if( sci_write( data_api, (byte)(len+4) ) == 0 ){
tukudani 0:65f9be2858c9 1986 /* シリアルリセット */
tukudani 0:65f9be2858c9 1987 #ifndef H3694
tukudani 0:65f9be2858c9 1988 #ifndef ARDUINO
tukudani 0:65f9be2858c9 1989 #ifndef ARM_MBED // PC
tukudani 0:65f9be2858c9 1990 wait_millisec( 100 );
tukudani 0:65f9be2858c9 1991 close_serial_port(); // シリアルを閉じる
tukudani 0:65f9be2858c9 1992 wait_millisec( 300 );
tukudani 0:65f9be2858c9 1993 printf("RESET serial\n");
tukudani 0:65f9be2858c9 1994 while( sci_init( xbee_com_port )==0){ // 再度オープン
tukudani 0:65f9be2858c9 1995 wait_millisec( 3000 );
tukudani 0:65f9be2858c9 1996 close_serial_port(); // シリアルを閉じる
tukudani 0:65f9be2858c9 1997 wait_millisec( 3000 );
tukudani 0:65f9be2858c9 1998 printf("RESET serial\n");
tukudani 0:65f9be2858c9 1999 }
tukudani 0:65f9be2858c9 2000 wait_millisec( 300 );
tukudani 0:65f9be2858c9 2001 sci_write( data_api, (byte)(len+4) ); // 再送信
tukudani 0:65f9be2858c9 2002 #endif
tukudani 0:65f9be2858c9 2003 #endif
tukudani 0:65f9be2858c9 2004 #endif
tukudani 0:65f9be2858c9 2005 }
tukudani 0:65f9be2858c9 2006 ret=len+3;
tukudani 0:65f9be2858c9 2007 }else{
tukudani 0:65f9be2858c9 2008 #ifdef H3694
tukudani 0:65f9be2858c9 2009 led_red( 1 );
tukudani 0:65f9be2858c9 2010 #endif
tukudani 0:65f9be2858c9 2011 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2012 strcopy( ERR_LOG , "ERR:TX Buffer Over" );
tukudani 0:65f9be2858c9 2013 ERR_CODE = check;
tukudani 0:65f9be2858c9 2014 #endif
tukudani 0:65f9be2858c9 2015 ret=0;
tukudani 0:65f9be2858c9 2016 }
tukudani 0:65f9be2858c9 2017 #endif
tukudani 0:65f9be2858c9 2018 }
tukudani 0:65f9be2858c9 2019 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 2020 switch( at[0] ){
tukudani 0:65f9be2858c9 2021 case 'A':
tukudani 0:65f9be2858c9 2022 case 'F':
tukudani 0:65f9be2858c9 2023 if( at[1]=='T' ){
tukudani 0:65f9be2858c9 2024 data_api[0]=(char)0x02; // ATコマンドモード
tukudani 0:65f9be2858c9 2025 data_api[1]=(char)0x00; // 固定
tukudani 0:65f9be2858c9 2026 if( at[0] == 'A' ) data_api[2]=(char)PACKET_ID; // フレームID
tukudani 0:65f9be2858c9 2027 data_api[3]=(char)0x02;
tukudani 0:65f9be2858c9 2028 data_api[4]=(char)at[2]; // ATコマンド上位
tukudani 0:65f9be2858c9 2029 data_api[5]=(char)at[3]; // ATコマンド下位
tukudani 0:65f9be2858c9 2030 len=6;
tukudani 0:65f9be2858c9 2031 }
tukudani 0:65f9be2858c9 2032 break;
tukudani 0:65f9be2858c9 2033 case 'R':
tukudani 0:65f9be2858c9 2034 case 'S':
tukudani 0:65f9be2858c9 2035 if( at[1]=='A' && at[2]=='T' ){
tukudani 0:65f9be2858c9 2036 data_api[0]=(char)0x02; // ATコマンドモード
tukudani 0:65f9be2858c9 2037 data_api[1]=(char)0x00; // 固定
tukudani 0:65f9be2858c9 2038 if( at[0] == 'R'){
tukudani 0:65f9be2858c9 2039 data_api[2]=(char)PACKET_ID; // フレームID
tukudani 0:65f9be2858c9 2040 data_api[3]=(char)0x02;
tukudani 0:65f9be2858c9 2041 }else{
tukudani 0:65f9be2858c9 2042 data_api[2]=(char)0x00;
tukudani 0:65f9be2858c9 2043 data_api[3]=(char)0x00;
tukudani 0:65f9be2858c9 2044 }
tukudani 0:65f9be2858c9 2045 data_api[4]=(char)at[3]; // ATコマンド上位
tukudani 0:65f9be2858c9 2046 data_api[5]=(char)at[4]; // ATコマンド下位
tukudani 0:65f9be2858c9 2047 len=6;
tukudani 0:65f9be2858c9 2048 }
tukudani 0:65f9be2858c9 2049 case 'T':
tukudani 0:65f9be2858c9 2050 if( at[1]=='X'){
tukudani 0:65f9be2858c9 2051 data_api[0]=(char)0x00; // シリアルデータモード
tukudani 0:65f9be2858c9 2052 data_api[1]=(char)0x00; // ackが必要なときは01
tukudani 0:65f9be2858c9 2053 len=2; // datasheetでは3だが誤り。実装は2
tukudani 0:65f9be2858c9 2054 }
tukudani 0:65f9be2858c9 2055 break;
tukudani 0:65f9be2858c9 2056 default:
tukudani 0:65f9be2858c9 2057 break;
tukudani 0:65f9be2858c9 2058 }
tukudani 0:65f9be2858c9 2059 if( len ){
tukudani 0:65f9be2858c9 2060 if( value_len > 0 ){
tukudani 0:65f9be2858c9 2061 for( i=0 ; i<value_len; i++){
tukudani 0:65f9be2858c9 2062 data_api[len] = (char)value[i];
tukudani 0:65f9be2858c9 2063 len++;
tukudani 0:65f9be2858c9 2064 }
tukudani 0:65f9be2858c9 2065 }
tukudani 0:65f9be2858c9 2066 if(DEVICE_TYPE == XB_TYPE_WIFI20){
tukudani 0:65f9be2858c9 2067 #ifdef DEBUG
tukudani 0:65f9be2858c9 2068 printf("DEBUG:TX_10(%3d):",len);
tukudani 0:65f9be2858c9 2069 for(i=0;i<len;i++) printf("%02X,",data_api[i]);
tukudani 0:65f9be2858c9 2070 printf("\n");
tukudani 0:65f9be2858c9 2071 #endif
tukudani 0:65f9be2858c9 2072 len+=6;
tukudani 0:65f9be2858c9 2073 if( len <= API_TXSIZE ){
tukudani 0:65f9be2858c9 2074 for(i=len-6; i>0; i--){
tukudani 0:65f9be2858c9 2075 data_api[i+5]=data_api[i-1];
tukudani 0:65f9be2858c9 2076 }
tukudani 0:65f9be2858c9 2077 data_api[0]=0x42;
tukudani 0:65f9be2858c9 2078 data_api[1]=0x42;
tukudani 0:65f9be2858c9 2079 data_api[2]=0x0;
tukudani 0:65f9be2858c9 2080 data_api[3]=0x0;
tukudani 0:65f9be2858c9 2081 data_api[4]=0x0;
tukudani 0:65f9be2858c9 2082 data_api[5]=0x0;
tukudani 0:65f9be2858c9 2083 }
tukudani 0:65f9be2858c9 2084 #ifdef DEBUG
tukudani 0:65f9be2858c9 2085 printf("DEBUG:TX_20(%3d):",len);
tukudani 0:65f9be2858c9 2086 for(i=0;i<len;i++) printf("%02X,",data_api[i]);
tukudani 0:65f9be2858c9 2087 printf("\n");
tukudani 0:65f9be2858c9 2088 #endif
tukudani 0:65f9be2858c9 2089 }
tukudani 0:65f9be2858c9 2090 if( sci_write_check() > 0 ){ // XBEE_WIFIなのでifdef LITEは不要
tukudani 0:65f9be2858c9 2091 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 2092 Serial.print("TX udp size=");
tukudani 0:65f9be2858c9 2093 Serial.print(len);
tukudani 0:65f9be2858c9 2094 Serial.print(", ");
tukudani 0:65f9be2858c9 2095 for(i=0;i<len;i++){
tukudani 0:65f9be2858c9 2096 Serial.print(data_api[i],HEX);
tukudani 0:65f9be2858c9 2097 Serial.print(" ");
tukudani 0:65f9be2858c9 2098 }
tukudani 0:65f9be2858c9 2099 Serial.println("");
tukudani 0:65f9be2858c9 2100 #endif
tukudani 0:65f9be2858c9 2101 #ifdef DEBUG
tukudani 0:65f9be2858c9 2102 printf("TX udp size=%d\n",len);
tukudani 0:65f9be2858c9 2103 #endif
tukudani 0:65f9be2858c9 2104 if( sci_write( data_api, len ) == 0 ){
tukudani 0:65f9be2858c9 2105 #ifdef H3694
tukudani 0:65f9be2858c9 2106 led_red( 1 );
tukudani 0:65f9be2858c9 2107 #endif
tukudani 0:65f9be2858c9 2108 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2109 strcopy( ERR_LOG , "ERR:TX Write Error" );
tukudani 0:65f9be2858c9 2110 ERR_CODE = 0x00;
tukudani 0:65f9be2858c9 2111 #endif
tukudani 0:65f9be2858c9 2112 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 2113 Serial.println("ERR:TX Write Error" );
tukudani 0:65f9be2858c9 2114 #endif
tukudani 0:65f9be2858c9 2115 #ifdef DEBUG
tukudani 0:65f9be2858c9 2116 printf("ERR:TX Write Error\n");
tukudani 0:65f9be2858c9 2117 #endif
tukudani 0:65f9be2858c9 2118 ret=0;
tukudani 0:65f9be2858c9 2119 }else ret = len;
tukudani 0:65f9be2858c9 2120 }else{
tukudani 0:65f9be2858c9 2121 #ifdef H3694
tukudani 0:65f9be2858c9 2122 led_red( 1 );
tukudani 0:65f9be2858c9 2123 #endif
tukudani 0:65f9be2858c9 2124 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2125 strcopy( ERR_LOG , "ERR:TX Buffer Over" );
tukudani 0:65f9be2858c9 2126 ERR_CODE = 0x00;
tukudani 0:65f9be2858c9 2127 #endif
tukudani 0:65f9be2858c9 2128 #ifdef DEBUG
tukudani 0:65f9be2858c9 2129 printf("ERR:TX Buffer Over\n");
tukudani 0:65f9be2858c9 2130 #endif
tukudani 0:65f9be2858c9 2131 ret=0;
tukudani 0:65f9be2858c9 2132 }
tukudani 0:65f9be2858c9 2133 }
tukudani 0:65f9be2858c9 2134 #endif
tukudani 0:65f9be2858c9 2135 return( ret );
tukudani 0:65f9be2858c9 2136 }
tukudani 0:65f9be2858c9 2137
tukudani 0:65f9be2858c9 2138 /* (ドライバ)ATコマンド受信 */
tukudani 0:65f9be2858c9 2139 byte xbee_at_rx(byte *data){
tukudani 0:65f9be2858c9 2140 /*
tukudani 0:65f9be2858c9 2141 リモートATコマンドの応答を受信するXBeeドライバ部
tukudani 0:65f9be2858c9 2142 処理:XBeeからの受信データをdata[]へ代入(応答)する
tukudani 0:65f9be2858c9 2143 入出力:APIデータdata[] 前3バイトは'7E'+len(2) data[0]が0x00の場合はタイムアウト
tukudani 0:65f9be2858c9 2144 リターン:APIサービス長、0はタイムアウト
tukudani 0:65f9be2858c9 2145 */
tukudani 0:65f9be2858c9 2146 byte i;
tukudani 0:65f9be2858c9 2147 byte len; // APIサービス長-3 (APIフレームの前3バイトとチェックサム1バイトを除く)
tukudani 0:65f9be2858c9 2148 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 2149 unsigned int leni; // 実際のAPIサービス長と使用するAPIサービス長の差(データ破棄用)
tukudani 0:65f9be2858c9 2150 byte check = 0xFF; // チェックサム
tukudani 0:65f9be2858c9 2151 #endif
tukudani 0:65f9be2858c9 2152 byte ret=0;
tukudani 0:65f9be2858c9 2153
tukudani 0:65f9be2858c9 2154 /* 受信処理 */
tukudani 0:65f9be2858c9 2155 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 2156 data[0] = sci_read( 1 ); // 1ms待ち受けで受信
tukudani 0:65f9be2858c9 2157 if( data[0] == 0x7E ) { // 期待デリミタ0x7E時
tukudani 0:65f9be2858c9 2158 for( i=1;i<=2;i++ ){
tukudani 0:65f9be2858c9 2159 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2160 data[i] = sci_read( 35 ); // 1割だけ緩和しました。sci_read 32⇒35(Wataru)
tukudani 0:65f9be2858c9 2161 #else
tukudani 0:65f9be2858c9 2162 data[i] = sci_read( 50 );
tukudani 0:65f9be2858c9 2163 #endif
tukudani 0:65f9be2858c9 2164 }
tukudani 0:65f9be2858c9 2165 if(data[1] == 0x00) len = data[2];
tukudani 0:65f9be2858c9 2166 else len = 0xFF - 4; // API長が255バイトまでの制約(本来は64KB)
tukudani 0:65f9be2858c9 2167 if( len > (API_SIZE-4) ) len = API_SIZE-4;
tukudani 0:65f9be2858c9 2168 leni = (unsigned int)data[1] * 256 + (unsigned int)data[2] - (unsigned int)len;
tukudani 0:65f9be2858c9 2169 // 通常は0。lenが本来の容量よりも少ない場合に不足分が代入されれる
tukudani 0:65f9be2858c9 2170 for( i=0 ; i <= len ; i++){ // i = lenはチェックサムを入力する
tukudani 0:65f9be2858c9 2171 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2172 data[i+3] = sci_read( 35 ); // 1割だけ緩和しました。sci_read 32⇒35(Wataru)
tukudani 0:65f9be2858c9 2173 #else
tukudani 0:65f9be2858c9 2174 data[i+3] = sci_read( 50 );
tukudani 0:65f9be2858c9 2175 #endif
tukudani 0:65f9be2858c9 2176 if( i != len) check -= data[i+3]; // チェックサムのカウント
tukudani 0:65f9be2858c9 2177 }
tukudani 0:65f9be2858c9 2178 while( leni > 0 ){
tukudani 0:65f9be2858c9 2179 data[len+3] = sci_read( 50 ); // データの空読み(lenは固定)
tukudani 0:65f9be2858c9 2180 if( leni != 1 ) {
tukudani 0:65f9be2858c9 2181 check -= data[len+3]; // leni=0の時はCheck sumなので減算しない
tukudani 0:65f9be2858c9 2182 }
tukudani 0:65f9be2858c9 2183 leni--;
tukudani 0:65f9be2858c9 2184 }
tukudani 0:65f9be2858c9 2185 if( check == data[len+3] ) ret = len +3;
tukudani 0:65f9be2858c9 2186 else ret = 0;
tukudani 0:65f9be2858c9 2187 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 2188 lcd_cls(); lcd_goto(LCD_ROW_1);
tukudani 0:65f9be2858c9 2189 lcd_putstr("->");
tukudani 0:65f9be2858c9 2190 for( i=0 ; i <= len+3 ; i++){
tukudani 0:65f9be2858c9 2191 lcd_disp_hex( data[i] );
tukudani 0:65f9be2858c9 2192 if( i == 8 ) lcd_goto(LCD_ROW_2);
tukudani 0:65f9be2858c9 2193 if( i == 18 ) lcd_goto(LCD_ROW_3);
tukudani 0:65f9be2858c9 2194 if( i == 28 ) lcd_goto(LCD_ROW_4);
tukudani 0:65f9be2858c9 2195 if( i == 38 ) lcd_goto(LCD_ROW_1);
tukudani 0:65f9be2858c9 2196 }
tukudani 0:65f9be2858c9 2197 lcd_goto(LCD_ROW_4);
tukudani 0:65f9be2858c9 2198 lcd_putstr("ID=");
tukudani 0:65f9be2858c9 2199 lcd_disp_hex( PACKET_ID );
tukudani 0:65f9be2858c9 2200 lcd_putstr(":");
tukudani 0:65f9be2858c9 2201 lcd_disp_hex( data[4] );
tukudani 0:65f9be2858c9 2202 if( check != data[len+3]){
tukudani 0:65f9be2858c9 2203 lcd_putstr(" ER=");
tukudani 0:65f9be2858c9 2204 }else{
tukudani 0:65f9be2858c9 2205 lcd_putstr(" OK=");
tukudani 0:65f9be2858c9 2206 }
tukudani 0:65f9be2858c9 2207 lcd_disp_hex( check );
tukudani 0:65f9be2858c9 2208 lcd_putstr(":");
tukudani 0:65f9be2858c9 2209 lcd_disp_hex( data[len+3] );
tukudani 0:65f9be2858c9 2210 #endif
tukudani 0:65f9be2858c9 2211 }
tukudani 0:65f9be2858c9 2212 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 2213 ret = sci_read_frame( data );
tukudani 0:65f9be2858c9 2214
tukudani 0:65f9be2858c9 2215 if( ret >= 7){ // もともと10バイト以上にしていたけどUARTは最低7バイトからなので変更
tukudani 0:65f9be2858c9 2216 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 2217 if(DEVICE_TYPE == XB_TYPE_WIFI20 && data[4] != 0x00){ // UARTのときは処理不要
tukudani 0:65f9be2858c9 2218 #ifdef DEBUG
tukudani 0:65f9be2858c9 2219 printf("DEBUG:RX_10(%3d):",ret);
tukudani 0:65f9be2858c9 2220 for(i=0;i<ret;i++) printf("%02X,",data[i]);
tukudani 0:65f9be2858c9 2221 printf("\n");
tukudani 0:65f9be2858c9 2222 #endif
tukudani 0:65f9be2858c9 2223 if( ret > 10 ){
tukudani 0:65f9be2858c9 2224 for(i=10; i<ret; i++)data[i-6]=data[i];
tukudani 0:65f9be2858c9 2225 ret -=6;
tukudani 0:65f9be2858c9 2226 }
tukudani 0:65f9be2858c9 2227 #ifdef DEBUG
tukudani 0:65f9be2858c9 2228 printf("DEBUG:RX_20(%3d):",ret);
tukudani 0:65f9be2858c9 2229 for(i=0;i<ret;i++) printf("%02X,",data[i]);
tukudani 0:65f9be2858c9 2230 printf("\n");
tukudani 0:65f9be2858c9 2231 #endif
tukudani 0:65f9be2858c9 2232 }
tukudani 0:65f9be2858c9 2233 #endif
tukudani 0:65f9be2858c9 2234 switch( data[4] ){
tukudani 0:65f9be2858c9 2235 case 0x82: // Remote AT Resp. アドレス4バイト+基本レスポンス6バイト+オプション0バイト以上
tukudani 0:65f9be2858c9 2236 if(ret >= 10){
tukudani 0:65f9be2858c9 2237 for(i=ret;i>=10; i--){
tukudani 0:65f9be2858c9 2238 data[i+8] = data[i]; // 破壊 18以降全て ※メモリリークするのでUdpXBeeR受信で制限している
tukudani 0:65f9be2858c9 2239 }
tukudani 0:65f9be2858c9 2240 data[17] = data[9]; // AT Command status
tukudani 0:65f9be2858c9 2241 data[16] = data[8]; // AT Command LSB
tukudani 0:65f9be2858c9 2242 data[15] = data[7]; // AT Command MSB
tukudani 0:65f9be2858c9 2243 data[14] = 0xFE;
tukudani 0:65f9be2858c9 2244 data[13] = 0xFF;
tukudani 0:65f9be2858c9 2245 data[4]=data[6]; // Frame ID 破壊 4
tukudani 0:65f9be2858c9 2246 for(i=0;i<4;i++){ // IPアドレス
tukudani 0:65f9be2858c9 2247 data[5+i]=data[i]; // 破壊 5,6,7,8
tukudani 0:65f9be2858c9 2248 data[9+i]=0x00; // 破壊 9,10,11,12
tukudani 0:65f9be2858c9 2249 }
tukudani 0:65f9be2858c9 2250 data[3]=0x97; // rat res
tukudani 0:65f9be2858c9 2251 data[0]=0x7E; // 受信成功
tukudani 0:65f9be2858c9 2252 ret += 9;
tukudani 0:65f9be2858c9 2253 data[1]=0x00; // length上位
tukudani 0:65f9be2858c9 2254 data[2]=ret-3; // length下位
tukudani 0:65f9be2858c9 2255 }
tukudani 0:65f9be2858c9 2256 break;
tukudani 0:65f9be2858c9 2257 case 0x04: // IO RX Sample アドレス4バイト+基本レスポンス6バイト+オプション0バイト以上
tukudani 0:65f9be2858c9 2258 if(ret >= 10){
tukudani 0:65f9be2858c9 2259 for(i=ret;i>=6; i--){ // datasheetの誤り Number SamplesのOffsetはdatesheetでは3だが実際は2
tukudani 0:65f9be2858c9 2260 // これにIPの4バイトが追加されるので6
tukudani 0:65f9be2858c9 2261 data[i+9] = data[i]; // 破壊 15以降全て ※メモリリークするのでUdpXBeeR受信で制限している
tukudani 0:65f9be2858c9 2262 }
tukudani 0:65f9be2858c9 2263 data[14] = 0x02; // Packet was a broadcast
tukudani 0:65f9be2858c9 2264 data[13] = 0xFE;
tukudani 0:65f9be2858c9 2265 data[12] = 0xFF;
tukudani 0:65f9be2858c9 2266 for(i=0;i<4;i++){ // IPアドレス
tukudani 0:65f9be2858c9 2267 data[4+i]=data[i]; // 破壊 5,6,7,8
tukudani 0:65f9be2858c9 2268 data[8+i]=0x00; // 破壊 9,10,11,12
tukudani 0:65f9be2858c9 2269 }
tukudani 0:65f9be2858c9 2270 data[3]=0x92; // io sample
tukudani 0:65f9be2858c9 2271 data[0]=0x7E; // 受信成功
tukudani 0:65f9be2858c9 2272 ret += 10;
tukudani 0:65f9be2858c9 2273 data[1]=0x00; // length上位
tukudani 0:65f9be2858c9 2274 data[2]=ret-3; // length下位
tukudani 0:65f9be2858c9 2275 }
tukudani 0:65f9be2858c9 2276 break;
tukudani 0:65f9be2858c9 2277 case 0x00: // UART
tukudani 0:65f9be2858c9 2278 len = data[5]; // 破壊されるのでUART長はバックアップ
tukudani 0:65f9be2858c9 2279 for(i=ret;i>=6; i--){
tukudani 0:65f9be2858c9 2280 data[i+9] = data[i]; // 破壊 15以降全て ※メモリリークするのでUdpXBeeU受信で制限している
tukudani 0:65f9be2858c9 2281 }
tukudani 0:65f9be2858c9 2282 data[14] = 0x02; // Packet was a broadcast
tukudani 0:65f9be2858c9 2283 data[13] = 0xFE;
tukudani 0:65f9be2858c9 2284 data[12] = 0xFF;
tukudani 0:65f9be2858c9 2285 for(i=0;i<4;i++){ // IPアドレス
tukudani 0:65f9be2858c9 2286 data[4+i]=data[i]; // 破壊 5,6,7,8
tukudani 0:65f9be2858c9 2287 data[8+i]=0x00; // 破壊 9,10,11,12
tukudani 0:65f9be2858c9 2288 }
tukudani 0:65f9be2858c9 2289 data[1]=0x00; // 243バイトまでしか考慮しない
tukudani 0:65f9be2858c9 2290 data[2]=len + 0x0C; // 243バイトまでしか考慮しない
tukudani 0:65f9be2858c9 2291 data[3]=0x90; // UART
tukudani 0:65f9be2858c9 2292 data[0]=0x7E; // 受信成功
tukudani 0:65f9be2858c9 2293 ret += 10;
tukudani 0:65f9be2858c9 2294 break;
tukudani 0:65f9be2858c9 2295 default:
tukudani 0:65f9be2858c9 2296 for(i=ret;i>=7; i--){
tukudani 0:65f9be2858c9 2297 data[i+8] = data[i]; // 破壊 18以降全て ※メモリリークするのでUdpXBeeR受信で制限している
tukudani 0:65f9be2858c9 2298 }
tukudani 0:65f9be2858c9 2299 data[4]=data[6]; // Frame ID 破壊 4
tukudani 0:65f9be2858c9 2300 for(i=0;i<4;i++){ // IPアドレス
tukudani 0:65f9be2858c9 2301 data[5+i]=data[i]; // 破壊 5,6,7,8
tukudani 0:65f9be2858c9 2302 data[9+i]=0x00; // 破壊 9,10,11,12
tukudani 0:65f9be2858c9 2303 }
tukudani 0:65f9be2858c9 2304 data[3]=data[4];
tukudani 0:65f9be2858c9 2305 ret=0;
tukudani 0:65f9be2858c9 2306 }
tukudani 0:65f9be2858c9 2307 #ifdef DEBUG
tukudani 0:65f9be2858c9 2308 printf("DEBUG:RX_ZB(%3d):",ret);
tukudani 0:65f9be2858c9 2309 for(i=0;i<ret;i++) printf("%02X,",data[i]);
tukudani 0:65f9be2858c9 2310 printf("\n");
tukudani 0:65f9be2858c9 2311 #endif
tukudani 0:65f9be2858c9 2312 }else ret=0;
tukudani 0:65f9be2858c9 2313 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 2314 Serial.print("RX api size=");
tukudani 0:65f9be2858c9 2315 Serial.print(ret);
tukudani 0:65f9be2858c9 2316 Serial.print(", ");
tukudani 0:65f9be2858c9 2317 for(i=0;i<ret;i++){
tukudani 0:65f9be2858c9 2318 Serial.print(data[i],HEX);
tukudani 0:65f9be2858c9 2319 Serial.print(" ");
tukudani 0:65f9be2858c9 2320 }
tukudani 0:65f9be2858c9 2321 Serial.println("");
tukudani 0:65f9be2858c9 2322 #endif
tukudani 0:65f9be2858c9 2323 #endif
tukudani 0:65f9be2858c9 2324 return( ret );
tukudani 0:65f9be2858c9 2325 }
tukudani 0:65f9be2858c9 2326
tukudani 0:65f9be2858c9 2327 /* (ドライバ)パケット差出人の抽出 */
tukudani 0:65f9be2858c9 2328 byte xbee_from_acum( const byte *data ){
tukudani 0:65f9be2858c9 2329 /*
tukudani 0:65f9be2858c9 2330 処理:受信データの差出人をグローバル変数へ登録 ※xbee_at_rxでは登録しない
tukudani 0:65f9be2858c9 2331 入力:data[] APIデータ
tukudani 0:65f9be2858c9 2332 出力:byte 受信データの種別mode値を戻り値に代入
tukudani 0:65f9be2858c9 2333    受信データの差出人をグローバル変数ADR_FROMに代入
tukudani 0:65f9be2858c9 2334 再起動で8Aのモード応答あり
tukudani 0:65f9be2858c9 2335 */
tukudani 0:65f9be2858c9 2336 byte i;
tukudani 0:65f9be2858c9 2337 byte ret=0;
tukudani 0:65f9be2858c9 2338
tukudani 0:65f9be2858c9 2339 if( data[3]==MODE_UART // 0x90 UART Receive
tukudani 0:65f9be2858c9 2340 || data[3]==MODE_UAR2 // 0x91 UART AO=0
tukudani 0:65f9be2858c9 2341 || data[3]==MODE_GPIN // 0x92 GPI data
tukudani 0:65f9be2858c9 2342 || data[3]==MODE_SENS // 0x94 XB Sensor
tukudani 0:65f9be2858c9 2343 || data[3]==MODE_IDNT){ // 0x95 Node Identify
tukudani 0:65f9be2858c9 2344 for(i=0;i<8;i++) ADR_FROM[i]=data[4+i];
tukudani 0:65f9be2858c9 2345 ret = data[3]; // mode値
tukudani 0:65f9be2858c9 2346 }else if(
tukudani 0:65f9be2858c9 2347 data[3]==MODE_RESP){ // 0x97 リモートATコマンドの結果
tukudani 0:65f9be2858c9 2348 for(i=0;i<8;i++) ADR_FROM[i]=data[5+i];
tukudani 0:65f9be2858c9 2349 ret = data[3]; // mode値
tukudani 0:65f9be2858c9 2350 }else if(
tukudani 0:65f9be2858c9 2351 data[3]==MODE_RES // 0x88 ローカルATコマンドの結果
tukudani 0:65f9be2858c9 2352 || data[3]==MODE_MODM){ // 0x8A Modem Statusを受信
tukudani 0:65f9be2858c9 2353 ret = data[3]; // mode値
tukudani 0:65f9be2858c9 2354 }else{
tukudani 0:65f9be2858c9 2355 #ifdef H3694
tukudani 0:65f9be2858c9 2356 led_red( 1 );
tukudani 0:65f9be2858c9 2357 #endif
tukudani 0:65f9be2858c9 2358 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2359 strcopy( ERR_LOG , "ERR:xbee_from" );
tukudani 0:65f9be2858c9 2360 ERR_CODE = data[3];
tukudani 0:65f9be2858c9 2361 #endif
tukudani 0:65f9be2858c9 2362 ret = 0;
tukudani 0:65f9be2858c9 2363 }
tukudani 0:65f9be2858c9 2364 return( ret );
tukudani 0:65f9be2858c9 2365 }
tukudani 0:65f9be2858c9 2366
tukudani 0:65f9be2858c9 2367 /* (ドライバ)GPIO入力データの計算部 */
tukudani 0:65f9be2858c9 2368 byte xbee_gpi_acum( byte *data ){
tukudani 0:65f9be2858c9 2369 /* リモート先(ADR_FROMに入力)のGPIOの入力値(下位バイト)を応答する。
tukudani 0:65f9be2858c9 2370 リモート先は(こちらから呼ばなくても)GPIOの変化などで自動送信する設定にしておく必要がある。
tukudani 0:65f9be2858c9 2371 具体的にはリモート先をATIC設定するなど。本演算関数はxbee_rx_callから呼ばれる。
tukudani 0:65f9be2858c9 2372 port: port指定 IO名 ピン番号 USB評価ボード(XBIB-U-Dev)
tukudani 0:65f9be2858c9 2373 port= 1 DIO1 XBee_pin 19 (AD1) SW2
tukudani 0:65f9be2858c9 2374 port= 2 DIO2 XBee_pin 18 (AD2) SW3
tukudani 0:65f9be2858c9 2375 port= 3 DIO3 XBee_pin 17 (AD3) SW4
tukudani 0:65f9be2858c9 2376 out: port 1~7の入力値をバイトで応答。
tukudani 0:65f9be2858c9 2377 上位バイトのポートには対応しない
tukudani 0:65f9be2858c9 2378 DIOのport1~7がオフでかつADCが有効の場合は有効ADC(1~3の最も若い番号)のAD変換結果を応答する。
tukudani 0:65f9be2858c9 2379 ADC結果は8ビットに間引かれる。電圧は0~1.2Vに対応する。以下でADC電圧を得ることが可能。
tukudani 0:65f9be2858c9 2380 (unsigned int)xbee_gpi_acum(data)/255*1200 [mV]
tukudani 0:65f9be2858c9 2381 注意1:DIO/ADCの切り換え方法:DIOのport1~7が一つでも有効であればDIOとして動作します。
tukudani 0:65f9be2858c9 2382 port1~7のDIOが全て無効でADCが有効であれば有効なADC(1~3の最も若い番号)が動作します。
tukudani 0:65f9be2858c9 2383 port1~7のDIOとADC1~3が全て無効で電源電圧検出ADCが有効であれば電圧を応答します。(要確認)
tukudani 0:65f9be2858c9 2384 未対応:電源電圧ADCの範囲と単位を要確認。必要に応じて値を変換する必要がある。
tukudani 0:65f9be2858c9 2385 mode = MODE_GPIN(0x92) GPI data
tukudani 0:65f9be2858c9 2386
tukudani 0:65f9be2858c9 2387 data[17]:mask(下位)
tukudani 0:65f9be2858c9 2388 */
tukudani 0:65f9be2858c9 2389 byte ret=0xFF;
tukudani 0:65f9be2858c9 2390
tukudani 0:65f9be2858c9 2391 if( data[3] == MODE_GPIN ){ // data[3]:MODE_GPIN(92)
tukudani 0:65f9be2858c9 2392 if( xbee_from_acum( data ) > 0 ){ // 差出人をグローバル変数へ
tukudani 0:65f9be2858c9 2393 if( (data[17]&0xFE) != 0x00 ){ // DIO(port 1~7)がある時
tukudani 0:65f9be2858c9 2394 ret = (data[17] & data[20]); // 20:DIOデータ(下位バイト) 17:マスク
tukudani 0:65f9be2858c9 2395 }else if( (data[18]&0x8E) != 0 ){ // ADCがあるとき
tukudani 0:65f9be2858c9 2396 if( data[16] == 0x00 && data[17] == 0x00 ){ // DIOが全てマスクされているとき
tukudani 0:65f9be2858c9 2397 ret = (data[19]<6) | (data[20]>2); // 19:ADCデータ(上位)、20:下位
tukudani 0:65f9be2858c9 2398 }else{ //
tukudani 0:65f9be2858c9 2399 ret = (data[21]<6) | (data[22]>2); // 21:ADCデータ(上位)、22:下位
tukudani 0:65f9be2858c9 2400 }
tukudani 0:65f9be2858c9 2401 }
tukudani 0:65f9be2858c9 2402 }else{
tukudani 0:65f9be2858c9 2403 #ifdef H3694
tukudani 0:65f9be2858c9 2404 led_red( 1 );
tukudani 0:65f9be2858c9 2405 #endif
tukudani 0:65f9be2858c9 2406 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2407 strcopy( ERR_LOG , "ERR:xbee_gpi acum" );
tukudani 0:65f9be2858c9 2408 ERR_CODE = xbee_from_acum( data );
tukudani 0:65f9be2858c9 2409 #endif
tukudani 0:65f9be2858c9 2410 }
tukudani 0:65f9be2858c9 2411 }else{
tukudani 0:65f9be2858c9 2412 #ifdef H3694
tukudani 0:65f9be2858c9 2413 led_red( 1 );
tukudani 0:65f9be2858c9 2414 #endif
tukudani 0:65f9be2858c9 2415 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2416 strcopy( ERR_LOG , "ERR:xbee_gpi mode" );
tukudani 0:65f9be2858c9 2417 ERR_CODE = data[3];
tukudani 0:65f9be2858c9 2418 #endif
tukudani 0:65f9be2858c9 2419 }
tukudani 0:65f9be2858c9 2420 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 2421 lcd_goto(LCD_ROW_2);
tukudani 0:65f9be2858c9 2422 lcd_putstr(" M:"); // MASK表示
tukudani 0:65f9be2858c9 2423 lcd_disp_hex( data[16] );
tukudani 0:65f9be2858c9 2424 lcd_disp_hex( data[17] );
tukudani 0:65f9be2858c9 2425 lcd_putstr(" D:"); // デジタルサンプル値
tukudani 0:65f9be2858c9 2426 lcd_disp_hex( data[19] );
tukudani 0:65f9be2858c9 2427 lcd_disp_hex( data[20] );
tukudani 0:65f9be2858c9 2428 #endif
tukudani 0:65f9be2858c9 2429 return(ret);
tukudani 0:65f9be2858c9 2430 }
tukudani 0:65f9be2858c9 2431
tukudani 0:65f9be2858c9 2432 /* (ドライバ)UART入力データの計算部 */
tukudani 0:65f9be2858c9 2433 byte xbee_uart_acum( byte *data ){
tukudani 0:65f9be2858c9 2434 /*
tukudani 0:65f9be2858c9 2435 mode =MODE_UART(0x90) UART Receive
tukudani 0:65f9be2858c9 2436 mode =MODE_UAR2(0x91) UART AO=0
tukudani 0:65f9be2858c9 2437 */
tukudani 0:65f9be2858c9 2438 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 2439 byte i; // DEGUG用
tukudani 0:65f9be2858c9 2440 byte len;
tukudani 0:65f9be2858c9 2441 #endif
tukudani 0:65f9be2858c9 2442 byte ret=0;
tukudani 0:65f9be2858c9 2443
tukudani 0:65f9be2858c9 2444 if( data[3] == 0x90 ){ // data[3]:データ種別=ZigBee Recieve Packet
tukudani 0:65f9be2858c9 2445 if( xbee_from_acum( data ) > 0 ){
tukudani 0:65f9be2858c9 2446 ret = data[15]; // 15:データ1文字 ※Digi仕様書P100記載誤り
tukudani 0:65f9be2858c9 2447 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 2448 len = data[2]-0x0C; // 12バイト減算
tukudani 0:65f9be2858c9 2449 #endif
tukudani 0:65f9be2858c9 2450 }else{
tukudani 0:65f9be2858c9 2451 #ifdef H3694
tukudani 0:65f9be2858c9 2452 led_red( 1 );
tukudani 0:65f9be2858c9 2453 #endif
tukudani 0:65f9be2858c9 2454 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2455 strcopy( ERR_LOG , "ERR:xbee_uart from" );
tukudani 0:65f9be2858c9 2456 ERR_CODE = xbee_from_acum( data );
tukudani 0:65f9be2858c9 2457 #endif
tukudani 0:65f9be2858c9 2458 }
tukudani 0:65f9be2858c9 2459 }else if( data[3] == 0x91 ){ // data[3]:データ種別=ZigBee Explict Rx Indicator
tukudani 0:65f9be2858c9 2460 if( xbee_from_acum( data ) > 0 ){
tukudani 0:65f9be2858c9 2461 ret = data[21]; // 14:データ1文字
tukudani 0:65f9be2858c9 2462 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 2463 len = data[2]-0x12; // 18バイト減算
tukudani 0:65f9be2858c9 2464 #endif
tukudani 0:65f9be2858c9 2465 }else{
tukudani 0:65f9be2858c9 2466 #ifdef H3694
tukudani 0:65f9be2858c9 2467 led_red( 1 );
tukudani 0:65f9be2858c9 2468 #endif
tukudani 0:65f9be2858c9 2469 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2470 strcopy( ERR_LOG , "ERR:xbee_uart2 from" );
tukudani 0:65f9be2858c9 2471 ERR_CODE = xbee_from_acum( data );
tukudani 0:65f9be2858c9 2472 #endif
tukudani 0:65f9be2858c9 2473 }
tukudani 0:65f9be2858c9 2474 }else{
tukudani 0:65f9be2858c9 2475 #ifdef H3694
tukudani 0:65f9be2858c9 2476 led_red( 1 );
tukudani 0:65f9be2858c9 2477 #endif
tukudani 0:65f9be2858c9 2478 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2479 strcopy( ERR_LOG , "ERR:xbee_uart mode" );
tukudani 0:65f9be2858c9 2480 ERR_CODE = data[3];
tukudani 0:65f9be2858c9 2481 #endif
tukudani 0:65f9be2858c9 2482 }
tukudani 0:65f9be2858c9 2483 #ifdef DEBUG_RX
tukudani 0:65f9be2858c9 2484 lcd_goto(LCD_ROW_3);
tukudani 0:65f9be2858c9 2485 lcd_putstr("[");
tukudani 0:65f9be2858c9 2486 if( data[3]==0x90 && len>1 ) for(i=0; i< len ; i++) lcd_putch( data[15+i] );
tukudani 0:65f9be2858c9 2487 else if( data[3]==0x91 && len>1 ) for(i=0; i< len ; i++) lcd_putch( data[21+i] );
tukudani 0:65f9be2858c9 2488 else lcd_putch( ret );
tukudani 0:65f9be2858c9 2489 lcd_putstr( "](0x" );
tukudani 0:65f9be2858c9 2490 lcd_disp_hex( ret );
tukudani 0:65f9be2858c9 2491 lcd_putstr( ") l=" );
tukudani 0:65f9be2858c9 2492 lcd_disp_hex( len );
tukudani 0:65f9be2858c9 2493 if( data[3]==0x91 ){
tukudani 0:65f9be2858c9 2494 lcd_putstr(" CI:");
tukudani 0:65f9be2858c9 2495 lcd_disp_hex( data[16] );
tukudani 0:65f9be2858c9 2496 lcd_disp_hex( data[17] );
tukudani 0:65f9be2858c9 2497 lcd_putstr(".");
tukudani 0:65f9be2858c9 2498 lcd_disp_hex( data[18] );
tukudani 0:65f9be2858c9 2499 lcd_disp_hex( data[19] );
tukudani 0:65f9be2858c9 2500 }
tukudani 0:65f9be2858c9 2501 #endif
tukudani 0:65f9be2858c9 2502 return(ret);
tukudani 0:65f9be2858c9 2503 }
tukudani 0:65f9be2858c9 2504
tukudani 0:65f9be2858c9 2505 /* (ドライバ)ATコマンドの送信+受信 */
tukudani 0:65f9be2858c9 2506 byte xbee_tx_rx(const char *at, byte *data, byte len){
tukudani 0:65f9be2858c9 2507 /*
tukudani 0:65f9be2858c9 2508 コマンド送信と受信がセットになったAPI(受信コマンドの簡単なチェックも実施)
tukudani 0:65f9be2858c9 2509 at: ATコマンド(文字列)
tukudani 0:65f9be2858c9 2510 data[API_SIZE]: 入力データ/出力データ(共用)
tukudani 0:65f9be2858c9 2511 ※本コマンドの処理中に受信した他のパケット(送信IDとATコマンド名で判定)は破棄する。
tukudani 0:65f9be2858c9 2512 (始めに受信キャッシュクリアしている)
tukudani 0:65f9be2858c9 2513 戻り値:エラー時 0x00
tukudani 0:65f9be2858c9 2514
tukudani 0:65f9be2858c9 2515 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 2516 */
tukudani 0:65f9be2858c9 2517
tukudani 0:65f9be2858c9 2518 byte err,retry;
tukudani 0:65f9be2858c9 2519 byte r_dat = 10; // AT=0、RAT=10、TX=未定 リモートATと通常ATの応答値dataの代入位置の差
tukudani 0:65f9be2858c9 2520 byte r_at = 1; // AT=0、RAT=1 リモートの可否
tukudani 0:65f9be2858c9 2521 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 2522 unsigned int wait_add = 0;
tukudani 0:65f9be2858c9 2523 #endif
tukudani 0:65f9be2858c9 2524 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 2525 byte i;
tukudani 0:65f9be2858c9 2526 #endif
tukudani 0:65f9be2858c9 2527
tukudani 0:65f9be2858c9 2528 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2529 sci_write_check();
tukudani 0:65f9be2858c9 2530 #endif
tukudani 0:65f9be2858c9 2531 #ifdef H3694
tukudani 0:65f9be2858c9 2532 sci_clear();
tukudani 0:65f9be2858c9 2533 #endif
tukudani 0:65f9be2858c9 2534
tukudani 0:65f9be2858c9 2535 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 2536 if( at[0] == 'A' && at[1] == 'T' ){
tukudani 0:65f9be2858c9 2537 r_dat = 0; r_at=0;
tukudani 0:65f9be2858c9 2538 if( at[2] == 'W' && at[3] == 'R' ) wait_add = 100; // ATWR 110~1100ms
tukudani 0:65f9be2858c9 2539 } else if ( at[0] == 'R' && at[1] == 'A' && at[2] == 'T' ){
tukudani 0:65f9be2858c9 2540 r_dat = 10; r_at=1;
tukudani 0:65f9be2858c9 2541 if( at[3] == 'W' && at[4] == 'R' ) wait_add = 100; // RATWR 120~1200ms
tukudani 0:65f9be2858c9 2542 }
tukudani 0:65f9be2858c9 2543 if( xbee_at_tx( at ,data ,len ) > 0){
tukudani 0:65f9be2858c9 2544 err = 12; // 受信なしエラー
tukudani 0:65f9be2858c9 2545 for( retry = 10 ; ( retry > 0 && err != 0 ) ; retry-- ){
tukudani 0:65f9be2858c9 2546 wait_millisec( 10 + (unsigned int)r_dat ); // 応答時間待ち AT 10~100ms / RAT 20~200ms
tukudani 0:65f9be2858c9 2547 if( wait_add != 0 ){
tukudani 0:65f9be2858c9 2548 wait_millisec( wait_add ); // 追加ウェイト
tukudani 0:65f9be2858c9 2549 }
tukudani 0:65f9be2858c9 2550 if( xbee_at_rx( data ) > 0){
tukudani 0:65f9be2858c9 2551 if( (
tukudani 0:65f9be2858c9 2552 ( data[3] == MODE_RES && r_at == 0 ) || ( data[3] == MODE_RESP && r_at == 1 )
tukudani 0:65f9be2858c9 2553 ) && (
tukudani 0:65f9be2858c9 2554 data[4] == PACKET_ID
tukudani 0:65f9be2858c9 2555 )
tukudani 0:65f9be2858c9 2556 ){
tukudani 0:65f9be2858c9 2557 /* ATコマンドが正しいかどうかの確認を削除(Ver 1.51)
tukudani 0:65f9be2858c9 2558 ※MODE_RESPの条件を追加したので不要のはず。
tukudani 0:65f9be2858c9 2559 && data[5+r_dat] == (byte)at[2+r_at] && data[6+r_dat] == (byte)at[3+r_at]
tukudani 0:65f9be2858c9 2560 */
tukudani 0:65f9be2858c9 2561 if( data[7+r_dat] == 0x00 ){
tukudani 0:65f9be2858c9 2562 err=0;
tukudani 0:65f9be2858c9 2563 }else err = data[7+r_dat]; // ATのERRORコード (AT:data[7] RAT:data[17])
tukudani 0:65f9be2858c9 2564 }else{ // 受信したパケットIDまたはATコマンドが相違しているとき
tukudani 0:65f9be2858c9 2565 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 2566 if( CACHE_COUNTER < CACHE_RES ){
tukudani 0:65f9be2858c9 2567 for( i=0 ; i < API_SIZE ; i++) CACHE_MEM[CACHE_COUNTER][i] = data[i];
tukudani 0:65f9be2858c9 2568 CACHE_COUNTER++;
tukudani 0:65f9be2858c9 2569 #ifdef DEBUG
tukudani 0:65f9be2858c9 2570 lcd_putstr("### CACHE_RES ### <- "); // DEBUG
tukudani 0:65f9be2858c9 2571 lcd_disp_hex( CACHE_COUNTER );
tukudani 0:65f9be2858c9 2572 lcd_putch('\n');
tukudani 0:65f9be2858c9 2573 #endif
tukudani 0:65f9be2858c9 2574 }
tukudani 0:65f9be2858c9 2575 #endif
tukudani 0:65f9be2858c9 2576 // キャッシュ容量が無い場合は受信データを保持せずにリトライする
tukudani 0:65f9be2858c9 2577 }
tukudani 0:65f9be2858c9 2578 }
tukudani 0:65f9be2858c9 2579 }
tukudani 0:65f9be2858c9 2580 //printf("\nretry=%d\n",retry);
tukudani 0:65f9be2858c9 2581 }else err = 11; // 送信失敗
tukudani 0:65f9be2858c9 2582 #else // XBEE_WIFI (PC + Arduino)
tukudani 0:65f9be2858c9 2583 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 2584 for(i=0;i<(5+len);i++) Serial.print(at[i]);
tukudani 0:65f9be2858c9 2585 Serial.print('(');
tukudani 0:65f9be2858c9 2586 Serial.print(PACKET_ID,HEX);
tukudani 0:65f9be2858c9 2587 Serial.println(')');
tukudani 0:65f9be2858c9 2588 #endif
tukudani 0:65f9be2858c9 2589
tukudani 0:65f9be2858c9 2590 if(DEVICE_TYPE == XB_TYPE_NULL){
tukudani 0:65f9be2858c9 2591 #ifdef DEBUG
tukudani 0:65f9be2858c9 2592 printf("DEBUG:check DEVICE_TYPE = XB_TYPE_WIFI20\n");
tukudani 0:65f9be2858c9 2593 #endif
tukudani 0:65f9be2858c9 2594 DEVICE_TYPE = XB_TYPE_WIFI20;
tukudani 0:65f9be2858c9 2595 if( xbee_tx_rx(at, data, len) > 0 ){
tukudani 0:65f9be2858c9 2596 return(1);
tukudani 0:65f9be2858c9 2597 }else{
tukudani 0:65f9be2858c9 2598 #ifdef DEBUG
tukudani 0:65f9be2858c9 2599 printf("DEBUG:check DEVICE_TYPE = XB_TYPE_WIFI10\n");
tukudani 0:65f9be2858c9 2600 #endif
tukudani 0:65f9be2858c9 2601 DEVICE_TYPE = XB_TYPE_WIFI10;
tukudani 0:65f9be2858c9 2602 if( xbee_tx_rx(at, data, len) > 0 ) return(1);
tukudani 0:65f9be2858c9 2603 else{
tukudani 0:65f9be2858c9 2604 #ifdef DEBUG
tukudani 0:65f9be2858c9 2605 printf("DEBUG:no Response at check DEVICE_TYPE\n");
tukudani 0:65f9be2858c9 2606 #endif
tukudani 0:65f9be2858c9 2607 DEVICE_TYPE = XB_TYPE_NULL;
tukudani 0:65f9be2858c9 2608 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2609 ERR_CODE=12;
tukudani 0:65f9be2858c9 2610 strcopy( ERR_LOG ,"ERR:tx_rx no Rx Res. wifi check");
tukudani 0:65f9be2858c9 2611 #endif
tukudani 0:65f9be2858c9 2612 return(0); // エラー終了
tukudani 0:65f9be2858c9 2613 }
tukudani 0:65f9be2858c9 2614 }
tukudani 0:65f9be2858c9 2615 }
tukudani 0:65f9be2858c9 2616 if( xbee_at_tx( at ,data ,len ) > 0){
tukudani 0:65f9be2858c9 2617 err = 12; // 受信なしエラー
tukudani 0:65f9be2858c9 2618 for( retry = 10 ; ( retry > 0 && err != 0 ) ; retry-- ){
tukudani 0:65f9be2858c9 2619 if( xbee_at_rx( data ) > 0){
tukudani 0:65f9be2858c9 2620 if( (
tukudani 0:65f9be2858c9 2621 ( data[3] == MODE_RES && r_at == 0 ) || ( data[3] == MODE_RESP && r_at == 1 )
tukudani 0:65f9be2858c9 2622 ) && (
tukudani 0:65f9be2858c9 2623 data[4] == PACKET_ID
tukudani 0:65f9be2858c9 2624 )
tukudani 0:65f9be2858c9 2625 ){
tukudani 0:65f9be2858c9 2626 /* ATコマンドが正しいかどうかの確認を削除(Ver 1.51)
tukudani 0:65f9be2858c9 2627 ※MODE_RESPの条件を追加したので不要のはず。
tukudani 0:65f9be2858c9 2628 && data[5+r_dat] == (byte)at[2+r_at] && data[6+r_dat] == (byte)at[3+r_at]
tukudani 0:65f9be2858c9 2629 */
tukudani 0:65f9be2858c9 2630 if( data[7+r_dat] == 0x00 ){
tukudani 0:65f9be2858c9 2631 err=0;
tukudani 0:65f9be2858c9 2632 }else err = data[7+r_dat]; // ATのERRORコード (AT:data[7] RAT:data[17])
tukudani 0:65f9be2858c9 2633 }else{ // 受信したパケットIDまたはATコマンドが相違しているとき
tukudani 0:65f9be2858c9 2634 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 2635 if( CACHE_COUNTER < CACHE_RES ){
tukudani 0:65f9be2858c9 2636 for( i=0 ; i < API_SIZE ; i++) CACHE_MEM[CACHE_COUNTER][i] = data[i];
tukudani 0:65f9be2858c9 2637 CACHE_COUNTER++;
tukudani 0:65f9be2858c9 2638 #ifdef DEBUG
tukudani 0:65f9be2858c9 2639 lcd_putstr("### CACHE_RES ### <- "); // DEBUG
tukudani 0:65f9be2858c9 2640 lcd_disp_hex( CACHE_COUNTER );
tukudani 0:65f9be2858c9 2641 lcd_putch('\n');
tukudani 0:65f9be2858c9 2642 #endif
tukudani 0:65f9be2858c9 2643 }
tukudani 0:65f9be2858c9 2644 #endif
tukudani 0:65f9be2858c9 2645 // キャッシュ容量が無い場合は受信データを保持せずにリトライする
tukudani 0:65f9be2858c9 2646 }
tukudani 0:65f9be2858c9 2647 }else{ // 受信データが無い時(リトライ前)
tukudani 0:65f9be2858c9 2648 wait_millisec(50); // 50msの待ち時間
tukudani 0:65f9be2858c9 2649 }
tukudani 0:65f9be2858c9 2650 }
tukudani 0:65f9be2858c9 2651 }else err = 11; // 送信失敗
tukudani 0:65f9be2858c9 2652 delay(1); // 直前のコマンド応答がすぐに返った時にキャッシュに貯めれないことを防止する
tukudani 0:65f9be2858c9 2653
tukudani 0:65f9be2858c9 2654 #endif
tukudani 0:65f9be2858c9 2655
tukudani 0:65f9be2858c9 2656 if( err ){
tukudani 0:65f9be2858c9 2657 wait_millisec( 1000 ); // 応答待ち状態で、次々にコマンドを送るとXBeeモジュールが非応答になる対策
tukudani 0:65f9be2858c9 2658 #ifdef H3694
tukudani 0:65f9be2858c9 2659 led_red( 1 );
tukudani 0:65f9be2858c9 2660 #endif
tukudani 0:65f9be2858c9 2661 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2662 ERR_CODE=err;
tukudani 0:65f9be2858c9 2663 switch(err){
tukudani 0:65f9be2858c9 2664 case 2: // 01234567890123456789
tukudani 0:65f9be2858c9 2665 strcopy( ERR_LOG ,"ERR:tx_rx AT Command");
tukudani 0:65f9be2858c9 2666 break;
tukudani 0:65f9be2858c9 2667 case 3:
tukudani 0:65f9be2858c9 2668 strcopy( ERR_LOG ,"ERR:tx_rx AT Param. ");
tukudani 0:65f9be2858c9 2669 break;
tukudani 0:65f9be2858c9 2670 case 4:
tukudani 0:65f9be2858c9 2671 strcopy( ERR_LOG ,"ERR:tx_rx AT Commu. ");
tukudani 0:65f9be2858c9 2672 break;
tukudani 0:65f9be2858c9 2673 case 10:
tukudani 0:65f9be2858c9 2674 strcopy( ERR_LOG ,"ERR:tx_rx not AT CMD");
tukudani 0:65f9be2858c9 2675 break;
tukudani 0:65f9be2858c9 2676 case 11:
tukudani 0:65f9be2858c9 2677 strcopy( ERR_LOG ,"ERR:tx_rx TX Failed ");
tukudani 0:65f9be2858c9 2678 break;
tukudani 0:65f9be2858c9 2679 case 12:
tukudani 0:65f9be2858c9 2680 strcopy( ERR_LOG ,"ERR:tx_rx no Rx Res.");
tukudani 0:65f9be2858c9 2681 break;
tukudani 0:65f9be2858c9 2682 case 13:
tukudani 0:65f9be2858c9 2683 strcopy( ERR_LOG ,"ERR:tx_rx AT Pckt ID");
tukudani 0:65f9be2858c9 2684 break;
tukudani 0:65f9be2858c9 2685 case 14:
tukudani 0:65f9be2858c9 2686 strcopy( ERR_LOG ,"ERR:tx_rx Diff.Adrs.");
tukudani 0:65f9be2858c9 2687 break;
tukudani 0:65f9be2858c9 2688 default:
tukudani 0:65f9be2858c9 2689 strcopy( ERR_LOG ,"ERR:tx_rx AT unknown");
tukudani 0:65f9be2858c9 2690 break;
tukudani 0:65f9be2858c9 2691 }
tukudani 0:65f9be2858c9 2692 ERR_LOG[20] = ':';
tukudani 0:65f9be2858c9 2693 if( at[0] == 'A' ){
tukudani 0:65f9be2858c9 2694 ERR_LOG[21] = at[2];
tukudani 0:65f9be2858c9 2695 ERR_LOG[22] = at[3];
tukudani 0:65f9be2858c9 2696 }else{
tukudani 0:65f9be2858c9 2697 ERR_LOG[21] = at[3];
tukudani 0:65f9be2858c9 2698 ERR_LOG[22] = at[4];
tukudani 0:65f9be2858c9 2699 }
tukudani 0:65f9be2858c9 2700 #endif
tukudani 0:65f9be2858c9 2701 }
tukudani 0:65f9be2858c9 2702 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 2703 if( err ){
tukudani 0:65f9be2858c9 2704 Serial.print("ERR in tx_rx ");
tukudani 0:65f9be2858c9 2705 Serial.println(err,DEC);
tukudani 0:65f9be2858c9 2706 }
tukudani 0:65f9be2858c9 2707 #endif
tukudani 0:65f9be2858c9 2708 #ifdef DEBUG
tukudani 0:65f9be2858c9 2709 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 2710 if( err ){
tukudani 0:65f9be2858c9 2711 _xbee_debug.printf("DEBUG: %s ", ERR_LOG);
tukudani 0:65f9be2858c9 2712 _xbee_debug.printf("(err=%d)\r\n", err);
tukudani 0:65f9be2858c9 2713 }
tukudani 0:65f9be2858c9 2714 #else
tukudani 0:65f9be2858c9 2715 if( err ) fprintf(stderr,"DEBUG: %s (err=%d)\n", ERR_LOG,err);
tukudani 0:65f9be2858c9 2716 #endif
tukudani 0:65f9be2858c9 2717 #endif
tukudani 0:65f9be2858c9 2718 return( !err );
tukudani 0:65f9be2858c9 2719 }
tukudani 0:65f9be2858c9 2720
tukudani 0:65f9be2858c9 2721 /***********************************************************************
tukudani 0:65f9be2858c9 2722 XBee用 UART出力API
tukudani 0:65f9be2858c9 2723 byte xbee_putch( const char c );
tukudani 0:65f9be2858c9 2724 byte xbee_putstr( const char *s );
tukudani 0:65f9be2858c9 2725 void xbee_disp_hex( const unsigned char i );
tukudani 0:65f9be2858c9 2726 void xbee_disp_1( const unsigned int x );
tukudani 0:65f9be2858c9 2727 void xbee_disp_2( unsigned int x );
tukudani 0:65f9be2858c9 2728 void xbee_disp_3(unsigned int x);
tukudani 0:65f9be2858c9 2729 void xbee_disp_5(unsigned int x);
tukudani 0:65f9be2858c9 2730 void xbee_log( const byte level, const char *err , const byte x );
tukudani 0:65f9be2858c9 2731 ***********************************************************************/
tukudani 0:65f9be2858c9 2732
tukudani 0:65f9be2858c9 2733 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2734 byte xbee_putch( const char c ){
tukudani 0:65f9be2858c9 2735 byte data[2];
tukudani 0:65f9be2858c9 2736 byte len;
tukudani 0:65f9be2858c9 2737 data[0] = (byte)c;
tukudani 0:65f9be2858c9 2738 if( xbee_at_tx( "TX", data , 1) == 0 ) len=0; else len=1;
tukudani 0:65f9be2858c9 2739 return( len );
tukudani 0:65f9be2858c9 2740 }
tukudani 0:65f9be2858c9 2741 #endif
tukudani 0:65f9be2858c9 2742
tukudani 0:65f9be2858c9 2743 byte xbee_putstr( const char *s ){
tukudani 0:65f9be2858c9 2744 /*
tukudani 0:65f9be2858c9 2745 文字を送信する
tukudani 0:65f9be2858c9 2746 入力:char *s
tukudani 0:65f9be2858c9 2747 出力:送信データ長を応答。0の場合は異常
tukudani 0:65f9be2858c9 2748 */
tukudani 0:65f9be2858c9 2749 byte data[API_TXSIZE-17]; // 17バイトはAPIヘッダ+CRC1バイトなのでデータ長は[API_TXSIZE-18]+null文字で+1する-17
tukudani 0:65f9be2858c9 2750 byte i;
tukudani 0:65f9be2858c9 2751
tukudani 0:65f9be2858c9 2752 for(i=0; (i< (API_TXSIZE-18) ) && (s[i] != 0x00) ; i++){ // データ長はAPI_TXSIZE-18
tukudani 0:65f9be2858c9 2753 data[i] = (byte)s[i]; // テキストデータをバイナリデータ(バイト値)に変換する
tukudani 0:65f9be2858c9 2754 }
tukudani 0:65f9be2858c9 2755 data[i] = 0x00;
tukudani 0:65f9be2858c9 2756 if( xbee_at_tx( "TX", data , i) == 0) i=0;
tukudani 0:65f9be2858c9 2757 return( i );
tukudani 0:65f9be2858c9 2758 }
tukudani 0:65f9be2858c9 2759
tukudani 0:65f9be2858c9 2760 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2761 void xbee_disp_hex( const byte i ){
tukudani 0:65f9be2858c9 2762 byte data[3];
tukudani 0:65f9be2858c9 2763 data[0] = i&0xF0;
tukudani 0:65f9be2858c9 2764 data[0] = data[0]>>4;
tukudani 0:65f9be2858c9 2765 data[0] += '0';
tukudani 0:65f9be2858c9 2766 if (data[0]>'9') data[0] += 7;
tukudani 0:65f9be2858c9 2767 data[1]=(i&0x0F)+'0';
tukudani 0:65f9be2858c9 2768 if (data[1]>'9') data[1]+=7;
tukudani 0:65f9be2858c9 2769 xbee_at_tx( "TX", data , 2);
tukudani 0:65f9be2858c9 2770 }
tukudani 0:65f9be2858c9 2771
tukudani 0:65f9be2858c9 2772 void xbee_disp_1( const unsigned int x ){
tukudani 0:65f9be2858c9 2773 if (x<10) xbee_putch((char)(x+0x30));
tukudani 0:65f9be2858c9 2774 else if (x<16) xbee_putch((char)(x-10+'A'));
tukudani 0:65f9be2858c9 2775 else xbee_putch('X');
tukudani 0:65f9be2858c9 2776 }
tukudani 0:65f9be2858c9 2777
tukudani 0:65f9be2858c9 2778 void xbee_disp_2( unsigned int x ){
tukudani 0:65f9be2858c9 2779 char s[3];
tukudani 0:65f9be2858c9 2780 unsigned int y;
tukudani 0:65f9be2858c9 2781 if (x<100){
tukudani 0:65f9be2858c9 2782 y=x/10; s[0]=(char)(y+0x30); x-=(y*10);
tukudani 0:65f9be2858c9 2783 s[1]=(char)(x+0x30);
tukudani 0:65f9be2858c9 2784 s[2]='\0';
tukudani 0:65f9be2858c9 2785 if( s[0]=='0' ){
tukudani 0:65f9be2858c9 2786 s[0]=' ';
tukudani 0:65f9be2858c9 2787 }
tukudani 0:65f9be2858c9 2788 xbee_putstr( s );
tukudani 0:65f9be2858c9 2789 }else xbee_putstr("XX");
tukudani 0:65f9be2858c9 2790 }
tukudani 0:65f9be2858c9 2791
tukudani 0:65f9be2858c9 2792 void xbee_disp_3(unsigned int x){
tukudani 0:65f9be2858c9 2793 char s[4];
tukudani 0:65f9be2858c9 2794 unsigned int y;
tukudani 0:65f9be2858c9 2795 if (x<1000){
tukudani 0:65f9be2858c9 2796 y=x/100; s[0]=(char)(y+0x30); x-=(y*100);
tukudani 0:65f9be2858c9 2797 y=x/10; s[1]=(char)(y+0x30); x-= (y*10);
tukudani 0:65f9be2858c9 2798 s[2]=(char)(x+0x30);
tukudani 0:65f9be2858c9 2799 s[3]='\0';
tukudani 0:65f9be2858c9 2800 if( s[0]=='0' ){
tukudani 0:65f9be2858c9 2801 s[0]=' ';
tukudani 0:65f9be2858c9 2802 if( s[1]=='0' ){
tukudani 0:65f9be2858c9 2803 s[1]=' ';
tukudani 0:65f9be2858c9 2804 }
tukudani 0:65f9be2858c9 2805 }
tukudani 0:65f9be2858c9 2806 xbee_putstr( s );
tukudani 0:65f9be2858c9 2807 }else xbee_putstr("XXX");
tukudani 0:65f9be2858c9 2808 }
tukudani 0:65f9be2858c9 2809
tukudani 0:65f9be2858c9 2810 void xbee_disp_5(unsigned int x){
tukudani 0:65f9be2858c9 2811 char s[6];
tukudani 0:65f9be2858c9 2812 unsigned int y;
tukudani 0:65f9be2858c9 2813 if (x<=65535){
tukudani 0:65f9be2858c9 2814 y=x/10000; s[0]=(char)(y+0x30); x-=(y*10000);
tukudani 0:65f9be2858c9 2815 y=x/1000; s[1]=(char)(y+0x30); x-= (y*1000);
tukudani 0:65f9be2858c9 2816 y=x/100; s[2]=(char)(y+0x30); x-= (y*100);
tukudani 0:65f9be2858c9 2817 y=x/10; s[3]=(char)(y+0x30); x-= (y*10);
tukudani 0:65f9be2858c9 2818 s[4]=(char)(x+0x30);
tukudani 0:65f9be2858c9 2819 s[5]='\0';
tukudani 0:65f9be2858c9 2820 if( s[0]=='0' ){
tukudani 0:65f9be2858c9 2821 s[0]=' ';
tukudani 0:65f9be2858c9 2822 if( s[1]=='0' ){
tukudani 0:65f9be2858c9 2823 s[1]=' ';
tukudani 0:65f9be2858c9 2824 if( s[2]=='0' ){
tukudani 0:65f9be2858c9 2825 s[2]=' ';
tukudani 0:65f9be2858c9 2826 if( s[3]=='0' ){
tukudani 0:65f9be2858c9 2827 s[3]=' ';
tukudani 0:65f9be2858c9 2828 }
tukudani 0:65f9be2858c9 2829 }
tukudani 0:65f9be2858c9 2830 }
tukudani 0:65f9be2858c9 2831 }
tukudani 0:65f9be2858c9 2832 xbee_putstr( s );
tukudani 0:65f9be2858c9 2833 }else xbee_putstr("XXXXX");
tukudani 0:65f9be2858c9 2834 }
tukudani 0:65f9be2858c9 2835 #endif // LITE
tukudani 0:65f9be2858c9 2836
tukudani 0:65f9be2858c9 2837 void xbee_log(const byte level, const char *err, const byte x ){
tukudani 0:65f9be2858c9 2838 /*
tukudani 0:65f9be2858c9 2839 エラーログ用レベルガイド
tukudani 0:65f9be2858c9 2840 LEVEL 5 ERROR 致命的なエラー、バグ発生
tukudani 0:65f9be2858c9 2841 LEVEL 4 ERR 通信エラー等のうち不具合動作を伴う懸念のあるもの。500ms待ち挿入
tukudani 0:65f9be2858c9 2842 LEVEL 3 CAUTION 通信エラー、使い方の問題など
tukudani 0:65f9be2858c9 2843 LEVEL 2 未定義
tukudani 0:65f9be2858c9 2844 LEVEL 1 LOG 各関数の動作確認用
tukudani 0:65f9be2858c9 2845 */
tukudani 0:65f9be2858c9 2846 #ifdef ERRLOG
tukudani 0:65f9be2858c9 2847 /* PCの場合 */
tukudani 0:65f9be2858c9 2848 #ifndef ARDUINO
tukudani 0:65f9be2858c9 2849 #ifndef H3694
tukudani 0:65f9be2858c9 2850 #ifdef XBEE_ERROR_TIME
tukudani 0:65f9be2858c9 2851 time_t error_time;
tukudani 0:65f9be2858c9 2852 struct tm *error_time_st;
tukudani 0:65f9be2858c9 2853
tukudani 0:65f9be2858c9 2854 time(&error_time);
tukudani 0:65f9be2858c9 2855 error_time_st = localtime(&error_time);
tukudani 0:65f9be2858c9 2856 #endif
tukudani 0:65f9be2858c9 2857 #endif
tukudani 0:65f9be2858c9 2858 #endif
tukudani 0:65f9be2858c9 2859
tukudani 0:65f9be2858c9 2860 #ifdef DEBUG
tukudani 0:65f9be2858c9 2861 if( level >= 1 ){
tukudani 0:65f9be2858c9 2862 #else
tukudani 0:65f9be2858c9 2863 if( level >= 3 ){
tukudani 0:65f9be2858c9 2864 #endif
tukudani 0:65f9be2858c9 2865 /* マイコンの場合 */
tukudani 0:65f9be2858c9 2866 #ifdef ARDUINO
tukudani 0:65f9be2858c9 2867 lcd_goto(LCD_ROW_4);
tukudani 0:65f9be2858c9 2868 lcd_putch('[');
tukudani 0:65f9be2858c9 2869 lcd_disp_hex( TIMER_SEC );
tukudani 0:65f9be2858c9 2870 lcd_putch(':');
tukudani 0:65f9be2858c9 2871 lcd_disp_hex( timera() );
tukudani 0:65f9be2858c9 2872 lcd_putch(']');
tukudani 0:65f9be2858c9 2873 lcd_putstr( err );
tukudani 0:65f9be2858c9 2874 lcd_putch('(');
tukudani 0:65f9be2858c9 2875 lcd_disp_hex( x );
tukudani 0:65f9be2858c9 2876 lcd_putch(')');
tukudani 0:65f9be2858c9 2877 #endif
tukudani 0:65f9be2858c9 2878 #ifdef H3694
tukudani 0:65f9be2858c9 2879 lcd_goto(LCD_ROW_4);
tukudani 0:65f9be2858c9 2880 lcd_putch('[');
tukudani 0:65f9be2858c9 2881 lcd_disp_hex( TIMER_SEC );
tukudani 0:65f9be2858c9 2882 lcd_putch(':');
tukudani 0:65f9be2858c9 2883 lcd_disp_hex( timera() );
tukudani 0:65f9be2858c9 2884 lcd_putch(']');
tukudani 0:65f9be2858c9 2885 lcd_putstr( err );
tukudani 0:65f9be2858c9 2886 lcd_putch('(');
tukudani 0:65f9be2858c9 2887 lcd_disp_hex( x );
tukudani 0:65f9be2858c9 2888 lcd_putch(')');
tukudani 0:65f9be2858c9 2889 #endif
tukudani 0:65f9be2858c9 2890 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 2891 lcd_goto(LCD_ROW_4);
tukudani 0:65f9be2858c9 2892 lcd_putch('[');
tukudani 0:65f9be2858c9 2893 lcd_disp_hex( TIMER_SEC );
tukudani 0:65f9be2858c9 2894 lcd_putch(':');
tukudani 0:65f9be2858c9 2895 lcd_disp_hex( timera() );
tukudani 0:65f9be2858c9 2896 lcd_putch(']');
tukudani 0:65f9be2858c9 2897 lcd_putstr( err );
tukudani 0:65f9be2858c9 2898 lcd_putch('(');
tukudani 0:65f9be2858c9 2899 lcd_disp_hex( x );
tukudani 0:65f9be2858c9 2900 lcd_putch(')');
tukudani 0:65f9be2858c9 2901 lcd_putch( 0x0D ); // CR(\r)
tukudani 0:65f9be2858c9 2902 lcd_putch( 0x0A ); // LF(\n)
tukudani 0:65f9be2858c9 2903 #endif
tukudani 0:65f9be2858c9 2904
tukudani 0:65f9be2858c9 2905 /*
tukudani 0:65f9be2858c9 2906 lcd_putch( 0x0D ); // CR(\r)
tukudani 0:65f9be2858c9 2907 lcd_putch( 0x0A ); // LF(\n)
tukudani 0:65f9be2858c9 2908 */
tukudani 0:65f9be2858c9 2909 /* PCの場合 */
tukudani 0:65f9be2858c9 2910 #ifndef ARDUINO
tukudani 0:65f9be2858c9 2911 #ifndef H3694
tukudani 0:65f9be2858c9 2912 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 2913 #ifdef XBEE_ERROR_TIME
tukudani 0:65f9be2858c9 2914 fprintf(stderr,"%4d/%02d/%02d %02d:%02d:%02d[%02X:%02X]%s(%02X)\n",
tukudani 0:65f9be2858c9 2915 error_time_st->tm_year+1900,
tukudani 0:65f9be2858c9 2916 error_time_st->tm_mon+1,
tukudani 0:65f9be2858c9 2917 error_time_st->tm_mday,
tukudani 0:65f9be2858c9 2918 error_time_st->tm_hour,
tukudani 0:65f9be2858c9 2919 error_time_st->tm_min,
tukudani 0:65f9be2858c9 2920 error_time_st->tm_sec,
tukudani 0:65f9be2858c9 2921 TIMER_SEC,
tukudani 0:65f9be2858c9 2922 timera(),
tukudani 0:65f9be2858c9 2923 err,
tukudani 0:65f9be2858c9 2924 x);
tukudani 0:65f9be2858c9 2925 #else
tukudani 0:65f9be2858c9 2926 fprintf(stderr,"[%02X:%02X]%s(%02X)\n",
tukudani 0:65f9be2858c9 2927 TIMER_SEC,
tukudani 0:65f9be2858c9 2928 timera(),
tukudani 0:65f9be2858c9 2929 err,
tukudani 0:65f9be2858c9 2930 x);
tukudani 0:65f9be2858c9 2931 #endif
tukudani 0:65f9be2858c9 2932 #endif
tukudani 0:65f9be2858c9 2933 #endif
tukudani 0:65f9be2858c9 2934 #endif
tukudani 0:65f9be2858c9 2935 }
tukudani 0:65f9be2858c9 2936 if( level > 3 ) wait_millisec( 500 ); // LEVEL 4以上に対して待ち時間を設定
tukudani 0:65f9be2858c9 2937 #endif
tukudani 0:65f9be2858c9 2938 }
tukudani 0:65f9be2858c9 2939
tukudani 0:65f9be2858c9 2940 /***********************************************************************
tukudani 0:65f9be2858c9 2941 アプリ向け提供関数
tukudani 0:65f9be2858c9 2942 XBee用 各種制御 入出力定義を変更した場合は履歴に記載します
tukudani 0:65f9be2858c9 2943 ***********************************************************************/
tukudani 0:65f9be2858c9 2944 byte xbee_readable(void){
tukudani 0:65f9be2858c9 2945 //byte available = _xbee_serial.readable();
tukudani 0:65f9be2858c9 2946 byte available = _xbee_serial_bp;
tukudani 0:65f9be2858c9 2947 return available;
tukudani 0:65f9be2858c9 2948
tukudani 0:65f9be2858c9 2949 }
tukudani 0:65f9be2858c9 2950
tukudani 0:65f9be2858c9 2951 byte xbee_getc(void){
tukudani 0:65f9be2858c9 2952 byte getd = _xbee_serial.getc();
tukudani 0:65f9be2858c9 2953 //sci_read(100);
tukudani 0:65f9be2858c9 2954 return getd;
tukudani 0:65f9be2858c9 2955 }
tukudani 0:65f9be2858c9 2956
tukudani 0:65f9be2858c9 2957 void xbee_clear(void){
tukudani 0:65f9be2858c9 2958 _xbee_serial_bp = 0;
tukudani 0:65f9be2858c9 2959 _xbee_serial_rp = 0;
tukudani 0:65f9be2858c9 2960 }
tukudani 0:65f9be2858c9 2961
tukudani 0:65f9be2858c9 2962 byte xbee_reset( void ){
tukudani 0:65f9be2858c9 2963 /* XBeeモジュールのリセット
tukudani 0:65f9be2858c9 2964 戻り値:0=エラー、強制終了あり(PC版)、無限定しあり(H8版)
tukudani 0:65f9be2858c9 2965 */
tukudani 0:65f9be2858c9 2966 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2967 byte i;
tukudani 0:65f9be2858c9 2968 #endif
tukudani 0:65f9be2858c9 2969 byte ret=0; // 戻り値 0:異常終了
tukudani 0:65f9be2858c9 2970 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 2971 byte value[API_SIZE];
tukudani 0:65f9be2858c9 2972 value[0] = 0x00;
tukudani 0:65f9be2858c9 2973 #endif
tukudani 0:65f9be2858c9 2974
tukudani 0:65f9be2858c9 2975 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 2976 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2977 sci_write_check();
tukudani 0:65f9be2858c9 2978 #endif
tukudani 0:65f9be2858c9 2979 sci_clear(); // シリアル異常をクリア
tukudani 0:65f9be2858c9 2980 DEVICE_TYPE = 0x20; // タイプ名を初期化
tukudani 0:65f9be2858c9 2981 #endif
tukudani 0:65f9be2858c9 2982
tukudani 0:65f9be2858c9 2983 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 2984 ret = xbee_tx_rx( "ATVR", value ,0 ); // ZigBee 種類の取得
tukudani 0:65f9be2858c9 2985 if( ret == 0 ){
tukudani 0:65f9be2858c9 2986 #ifdef ARDUINO
tukudani 0:65f9be2858c9 2987 return(0);
tukudani 0:65f9be2858c9 2988 #else
tukudani 0:65f9be2858c9 2989 exit(-1);
tukudani 0:65f9be2858c9 2990 #endif
tukudani 0:65f9be2858c9 2991 }else{
tukudani 0:65f9be2858c9 2992 DEVICE_TYPE = value[8];
tukudani 0:65f9be2858c9 2993 } // LITEはリセットしない
tukudani 0:65f9be2858c9 2994 #else // normal
tukudani 0:65f9be2858c9 2995 wait_millisec(100);
tukudani 0:65f9be2858c9 2996 for( i=1 ; i< 4 ; i++){
tukudani 0:65f9be2858c9 2997 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 2998 ret = xbee_tx_rx( "ATVR", value ,0 );
tukudani 0:65f9be2858c9 2999 #else
tukudani 0:65f9be2858c9 3000 ret = 1;
tukudani 0:65f9be2858c9 3001 #endif
tukudani 0:65f9be2858c9 3002 if( ret > 0){
tukudani 0:65f9be2858c9 3003 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3004 DEVICE_TYPE = value[8];
tukudani 0:65f9be2858c9 3005 if( DEVICE_TYPE != ZB_TYPE_COORD &&
tukudani 0:65f9be2858c9 3006 DEVICE_TYPE != ZB_TYPE_ROUTER &&
tukudani 0:65f9be2858c9 3007 DEVICE_TYPE != ZB_TYPE_ENDDEV){ // VRの確認
tukudani 0:65f9be2858c9 3008 #ifdef LCD_H
tukudani 0:65f9be2858c9 3009 lcd_cls();
tukudani 0:65f9be2858c9 3010 #ifdef H3694
tukudani 0:65f9be2858c9 3011 led_red( 1 );
tukudani 0:65f9be2858c9 3012 #endif
tukudani 0:65f9be2858c9 3013 lcd_putstr( "EXIT:XBEE NOT IN API MODE" );
tukudani 0:65f9be2858c9 3014 #endif
tukudani 0:65f9be2858c9 3015 #ifdef H3694
tukudani 0:65f9be2858c9 3016 return(0);
tukudani 0:65f9be2858c9 3017 #elif ARDUINO
tukudani 0:65f9be2858c9 3018 return(0);
tukudani 0:65f9be2858c9 3019 #else
tukudani 0:65f9be2858c9 3020 exit(-1);
tukudani 0:65f9be2858c9 3021 #endif
tukudani 0:65f9be2858c9 3022 }
tukudani 0:65f9be2858c9 3023 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3024 DEVICE_TYPE = XB_TYPE_NULL;
tukudani 0:65f9be2858c9 3025 #endif
tukudani 0:65f9be2858c9 3026 }else{
tukudani 0:65f9be2858c9 3027 if( i == 3 ){
tukudani 0:65f9be2858c9 3028 #ifdef LCD_H
tukudani 0:65f9be2858c9 3029 lcd_cls();
tukudani 0:65f9be2858c9 3030 #ifdef H3694
tukudani 0:65f9be2858c9 3031 led_red( 1 );
tukudani 0:65f9be2858c9 3032 #endif
tukudani 0:65f9be2858c9 3033 lcd_putstr( "EXIT:NO RESPONCE FROM XBEE" );
tukudani 0:65f9be2858c9 3034 #endif
tukudani 0:65f9be2858c9 3035 #ifdef H3694
tukudani 0:65f9be2858c9 3036 return(0);
tukudani 0:65f9be2858c9 3037 #elif ARDUINO
tukudani 0:65f9be2858c9 3038 return(0);
tukudani 0:65f9be2858c9 3039 #else
tukudani 0:65f9be2858c9 3040 exit(-1);
tukudani 0:65f9be2858c9 3041 #endif
tukudani 0:65f9be2858c9 3042 }
tukudani 0:65f9be2858c9 3043 wait_millisec(1000);
tukudani 0:65f9be2858c9 3044 }
tukudani 0:65f9be2858c9 3045 }
tukudani 0:65f9be2858c9 3046 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 3047 wait_millisec(100);
tukudani 0:65f9be2858c9 3048 ret = xbee_tx_rx( "ATFR", value ,0 );
tukudani 0:65f9be2858c9 3049 if( ret == 0){
tukudani 0:65f9be2858c9 3050 #ifdef LCD_H
tukudani 0:65f9be2858c9 3051 lcd_cls();
tukudani 0:65f9be2858c9 3052 #ifdef H3694
tukudani 0:65f9be2858c9 3053 led_red( 1 );
tukudani 0:65f9be2858c9 3054 #endif
tukudani 0:65f9be2858c9 3055 lcd_putstr( "EXIT:CANNOT RESET XBEE" );
tukudani 0:65f9be2858c9 3056 #endif
tukudani 0:65f9be2858c9 3057 #ifdef H3694
tukudani 0:65f9be2858c9 3058 return(0);
tukudani 0:65f9be2858c9 3059 #elif ARDUINO
tukudani 0:65f9be2858c9 3060 return(0);
tukudani 0:65f9be2858c9 3061 #else
tukudani 0:65f9be2858c9 3062 exit(-1);
tukudani 0:65f9be2858c9 3063 #endif
tukudani 0:65f9be2858c9 3064 }
tukudani 0:65f9be2858c9 3065 wait_millisec(3000); // リセット指示後3秒後に起動
tukudani 0:65f9be2858c9 3066 sci_clear(); // 再起動のメッセージをクリア
tukudani 0:65f9be2858c9 3067 // while( xbee_at_rx( value ) == 0 ); // パケットの破棄(永久ループの懸念がある)
tukudani 0:65f9be2858c9 3068 value[0] = 0x01; // API MODE=1に設定
tukudani 0:65f9be2858c9 3069 xbee_tx_rx("ATAP", value , 1 );
tukudani 0:65f9be2858c9 3070 value[0] = 0x05; // RSSI LEDを点灯
tukudani 0:65f9be2858c9 3071 xbee_tx_rx("ATP0", value , 1 );
tukudani 0:65f9be2858c9 3072 wait_millisec(500);
tukudani 0:65f9be2858c9 3073 value[0] = 0x01; // RSSI LEDを受信強度に戻す
tukudani 0:65f9be2858c9 3074 xbee_tx_rx("ATP0", value , 1 );
tukudani 0:65f9be2858c9 3075 #endif
tukudani 0:65f9be2858c9 3076 #endif // LITE
tukudani 0:65f9be2858c9 3077 return( ret );
tukudani 0:65f9be2858c9 3078 }
tukudani 0:65f9be2858c9 3079
tukudani 0:65f9be2858c9 3080 byte xbee_myaddress( byte *address ){
tukudani 0:65f9be2858c9 3081 /*
tukudani 0:65f9be2858c9 3082 自分自身のIEEEアドレスを取得する / XBee Wifi(PC)の場合は設定する
tukudani 0:65f9be2858c9 3083 byte *address : IEEEアドレスを代入する
tukudani 0:65f9be2858c9 3084 戻り値=1で正常読み込み、2は書き込み。0は異常
tukudani 0:65f9be2858c9 3085 */
tukudani 0:65f9be2858c9 3086 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3087 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3088 byte i=0;
tukudani 0:65f9be2858c9 3089 byte ret=0;
tukudani 0:65f9be2858c9 3090
tukudani 0:65f9be2858c9 3091 data[0]=0x00;
tukudani 0:65f9be2858c9 3092 /* ショートアドレスの取得はしないことにする。(Coordinatorは常に00)
tukudani 0:65f9be2858c9 3093 if( xbee_tx_rx( "ATMY",data,0) ){
tukudani 0:65f9be2858c9 3094 for(i=0;i<2;i++){
tukudani 0:65f9be2858c9 3095 SADR_MY[i]=data[8+i];
tukudani 0:65f9be2858c9 3096 }
tukudani 0:65f9be2858c9 3097 }
tukudani 0:65f9be2858c9 3098 */
tukudani 0:65f9be2858c9 3099 data[0]=0x00;
tukudani 0:65f9be2858c9 3100 if( xbee_tx_rx( "ATSH",data,0) ){
tukudani 0:65f9be2858c9 3101 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 3102 address[i]=data[8+i];
tukudani 0:65f9be2858c9 3103 }
tukudani 0:65f9be2858c9 3104 data[0]=0x00;
tukudani 0:65f9be2858c9 3105 if( xbee_tx_rx( "ATSL",data,0) ){
tukudani 0:65f9be2858c9 3106 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 3107 address[4+i]=data[8+i];
tukudani 0:65f9be2858c9 3108 }
tukudani 0:65f9be2858c9 3109 ret=1;
tukudani 0:65f9be2858c9 3110 }
tukudani 0:65f9be2858c9 3111 }
tukudani 0:65f9be2858c9 3112 #ifdef LCD_H
tukudani 0:65f9be2858c9 3113 if( ret==0 ) xbee_log( 5, "ERROR: at at_my" , 0 );
tukudani 0:65f9be2858c9 3114 xbee_log( 1, "done:xbee_myaddress" , ret );
tukudani 0:65f9be2858c9 3115 #endif
tukudani 0:65f9be2858c9 3116 return( ret );
tukudani 0:65f9be2858c9 3117 #else // XBEE_WIFI /******* ADR_MYに登録されているIPアドレスを設定する **********/
tukudani 0:65f9be2858c9 3118 #ifdef ARDUINO
tukudani 0:65f9be2858c9 3119 byte i=0;
tukudani 0:65f9be2858c9 3120 IPAddress ip = Ethernet.localIP();
tukudani 0:65f9be2858c9 3121 for(i=0;i<4;i++) ADR_MY[i] = ip[i];
tukudani 0:65f9be2858c9 3122 for(i=0;i<4;i++) address[i] = ADR_MY[i];
tukudani 0:65f9be2858c9 3123 return( 1 );
tukudani 0:65f9be2858c9 3124 #else // PC
tukudani 0:65f9be2858c9 3125 byte i;
tukudani 0:65f9be2858c9 3126 byte ret=2;
tukudani 0:65f9be2858c9 3127 for(i=0;i<4;i++) if( ADR_MY[i] != 0xFF ) ret=1;
tukudani 0:65f9be2858c9 3128 if(ret==1) for(i=0;i<4;i++) address[i]=ADR_MY[i]; // ADR_MYが設定されているときは読み込む
tukudani 0:65f9be2858c9 3129 else for(i=0;i<4;i++) ADR_MY[i]=address[i]; // ADR_MYが全てFFの時は入力addressを設定する。
tukudani 0:65f9be2858c9 3130 #ifdef DEBUG
tukudani 0:65f9be2858c9 3131 printf("ADR_MY=");
tukudani 0:65f9be2858c9 3132 for(i=0;i<4;i++) printf("%d.",(int)ADR_MY[i]);
tukudani 0:65f9be2858c9 3133 printf("\b\n");
tukudani 0:65f9be2858c9 3134 #endif
tukudani 0:65f9be2858c9 3135 return( ret );
tukudani 0:65f9be2858c9 3136 #endif
tukudani 0:65f9be2858c9 3137 #endif
tukudani 0:65f9be2858c9 3138 }
tukudani 0:65f9be2858c9 3139
tukudani 0:65f9be2858c9 3140 void xbee_address(const byte *address){
tukudani 0:65f9be2858c9 3141 /*
tukudani 0:65f9be2858c9 3142 送信用の宛先アドレス設定用の関数
tukudani 0:65f9be2858c9 3143 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 3144 */
tukudani 0:65f9be2858c9 3145 byte i;
tukudani 0:65f9be2858c9 3146
tukudani 0:65f9be2858c9 3147 #ifndef XBEE_WIFI // XBee ZB用
tukudani 0:65f9be2858c9 3148 for(i=0; i< 8 ;i++ ) ADR_DEST[i] = address[i];
tukudani 0:65f9be2858c9 3149 #ifndef ARDUINO
tukudani 0:65f9be2858c9 3150 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 3151 SADR_DEST[0] = 0xFF;
tukudani 0:65f9be2858c9 3152 SADR_DEST[1] = 0xFE;
tukudani 0:65f9be2858c9 3153 #endif
tukudani 0:65f9be2858c9 3154 #endif
tukudani 0:65f9be2858c9 3155 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3156 #ifdef ARDUINO
tukudani 0:65f9be2858c9 3157 for( i=0;i<4;i++) ADR_DEST[i] = address[i];
tukudani 0:65f9be2858c9 3158 #else
tukudani 0:65f9be2858c9 3159 if( bytecmp(ADR_DEST,address,4) != 0 ){
tukudani 0:65f9be2858c9 3160 close_serial_port_tx();
tukudani 0:65f9be2858c9 3161 open_serial_port_tx( address );
tukudani 0:65f9be2858c9 3162 for( i=0;i<4;i++) ADR_DEST[i] = address[i];
tukudani 0:65f9be2858c9 3163 }else{
tukudani 0:65f9be2858c9 3164 #ifdef LCD_H
tukudani 0:65f9be2858c9 3165 xbee_log( 2, "same address" , address[7] );
tukudani 0:65f9be2858c9 3166 #endif
tukudani 0:65f9be2858c9 3167 }
tukudani 0:65f9be2858c9 3168 #endif
tukudani 0:65f9be2858c9 3169 #endif
tukudani 0:65f9be2858c9 3170
tukudani 0:65f9be2858c9 3171 #ifdef DEBUG
tukudani 0:65f9be2858c9 3172 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3173 lcd_cls(); lcd_goto(LCD_ROW_1); lcd_putstr("DEST=");
tukudani 0:65f9be2858c9 3174 for(i=4;i<8;i++){ lcd_disp_hex( ADR_DEST[i] ); lcd_putstr(" "); }
tukudani 0:65f9be2858c9 3175 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3176 printf("DEST=");
tukudani 0:65f9be2858c9 3177 for(i=0;i<4;i++) printf("%d.",(int)ADR_DEST[i]);
tukudani 0:65f9be2858c9 3178 printf("\b\n");
tukudani 0:65f9be2858c9 3179 #endif
tukudani 0:65f9be2858c9 3180 #endif
tukudani 0:65f9be2858c9 3181 #ifdef LCD_H
tukudani 0:65f9be2858c9 3182 xbee_log( 1, "done:xbee_address" , address[7] );
tukudani 0:65f9be2858c9 3183 #endif
tukudani 0:65f9be2858c9 3184 }
tukudani 0:65f9be2858c9 3185
tukudani 0:65f9be2858c9 3186 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3187 #ifndef ARDUINO
tukudani 0:65f9be2858c9 3188 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 3189 void xbee_short_address(const byte *address){
tukudani 0:65f9be2858c9 3190 SADR_DEST[0] = address[0];
tukudani 0:65f9be2858c9 3191 SADR_DEST[1] = address[1];
tukudani 0:65f9be2858c9 3192 }
tukudani 0:65f9be2858c9 3193 #endif
tukudani 0:65f9be2858c9 3194 #endif
tukudani 0:65f9be2858c9 3195 #endif
tukudani 0:65f9be2858c9 3196
tukudani 0:65f9be2858c9 3197 byte xbee_atd( const byte *address ){
tukudani 0:65f9be2858c9 3198 /*
tukudani 0:65f9be2858c9 3199 送信用の宛先アドレス設定用の関数 XBeeへの設定
tukudani 0:65f9be2858c9 3200 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 3201 戻り値:戻り値=1で正常、0は異常
tukudani 0:65f9be2858c9 3202 */
tukudani 0:65f9be2858c9 3203 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3204 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3205 byte i;
tukudani 0:65f9be2858c9 3206 byte ret=0;
tukudani 0:65f9be2858c9 3207
tukudani 0:65f9be2858c9 3208 for(i=0;i<4;i++)data[i]=address[i];
tukudani 0:65f9be2858c9 3209 if( xbee_tx_rx( "ATDH",data,4) ){
tukudani 0:65f9be2858c9 3210 for(i=0;i<4;i++)data[i]=address[i+4];
tukudani 0:65f9be2858c9 3211 if( xbee_tx_rx( "ATDL",data,4) ){
tukudani 0:65f9be2858c9 3212 ret=1;
tukudani 0:65f9be2858c9 3213 }
tukudani 0:65f9be2858c9 3214 }
tukudani 0:65f9be2858c9 3215 return( ret );
tukudani 0:65f9be2858c9 3216 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3217 return( 0 );
tukudani 0:65f9be2858c9 3218 #endif
tukudani 0:65f9be2858c9 3219 }
tukudani 0:65f9be2858c9 3220
tukudani 0:65f9be2858c9 3221 byte xbee_ratd(const byte *dev_address, const byte *set_address ){
tukudani 0:65f9be2858c9 3222 /*
tukudani 0:65f9be2858c9 3223 指定したアドレスのXBee子機に任意のアドレスを(宛先として)設定する
tukudani 0:65f9be2858c9 3224 byte *dev_address : 設定先のXBeeデバイスのアドレス
tukudani 0:65f9be2858c9 3225 byte *set_address : 設定するアドレス
tukudani 0:65f9be2858c9 3226 戻り値=XBeeデバイス名。0xFFは異常
tukudani 0:65f9be2858c9 3227 */
tukudani 0:65f9be2858c9 3228 byte i=0;
tukudani 0:65f9be2858c9 3229 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 3230 byte len=0;
tukudani 0:65f9be2858c9 3231 #endif
tukudani 0:65f9be2858c9 3232 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3233 byte dd=0xFF; // デバイス名
tukudani 0:65f9be2858c9 3234
tukudani 0:65f9be2858c9 3235 xbee_address(dev_address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 3236 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3237 for( i=0;i<4;i++) data[i]=set_address[i]; // 上位4バイトをdataに代入
tukudani 0:65f9be2858c9 3238 if( xbee_tx_rx( "RATDH", data ,4 ) > 0 ){ // 上位4バイトを設定
tukudani 0:65f9be2858c9 3239 for( i=0;i<4;i++) data[i]=set_address[i+4]; // 下位4バイトをdataに代入
tukudani 0:65f9be2858c9 3240 if( xbee_tx_rx( "RATDL", data ,4 ) > 0 ){ // 下位4バイトを設定
tukudani 0:65f9be2858c9 3241 if( xbee_tx_rx( "RATDD", data ,0 ) > 0 ){ // デバイス名を取得
tukudani 0:65f9be2858c9 3242 if( data[18]==0x00 && data[19]==0x03 && data[20]==0x00 ){ // XBeeデバイス
tukudani 0:65f9be2858c9 3243 dd=data[21]; // デバイス名をddに代入
tukudani 0:65f9be2858c9 3244 }
tukudani 0:65f9be2858c9 3245 }else{
tukudani 0:65f9be2858c9 3246 #ifdef LCD_H
tukudani 0:65f9be2858c9 3247 xbee_log( 4, "ERR:tx_rx RATDD at ratd" , 1 );
tukudani 0:65f9be2858c9 3248 #endif
tukudani 0:65f9be2858c9 3249 }
tukudani 0:65f9be2858c9 3250 }else{
tukudani 0:65f9be2858c9 3251 #ifdef LCD_H
tukudani 0:65f9be2858c9 3252 xbee_log( 4, "ERR:tx_rx RATDL at ratd" , 1 );
tukudani 0:65f9be2858c9 3253 #endif
tukudani 0:65f9be2858c9 3254 }
tukudani 0:65f9be2858c9 3255 }else{
tukudani 0:65f9be2858c9 3256 #ifdef LCD_H
tukudani 0:65f9be2858c9 3257 xbee_log( 4, "ERR:tx_rx RATDH at ratd" , 1 );
tukudani 0:65f9be2858c9 3258 #endif
tukudani 0:65f9be2858c9 3259 }
tukudani 0:65f9be2858c9 3260 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3261 for( i=0;i<4;i++){
tukudani 0:65f9be2858c9 3262 if(set_address[i] >= 100){
tukudani 0:65f9be2858c9 3263 data[len] = (byte)(set_address[i]/100);
tukudani 0:65f9be2858c9 3264 data[len+1] = (byte)(set_address[i]/10)-10*data[len]+(byte)'0';
tukudani 0:65f9be2858c9 3265 data[len] += (byte)'0';
tukudani 0:65f9be2858c9 3266 len += 2;
tukudani 0:65f9be2858c9 3267 }else if(set_address[i] >= 10){
tukudani 0:65f9be2858c9 3268 data[len] = (byte)(set_address[i]/10)+(byte)'0';
tukudani 0:65f9be2858c9 3269 len += 1;
tukudani 0:65f9be2858c9 3270 }
tukudani 0:65f9be2858c9 3271 data[len] = (byte)(set_address[i]%10)+(byte)'0';
tukudani 0:65f9be2858c9 3272 data[len+1] = (byte)'.'; // 最後のデータdata[len]に「.」が入るが
tukudani 0:65f9be2858c9 3273 len += 2;
tukudani 0:65f9be2858c9 3274 }
tukudani 0:65f9be2858c9 3275 len--; // 最後の「.」は送信しない。
tukudani 0:65f9be2858c9 3276 if( xbee_tx_rx( "RATDL", data ,len ) > 0 ){ // 下位4バイトを設定
tukudani 0:65f9be2858c9 3277 dd = 0x00;
tukudani 0:65f9be2858c9 3278 }else{
tukudani 0:65f9be2858c9 3279 #ifdef LCD_H
tukudani 0:65f9be2858c9 3280 xbee_log( 4, "ERR:tx_rx RATDL at ratd" , 1 );
tukudani 0:65f9be2858c9 3281 #endif
tukudani 0:65f9be2858c9 3282 }
tukudani 0:65f9be2858c9 3283 #endif
tukudani 0:65f9be2858c9 3284 return(dd);
tukudani 0:65f9be2858c9 3285 }
tukudani 0:65f9be2858c9 3286
tukudani 0:65f9be2858c9 3287 byte xbee_ratd_myaddress(const byte *address){
tukudani 0:65f9be2858c9 3288 /*
tukudani 0:65f9be2858c9 3289 指定したアドレスのXBee子機に本機のアドレスを(宛先として)設定する
tukudani 0:65f9be2858c9 3290 byte *address : 設定先のXBeeデバイスのアドレス
tukudani 0:65f9be2858c9 3291 戻り値=XBeeデバイス名。0xFFは異常
tukudani 0:65f9be2858c9 3292
tukudani 0:65f9be2858c9 3293 */
tukudani 0:65f9be2858c9 3294 byte i=0;
tukudani 0:65f9be2858c9 3295 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 3296 byte len=0;
tukudani 0:65f9be2858c9 3297 #endif
tukudani 0:65f9be2858c9 3298 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3299 byte adr_my[8];
tukudani 0:65f9be2858c9 3300 byte dd=0xFF; // デバイス名
tukudani 0:65f9be2858c9 3301
tukudani 0:65f9be2858c9 3302 if( xbee_myaddress(adr_my) ){ // 自分のアドレスを取得
tukudani 0:65f9be2858c9 3303 #ifdef DEBUG
tukudani 0:65f9be2858c9 3304 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3305 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 3306 _xbee_debug.printf("adr_my =");
tukudani 0:65f9be2858c9 3307 for( i=0 ; i<8 ; i++) _xbee_debug.printf("%02x ",adr_my[i]);
tukudani 0:65f9be2858c9 3308 _xbee_debug.printf("\b\n");
tukudani 0:65f9be2858c9 3309 #else // PC
tukudani 0:65f9be2858c9 3310 printf("adr_my =");
tukudani 0:65f9be2858c9 3311 for( i=0 ; i<8 ; i++) printf("%02x ",adr_my[i]);
tukudani 0:65f9be2858c9 3312 printf("\b\n");
tukudani 0:65f9be2858c9 3313 #endif
tukudani 0:65f9be2858c9 3314 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3315 printf("adr_my =");
tukudani 0:65f9be2858c9 3316 for( i=0 ; i<4 ; i++) printf("%d.",adr_my[i]);
tukudani 0:65f9be2858c9 3317 printf("\b\n");
tukudani 0:65f9be2858c9 3318 #endif
tukudani 0:65f9be2858c9 3319 #endif
tukudani 0:65f9be2858c9 3320 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 3321 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3322 for( i=0;i<4;i++) data[i]=adr_my[i]; // 上位4バイトをdataに代入
tukudani 0:65f9be2858c9 3323 if( xbee_tx_rx( "RATDH", data ,4 ) > 0 ){ // 上位4バイトを設定
tukudani 0:65f9be2858c9 3324 for( i=0;i<4;i++) data[i]=adr_my[i+4]; // 下位4バイトをdataに代入
tukudani 0:65f9be2858c9 3325 if( xbee_tx_rx( "RATDL", data ,4 ) > 0 ){ // 下位4バイトを設定
tukudani 0:65f9be2858c9 3326 if( xbee_tx_rx( "RATDD", data ,0 ) > 0 ){ // デバイス名を取得
tukudani 0:65f9be2858c9 3327 if( data[18]==0x00 && data[19]==0x03 && data[20]==0x00 ){ // XBeeデバイス
tukudani 0:65f9be2858c9 3328 dd=data[21]; // デバイス名をddに代入
tukudani 0:65f9be2858c9 3329 }
tukudani 0:65f9be2858c9 3330 }else{
tukudani 0:65f9be2858c9 3331 #ifdef LCD_H
tukudani 0:65f9be2858c9 3332 xbee_log( 4, "ERR:tx_rx RATDD at set_myadd" , 1 );
tukudani 0:65f9be2858c9 3333 #endif
tukudani 0:65f9be2858c9 3334 }
tukudani 0:65f9be2858c9 3335 }else{
tukudani 0:65f9be2858c9 3336 #ifdef LCD_H
tukudani 0:65f9be2858c9 3337 xbee_log( 4, "ERR:tx_rx RATDL at set_myadd" , 1 );
tukudani 0:65f9be2858c9 3338 #endif
tukudani 0:65f9be2858c9 3339 }
tukudani 0:65f9be2858c9 3340 }else{
tukudani 0:65f9be2858c9 3341 #ifdef LCD_H
tukudani 0:65f9be2858c9 3342 xbee_log( 4, "ERR:tx_rx RATDH at set_myadd" , 1 );
tukudani 0:65f9be2858c9 3343 #endif
tukudani 0:65f9be2858c9 3344 }
tukudani 0:65f9be2858c9 3345 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3346 for( i=0;i<4;i++){
tukudani 0:65f9be2858c9 3347 if(adr_my[i] >= 100){
tukudani 0:65f9be2858c9 3348 data[len] = (byte)(adr_my[i]/100);
tukudani 0:65f9be2858c9 3349 data[len+1] = (byte)(adr_my[i]/10)-10*data[len]+(byte)'0';
tukudani 0:65f9be2858c9 3350 data[len] += (byte)'0';
tukudani 0:65f9be2858c9 3351 len += 2;
tukudani 0:65f9be2858c9 3352 }else if(adr_my[i] >= 10){
tukudani 0:65f9be2858c9 3353 data[len] = (byte)(adr_my[i]/10)+(byte)'0';
tukudani 0:65f9be2858c9 3354 len += 1;
tukudani 0:65f9be2858c9 3355 }
tukudani 0:65f9be2858c9 3356 data[len] = (byte)(adr_my[i]%10)+(byte)'0';
tukudani 0:65f9be2858c9 3357 data[len+1] = (byte)'.'; // 最後のデータdata[len]に「.」が入るが
tukudani 0:65f9be2858c9 3358 len += 2;
tukudani 0:65f9be2858c9 3359 }
tukudani 0:65f9be2858c9 3360 len--; // 最後の「.」は送信しない。
tukudani 0:65f9be2858c9 3361 if( xbee_tx_rx( "RATDL", data ,len ) > 0 ){ // 下位4バイトを設定
tukudani 0:65f9be2858c9 3362 dd = 0x00;
tukudani 0:65f9be2858c9 3363 }else{
tukudani 0:65f9be2858c9 3364 #ifdef LCD_H
tukudani 0:65f9be2858c9 3365 xbee_log( 4, "ERR:tx_rx RATDL at set_myadd" , 1 );
tukudani 0:65f9be2858c9 3366 #endif
tukudani 0:65f9be2858c9 3367 }
tukudani 0:65f9be2858c9 3368 // XBee Wi-Fi 2.0だと情報をクラウドに送信してしまう。LAN内に送信するように変更する
tukudani 0:65f9be2858c9 3369 if(DEVICE_TYPE == XB_TYPE_WIFI20){
tukudani 0:65f9be2858c9 3370 data[0]=0x00; xbee_tx_rx( "RATDO", data ,1 ); // DO: XBee WiFi 2.0用 Device Option=0
tukudani 0:65f9be2858c9 3371 }
tukudani 0:65f9be2858c9 3372 #endif
tukudani 0:65f9be2858c9 3373 }else{
tukudani 0:65f9be2858c9 3374 #ifdef LCD_H
tukudani 0:65f9be2858c9 3375 xbee_log( 4, "ERR:at_my at set_myadd" , 1 );
tukudani 0:65f9be2858c9 3376 #endif
tukudani 0:65f9be2858c9 3377 }
tukudani 0:65f9be2858c9 3378 return(dd);
tukudani 0:65f9be2858c9 3379 }
tukudani 0:65f9be2858c9 3380
tukudani 0:65f9be2858c9 3381 void xbee_from(byte *address){
tukudani 0:65f9be2858c9 3382 /*
tukudani 0:65f9be2858c9 3383 最後に受信したデバイスの送信元アドレスの読み込み。
tukudani 0:65f9be2858c9 3384 但し、xbee_rx_callで受信した場合の送信元アドレスは
tukudani 0:65f9be2858c9 3385 「構造体XBEE_RESULT変数.FROM」で読むこと。
tukudani 0:65f9be2858c9 3386 (キャッシュ値での受信の場合があるので)
tukudani 0:65f9be2858c9 3387 出力:byte *address IEEEアドレス
tukudani 0:65f9be2858c9 3388 */
tukudani 0:65f9be2858c9 3389 byte i;
tukudani 0:65f9be2858c9 3390
tukudani 0:65f9be2858c9 3391 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3392 for(i=0; i< 8 ;i++ ) address[i] = ADR_FROM[i];
tukudani 0:65f9be2858c9 3393 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3394 for(i=0; i< 4 ;i++ ) address[i] = ADR_FROM[i];
tukudani 0:65f9be2858c9 3395 #endif
tukudani 0:65f9be2858c9 3396 #ifdef LCD_H
tukudani 0:65f9be2858c9 3397 xbee_log( 1, "done:xbee_from" , 0 );
tukudani 0:65f9be2858c9 3398 #endif
tukudani 0:65f9be2858c9 3399 }
tukudani 0:65f9be2858c9 3400
tukudani 0:65f9be2858c9 3401 byte text2hex4data( byte *data, const char *in , const byte com_len){
tukudani 0:65f9be2858c9 3402 /*
tukudani 0:65f9be2858c9 3403 data 出力:変換後の数値データ(バイナリデータ)
tukudani 0:65f9be2858c9 3404 in 入力:文字としての16進数(テキストデータ)
tukudani 0:65f9be2858c9 3405 com_len 入力:ATコマンドの文字数(ATを含む)。16進数に変換する部分以外の先頭文字数
tukudani 0:65f9be2858c9 3406 戻り値 出力:変換したバイト数
tukudani 0:65f9be2858c9 3407 */
tukudani 0:65f9be2858c9 3408 byte i, j, len, val;
tukudani 0:65f9be2858c9 3409
tukudani 0:65f9be2858c9 3410 /* ATコマンドに続く16進数の引数入力 */
tukudani 0:65f9be2858c9 3411 for( len = 0 ; len < API_SIZE ; len++ ){
tukudani 0:65f9be2858c9 3412 j = (byte)in[len * 2 + com_len];
tukudani 0:65f9be2858c9 3413 i = (byte)in[len * 2 + com_len + 1];
tukudani 0:65f9be2858c9 3414 if( j == (byte)'\0' || i == (byte)'\0' )break;
tukudani 0:65f9be2858c9 3415 // 16進数の文字を数字にしてdataに代入。1桁は考慮。奇数桁は考慮せずに処理。
tukudani 0:65f9be2858c9 3416 if( (j >= (byte)'0' && j <= (byte)'9') || (j >= (byte)'A' && j <= (byte)'F') ){
tukudani 0:65f9be2858c9 3417 if( j > '9' ) data[len] = j - (byte)'A' + 10;
tukudani 0:65f9be2858c9 3418 else data[len] = j - (byte)'0';
tukudani 0:65f9be2858c9 3419 if( (i >= (byte)'0' && i <= (byte)'9') || (i >= (byte)'A' && i <= (byte)'F') ){
tukudani 0:65f9be2858c9 3420 if( i > '9' ) val = i - (byte)'A' + 10;
tukudani 0:65f9be2858c9 3421 else val = i - (byte)'0';
tukudani 0:65f9be2858c9 3422 data[len] *= 16;
tukudani 0:65f9be2858c9 3423 data[len] += val;
tukudani 0:65f9be2858c9 3424 }
tukudani 0:65f9be2858c9 3425 }else break;
tukudani 0:65f9be2858c9 3426 }
tukudani 0:65f9be2858c9 3427 return( len );
tukudani 0:65f9be2858c9 3428 }
tukudani 0:65f9be2858c9 3429
tukudani 0:65f9be2858c9 3430 byte xbee_rat(const byte *address, const char *in){
tukudani 0:65f9be2858c9 3431 /*
tukudani 0:65f9be2858c9 3432 リモートATコマンドを送信するユーザ提供関数
tukudani 0:65f9be2858c9 3433 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 3434    char *in = 入力するATコマンド 例: ATDL0000FFFF 最大文字数は XB_AT_SIZE-1
tukudani 0:65f9be2858c9 3435 出力:ATコマンドの結果 STATUS_OK / STATUS_ERR / STATUS_ERR_AT / STATUS_ERR_PARM / STATUS_ERR_AIR
tukudani 0:65f9be2858c9 3436    0xFFで送信失敗もしくはデータ異常
tukudani 0:65f9be2858c9 3437 */
tukudani 0:65f9be2858c9 3438 byte i;
tukudani 0:65f9be2858c9 3439 char at[6];
tukudani 0:65f9be2858c9 3440 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3441 byte len;
tukudani 0:65f9be2858c9 3442 byte ret = 0xFF;
tukudani 0:65f9be2858c9 3443 byte com_len =0;
tukudani 0:65f9be2858c9 3444 byte null_adr[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
tukudani 0:65f9be2858c9 3445
tukudani 0:65f9be2858c9 3446 if( in[0]=='A' && in[1]=='T' ){
tukudani 0:65f9be2858c9 3447 if( bytecmp(null_adr,address,8) == 0 ){
tukudani 0:65f9be2858c9 3448 // xbee_atから呼ばれた場合 ATxx_;ローカルAT
tukudani 0:65f9be2858c9 3449 com_len = 4; // 入力ATコマンド長 ATxx 4
tukudani 0:65f9be2858c9 3450 at[0]='A'; at[1]='T';
tukudani 0:65f9be2858c9 3451 if( in[2] == '\0' ){
tukudani 0:65f9be2858c9 3452 at[2]='D'; at[3]='D';
tukudani 0:65f9be2858c9 3453 }else{
tukudani 0:65f9be2858c9 3454 at[2]=in[2]; at[3]=in[3];
tukudani 0:65f9be2858c9 3455 }
tukudani 0:65f9be2858c9 3456 at[4]='\0';
tukudani 0:65f9be2858c9 3457 }else{
tukudani 0:65f9be2858c9 3458 // リモートat(xbee_rat)指定で呼び出された場合 RATxx_;実際のリモートAT
tukudani 0:65f9be2858c9 3459 com_len = 4; // 入力ATコマンド長 ATxx 4
tukudani 0:65f9be2858c9 3460 at[0]='R'; at[1]='A'; at[2]='T';
tukudani 0:65f9be2858c9 3461 // アプリ呼び出しにつき、ATの後に何もない場合が想定される。その時はATDDとみなす
tukudani 0:65f9be2858c9 3462 if( in[2] == '\0' ){
tukudani 0:65f9be2858c9 3463 at[3]='D'; at[4]='D';
tukudani 0:65f9be2858c9 3464 }else{
tukudani 0:65f9be2858c9 3465 at[3]=in[2]; at[4]=in[3];
tukudani 0:65f9be2858c9 3466 }
tukudani 0:65f9be2858c9 3467 at[5]='\0';
tukudani 0:65f9be2858c9 3468 }
tukudani 0:65f9be2858c9 3469 }else if( in[0]=='R' && in[1]=='A' && in[2]=='T' ){
tukudani 0:65f9be2858c9 3470 com_len = 5; // 入力ATコマンド長 RATxx 5
tukudani 0:65f9be2858c9 3471 at[0]='R'; at[1]='A'; at[2]='T';
tukudani 0:65f9be2858c9 3472 at[3]=in[3]; at[4]=in[4];
tukudani 0:65f9be2858c9 3473 at[5]='\0';
tukudani 0:65f9be2858c9 3474 }else if( in[0]=='T' && in[1]=='X' ){
tukudani 0:65f9be2858c9 3475 com_len = 2; // 入力ATコマンド長 TX 2
tukudani 0:65f9be2858c9 3476 at[0]='T'; at[1]='X';
tukudani 0:65f9be2858c9 3477 at[2]='\0';
tukudani 0:65f9be2858c9 3478 }
tukudani 0:65f9be2858c9 3479 #ifndef ARDUINO
tukudani 0:65f9be2858c9 3480 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 3481 else if(in[0]=='Z' && in[1]=='C'){
tukudani 0:65f9be2858c9 3482 com_len = 2; // 入力ATコマンド長 TX 2
tukudani 0:65f9be2858c9 3483 at[0]='Z'; at[1]='C';
tukudani 0:65f9be2858c9 3484 at[2]='\0';
tukudani 0:65f9be2858c9 3485 if( SADR_DEST[0]==0xFF && SADR_DEST[1] == 0xFE) xbee_address(address);
tukudani 0:65f9be2858c9 3486 len = text2hex4data( data, in , com_len);
tukudani 0:65f9be2858c9 3487 if( xbee_at_tx( at, data ,len ) > 0 ) ret=0;
tukudani 0:65f9be2858c9 3488 com_len = 0;
tukudani 0:65f9be2858c9 3489 }
tukudani 0:65f9be2858c9 3490 #endif
tukudani 0:65f9be2858c9 3491 #endif
tukudani 0:65f9be2858c9 3492 if( com_len > 0 ){
tukudani 0:65f9be2858c9 3493 if( at[0] != 'A' && SADR_DEST[0] == 0xFF && SADR_DEST[1] == 0xFE) xbee_address(address);
tukudani 0:65f9be2858c9 3494 len = text2hex4data( data, in , com_len);
tukudani 0:65f9be2858c9 3495 // test
tukudani 0:65f9be2858c9 3496 // printf("xbee_tx_rx %s len=%d\n",at,len);
tukudani 0:65f9be2858c9 3497 #ifdef LCD_H
tukudani 0:65f9be2858c9 3498 xbee_log( 1, "started:xbee_rat" , len );
tukudani 0:65f9be2858c9 3499 #endif
tukudani 0:65f9be2858c9 3500 if( xbee_tx_rx( at, data ,len ) > 0 ){
tukudani 0:65f9be2858c9 3501 #ifdef LCD_H
tukudani 0:65f9be2858c9 3502 xbee_log( 1, "done:rat" , len );
tukudani 0:65f9be2858c9 3503 #endif
tukudani 0:65f9be2858c9 3504 if( data[3] == MODE_RES ){
tukudani 0:65f9be2858c9 3505 ret = data[7];
tukudani 0:65f9be2858c9 3506 }else if( data[3] == MODE_RESP ){
tukudani 0:65f9be2858c9 3507 for(i=0;i<8;i++) ADR_FROM[i]=data[5+i]; // 2013.9.15 追加
tukudani 0:65f9be2858c9 3508 ret = data[17];
tukudani 0:65f9be2858c9 3509 }else{
tukudani 0:65f9be2858c9 3510 ret = 0xFF;
tukudani 0:65f9be2858c9 3511 }
tukudani 0:65f9be2858c9 3512 }else{
tukudani 0:65f9be2858c9 3513 #ifdef LCD_H
tukudani 0:65f9be2858c9 3514 xbee_log( 4, "ERR:tx_rx at xbee_rat" , 1 );
tukudani 0:65f9be2858c9 3515 #endif
tukudani 0:65f9be2858c9 3516 }
tukudani 0:65f9be2858c9 3517 }
tukudani 0:65f9be2858c9 3518 #ifdef LCD_H
tukudani 0:65f9be2858c9 3519 xbee_log( 1, "done:xbee_rat" , ret );
tukudani 0:65f9be2858c9 3520 #endif
tukudani 0:65f9be2858c9 3521 return( ret );
tukudani 0:65f9be2858c9 3522 }
tukudani 0:65f9be2858c9 3523
tukudani 0:65f9be2858c9 3524 byte xbee_rat_force(const byte *address, const char *in){
tukudani 0:65f9be2858c9 3525 /*
tukudani 0:65f9be2858c9 3526 リモートATコマンドを送信するユーザ提供関数。応答値をxbee_rx_callで得る。
tukudani 0:65f9be2858c9 3527 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 3528    char *in = 入力するATコマンド 例: ATDL0000FFFF 最大文字数は XB_AT_SIZE-1
tukudani 0:65f9be2858c9 3529 出力:0x00で送信失敗もしくはデータ異常
tukudani 0:65f9be2858c9 3530 */
tukudani 0:65f9be2858c9 3531 char at[6];
tukudani 0:65f9be2858c9 3532 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3533 byte len;
tukudani 0:65f9be2858c9 3534 byte ret = 0x00;
tukudani 0:65f9be2858c9 3535
tukudani 0:65f9be2858c9 3536 if( in[0]=='A' && in[1]=='T' ){
tukudani 0:65f9be2858c9 3537 at[0]='S'; at[1]='A'; at[2]='T';
tukudani 0:65f9be2858c9 3538 at[3]=in[3]; at[4]=in[4];
tukudani 0:65f9be2858c9 3539 at[5]='\0';
tukudani 0:65f9be2858c9 3540 xbee_address(address);
tukudani 0:65f9be2858c9 3541 len = text2hex4data( data, in , 5 );
tukudani 0:65f9be2858c9 3542 #ifdef LCD_H
tukudani 0:65f9be2858c9 3543 xbee_log( 1, "started:xbee_ratf" , len );
tukudani 0:65f9be2858c9 3544 #endif
tukudani 0:65f9be2858c9 3545 if( xbee_at_tx( at, data ,len ) > 0 ){
tukudani 0:65f9be2858c9 3546 #ifdef LCD_H
tukudani 0:65f9be2858c9 3547 xbee_log( 1, "done:ratf" , len );
tukudani 0:65f9be2858c9 3548 #endif
tukudani 0:65f9be2858c9 3549 ret = PACKET_ID;
tukudani 0:65f9be2858c9 3550 }
tukudani 0:65f9be2858c9 3551 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 3552 if(ret){ // 送信データを即確定する
tukudani 0:65f9be2858c9 3553 at[0]='S'; at[1]='A'; at[2]='T';
tukudani 0:65f9be2858c9 3554 at[3]='A'; at[4]='C';
tukudani 0:65f9be2858c9 3555 xbee_at_tx( at, data ,0 );
tukudani 0:65f9be2858c9 3556 }
tukudani 0:65f9be2858c9 3557 #endif
tukudani 0:65f9be2858c9 3558 }
tukudani 0:65f9be2858c9 3559 #ifdef LCD_H
tukudani 0:65f9be2858c9 3560 xbee_log( 1, "done:xbee_rat_force" , 1 );
tukudani 0:65f9be2858c9 3561 #endif
tukudani 0:65f9be2858c9 3562 return( ret );
tukudani 0:65f9be2858c9 3563 }
tukudani 0:65f9be2858c9 3564
tukudani 0:65f9be2858c9 3565 byte xbee_at(const char *in){
tukudani 0:65f9be2858c9 3566 /*
tukudani 0:65f9be2858c9 3567 ローカルATコマンドを送信するユーザ提供関数。
tukudani 0:65f9be2858c9 3568 入力:char *in = 入力するATコマンド 例: ATDL0000FFFF 最大文字数は XB_AT_SIZE-1
tukudani 0:65f9be2858c9 3569 出力:ATコマンドの結果 STATUS_OK / STATUS_ERR / STATUS_ERR_AT / STATUS_ERR_PARM / STATUS_ERR_AIR
tukudani 0:65f9be2858c9 3570    0xFFで送信失敗もしくはデータ異常
tukudani 0:65f9be2858c9 3571    別関数xbee_atnjとエラー時の戻り値が異なる。
tukudani 0:65f9be2858c9 3572    atnjは正常が複数あるので異常時がNULL、atはエラーが複数あるので正常時がNULL
tukudani 0:65f9be2858c9 3573 */
tukudani 0:65f9be2858c9 3574 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3575 byte address[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
tukudani 0:65f9be2858c9 3576 byte ret;
tukudani 0:65f9be2858c9 3577 ret = xbee_rat( address, in);
tukudani 0:65f9be2858c9 3578 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 3579 byte ret;
tukudani 0:65f9be2858c9 3580 ret = xbee_rat( ADR_DEST, in);
tukudani 0:65f9be2858c9 3581 #endif
tukudani 0:65f9be2858c9 3582
tukudani 0:65f9be2858c9 3583 #ifdef LCD_H
tukudani 0:65f9be2858c9 3584 if( ret == 0xFF ) xbee_log( 4, "ERR:tx_rx at xbee_at" , ret );
tukudani 0:65f9be2858c9 3585 xbee_log( 1, "done:xbee_at" , 1 );
tukudani 0:65f9be2858c9 3586 #endif
tukudani 0:65f9be2858c9 3587 return( ret );
tukudani 0:65f9be2858c9 3588 }
tukudani 0:65f9be2858c9 3589
tukudani 0:65f9be2858c9 3590 byte xbee_uart(const byte *address, const char *in){
tukudani 0:65f9be2858c9 3591 /*
tukudani 0:65f9be2858c9 3592 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 3593    char *in = 送信するテキスト文字。最大文字数はAPI_TXSIZE-1
tukudani 0:65f9be2858c9 3594 出力:戻り値 = 送信パケット番号PACKET_ID。0x00は失敗。
tukudani 0:65f9be2858c9 3595 */
tukudani 0:65f9be2858c9 3596 byte ret=0;
tukudani 0:65f9be2858c9 3597
tukudani 0:65f9be2858c9 3598 xbee_address( address ); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 3599 if( xbee_putstr( in ) > 0 ) ret = PACKET_ID;
tukudani 0:65f9be2858c9 3600 #ifdef LCD_H
tukudani 0:65f9be2858c9 3601 xbee_log( 1, "done:xbee_uart" , 1 );
tukudani 0:65f9be2858c9 3602 #endif
tukudani 0:65f9be2858c9 3603 return( ret );
tukudani 0:65f9be2858c9 3604 }
tukudani 0:65f9be2858c9 3605
tukudani 0:65f9be2858c9 3606 byte xbee_bytes(const byte *address, const byte *in, byte len){
tukudani 0:65f9be2858c9 3607 /*
tukudani 0:65f9be2858c9 3608 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 3609    byte *in = 送信するバイナリ情報。最大文字数はAPI_TXSIZE-1
tukudani 0:65f9be2858c9 3610    byte len = バイト数
tukudani 0:65f9be2858c9 3611 出力:戻り値 = 送信パケット番号PACKET_ID。0x00は失敗。
tukudani 0:65f9be2858c9 3612 */
tukudani 0:65f9be2858c9 3613 byte ret=0;
tukudani 0:65f9be2858c9 3614
tukudani 0:65f9be2858c9 3615 #ifdef LCD_H
tukudani 0:65f9be2858c9 3616 xbee_log( 1, "started:xbee_bytes" , 1 );
tukudani 0:65f9be2858c9 3617 #endif
tukudani 0:65f9be2858c9 3618 xbee_address( address ); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 3619 if(len > API_TXSIZE-18 ){
tukudani 0:65f9be2858c9 3620 #ifdef LCD_H
tukudani 0:65f9be2858c9 3621 xbee_log( 3, "CAUTION:xbee_bytes length" , len );
tukudani 0:65f9be2858c9 3622 #endif
tukudani 0:65f9be2858c9 3623 len = API_TXSIZE-18;
tukudani 0:65f9be2858c9 3624 }
tukudani 0:65f9be2858c9 3625 if( xbee_at_tx( "TX", in , len) > 0 ) ret = PACKET_ID;
tukudani 0:65f9be2858c9 3626 #ifdef LCD_H
tukudani 0:65f9be2858c9 3627 xbee_log( 1, "done:xbee_bytes" , 1 );
tukudani 0:65f9be2858c9 3628 #endif
tukudani 0:65f9be2858c9 3629 return( ret );
tukudani 0:65f9be2858c9 3630 }
tukudani 0:65f9be2858c9 3631
tukudani 0:65f9be2858c9 3632 byte xbee_atvr( void ){
tukudani 0:65f9be2858c9 3633 /*
tukudani 0:65f9be2858c9 3634 XBeeのデバイスタイプを取得するコマンド
tukudani 0:65f9be2858c9 3635 出力:戻り値= define値 ZB_TYPE_COORD、ZB_TYPE_ROUTER、ZB_TYPE_ENDDEV、0xFF=エラー
tukudani 0:65f9be2858c9 3636 */
tukudani 0:65f9be2858c9 3637 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3638 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3639 byte ret=0xFF;
tukudani 0:65f9be2858c9 3640
tukudani 0:65f9be2858c9 3641 if( xbee_tx_rx( "ATVR", data ,0 ) > 0){
tukudani 0:65f9be2858c9 3642 DEVICE_TYPE = data[8];
tukudani 0:65f9be2858c9 3643 if( DEVICE_TYPE != ZB_TYPE_COORD &&
tukudani 0:65f9be2858c9 3644 DEVICE_TYPE != ZB_TYPE_ROUTER &&
tukudani 0:65f9be2858c9 3645 DEVICE_TYPE != ZB_TYPE_ENDDEV) ret = 0xFF;
tukudani 0:65f9be2858c9 3646 else ret = DEVICE_TYPE;
tukudani 0:65f9be2858c9 3647 }
tukudani 0:65f9be2858c9 3648 #ifdef LCD_H
tukudani 0:65f9be2858c9 3649 xbee_log( 1, "done:xbee_atvr" , ret );
tukudani 0:65f9be2858c9 3650 #endif
tukudani 0:65f9be2858c9 3651 return( ret );
tukudani 0:65f9be2858c9 3652 #else
tukudani 0:65f9be2858c9 3653 return( DEVICE_TYPE );
tukudani 0:65f9be2858c9 3654 #endif
tukudani 0:65f9be2858c9 3655 }
tukudani 0:65f9be2858c9 3656
tukudani 0:65f9be2858c9 3657 byte xbee_atcb( byte cb ){
tukudani 0:65f9be2858c9 3658 /*
tukudani 0:65f9be2858c9 3659 XBeeのコミッション制御
tukudani 0:65f9be2858c9 3660 入力:コミッションボタンの押下数(1:ジョイン開始、2:ジョイン許可、4:初期化)
tukudani 0:65f9be2858c9 3661 出力:戻り値= 0xFF 異常終了、 0x00 指示成功
tukudani 0:65f9be2858c9 3662 */
tukudani 0:65f9be2858c9 3663 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3664 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3665 byte ret = 0xFF;
tukudani 0:65f9be2858c9 3666
tukudani 0:65f9be2858c9 3667 data[0] = cb;
tukudani 0:65f9be2858c9 3668 if( cb == 1 || cb == 2 ){
tukudani 0:65f9be2858c9 3669 if( xbee_tx_rx( "ATCB", data ,1 ) > 0) ret = 0x00;
tukudani 0:65f9be2858c9 3670 }else if( cb == 4){
tukudani 0:65f9be2858c9 3671 if( xbee_tx_rx( "ATCB", data ,1 ) > 0){
tukudani 0:65f9be2858c9 3672 if( DEVICE_TYPE == ZB_TYPE_COORD ){
tukudani 0:65f9be2858c9 3673 wait_millisec(1000);
tukudani 0:65f9be2858c9 3674 sci_clear();
tukudani 0:65f9be2858c9 3675 wait_millisec(5000);
tukudani 0:65f9be2858c9 3676 xbee_at_rx( data );
tukudani 0:65f9be2858c9 3677 if( data[3] == MODE_MODM && data[4] == MODM_STARTED ){
tukudani 0:65f9be2858c9 3678 ret = 0x00;
tukudani 0:65f9be2858c9 3679 }
tukudani 0:65f9be2858c9 3680 }else ret = 0x00;
tukudani 0:65f9be2858c9 3681 }
tukudani 0:65f9be2858c9 3682 }
tukudani 0:65f9be2858c9 3683 #ifdef LCD_H
tukudani 0:65f9be2858c9 3684 xbee_log( 1, "done:xbee_atcb" , ret );
tukudani 0:65f9be2858c9 3685 #endif
tukudani 0:65f9be2858c9 3686 return( ret );
tukudani 0:65f9be2858c9 3687 #else
tukudani 0:65f9be2858c9 3688 return( 0xFF );
tukudani 0:65f9be2858c9 3689 #endif
tukudani 0:65f9be2858c9 3690 }
tukudani 0:65f9be2858c9 3691
tukudani 0:65f9be2858c9 3692 byte xbee_atai(void){
tukudani 0:65f9be2858c9 3693 /*
tukudani 0:65f9be2858c9 3694 XBeeモジュールの状態を確認するコマンド AT Association Indication
tukudani 0:65f9be2858c9 3695 出力:
tukudani 0:65f9be2858c9 3696 0x00 - Successfully formed or joined a network. (Coordinators form a network, routers
tukudani 0:65f9be2858c9 3697 and end devices join a network.)
tukudani 0:65f9be2858c9 3698 ※Routerに対して実行すると01が返ってくる場合があるが仕様書には記載なし。
tukudani 0:65f9be2858c9 3699 0x21 - Scan found no PANs
tukudani 0:65f9be2858c9 3700 0x22 - Scan found no valid PANs based on current SC and ID settings
tukudani 0:65f9be2858c9 3701 0x23 - Valid Coordinator or Routers found, but they are not allowing joining (NJ expired)
tukudani 0:65f9be2858c9 3702 0x24 - No joinable beacons were found
tukudani 0:65f9be2858c9 3703 0x25 - Unexpected state, node should not be attempting to join at this time
tukudani 0:65f9be2858c9 3704 0x27 - Node Joining attempt failed (typically due to incompatible security settings)
tukudani 0:65f9be2858c9 3705 0x2A - Coordinator Start attempt failed‘
tukudani 0:65f9be2858c9 3706 0x2B - Checking for an existing coordinator
tukudani 0:65f9be2858c9 3707 0x2C - Attempt to leave the network failed
tukudani 0:65f9be2858c9 3708 0xAB - Attempted to join a device that did not respond.
tukudani 0:65f9be2858c9 3709 0xAC - Secure join error - network security key received unsecured
tukudani 0:65f9be2858c9 3710 0xAD - Secure join error - network security key not received
tukudani 0:65f9be2858c9 3711 0xAF - Secure join error - joining device does not have the right preconfigured link key
tukudani 0:65f9be2858c9 3712 0xFF - Scanning for a ZigBee network (routers and end devices)
tukudani 0:65f9be2858c9 3713
tukudani 0:65f9be2858c9 3714 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 3715    本コマンドをXBeeに送ると、XBeeからの応答用キャッシュがクリアされる場合があります。
tukudani 0:65f9be2858c9 3716    例えば、xbee_forceなどの応答を後から得るコマンドを実行後に本コマンドを実行すると
tukudani 0:65f9be2858c9 3717    xbee_rx_callで応答が受け取れなくなる場合があります。
tukudani 0:65f9be2858c9 3718    このような場合、xbee_ataiを実行してからxbee_forceを実行するような手順にしてください。
tukudani 0:65f9be2858c9 3719 */
tukudani 0:65f9be2858c9 3720 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3721 byte ret=0xFF;
tukudani 0:65f9be2858c9 3722
tukudani 0:65f9be2858c9 3723 #ifdef LCD_H
tukudani 0:65f9be2858c9 3724 xbee_log( 1, "started:xbee_atai" , 0x00 );
tukudani 0:65f9be2858c9 3725 #endif
tukudani 0:65f9be2858c9 3726 if( xbee_tx_rx( "ATAI", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3727 // wait_millisec(100);
tukudani 0:65f9be2858c9 3728 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3729 if( data[7] == 0x00 ) ret = data[8];
tukudani 0:65f9be2858c9 3730 #else
tukudani 0:65f9be2858c9 3731 if( data[17] == 0x00 ) ret = data[18];
tukudani 0:65f9be2858c9 3732 #endif
tukudani 0:65f9be2858c9 3733 }
tukudani 0:65f9be2858c9 3734 #ifdef LCD_H
tukudani 0:65f9be2858c9 3735 xbee_log( 1, "done:xbee_atai" , ret );
tukudani 0:65f9be2858c9 3736 #endif
tukudani 0:65f9be2858c9 3737 return( ret );
tukudani 0:65f9be2858c9 3738 }
tukudani 0:65f9be2858c9 3739
tukudani 0:65f9be2858c9 3740 unsigned short xbee_atop(byte *pan_id){
tukudani 0:65f9be2858c9 3741 /*
tukudani 0:65f9be2858c9 3742 PAN Idを求めるコマンド(書き込めないD
tukudani 0:65f9be2858c9 3743 出力:byte pan_id[8] PAN_ID 64bit (8bytes)
tukudani 0:65f9be2858c9 3744    unsigned short 戻り値 = PAN_ID 16bit
tukudani 0:65f9be2858c9 3745 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 3746 */
tukudani 0:65f9be2858c9 3747 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3748 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3749 byte i;
tukudani 0:65f9be2858c9 3750 unsigned short pan_id_s=0xFFFF;
tukudani 0:65f9be2858c9 3751
tukudani 0:65f9be2858c9 3752 for( i=0 ; i<8 ; i++ ) pan_id[i] = 0x00;
tukudani 0:65f9be2858c9 3753 #ifdef LCD_H
tukudani 0:65f9be2858c9 3754 xbee_log( 1, "started:xbee_atop" , 0x00 );
tukudani 0:65f9be2858c9 3755 #endif
tukudani 0:65f9be2858c9 3756 if( xbee_tx_rx( "ATOP", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3757 wait_millisec(100);
tukudani 0:65f9be2858c9 3758 if( data[7] == 0x00 ){
tukudani 0:65f9be2858c9 3759 for( i=0 ; i<8 ; i++ ) pan_id[i] = data[8+i];
tukudani 0:65f9be2858c9 3760 }
tukudani 0:65f9be2858c9 3761 xbee_tx_rx( "ATOI", data ,0 );
tukudani 0:65f9be2858c9 3762 if( data[7] == 0x00 ){
tukudani 0:65f9be2858c9 3763 pan_id_s = (unsigned short)data[8] * 16 + (unsigned short)data[9];
tukudani 0:65f9be2858c9 3764 }
tukudani 0:65f9be2858c9 3765 // wait_millisec(100);
tukudani 0:65f9be2858c9 3766 }
tukudani 0:65f9be2858c9 3767 #ifdef LCD_H
tukudani 0:65f9be2858c9 3768 xbee_log( 1, "done:xbee_atop" , 0x00 );
tukudani 0:65f9be2858c9 3769 #endif
tukudani 0:65f9be2858c9 3770 return( pan_id_s );
tukudani 0:65f9be2858c9 3771 #else
tukudani 0:65f9be2858c9 3772 return( 0x00 );
tukudani 0:65f9be2858c9 3773 #endif
tukudani 0:65f9be2858c9 3774 }
tukudani 0:65f9be2858c9 3775
tukudani 0:65f9be2858c9 3776 byte xbee_atee_on(const char *key ){
tukudani 0:65f9be2858c9 3777 /*
tukudani 0:65f9be2858c9 3778 暗号化通信の設定(コーディネータ、ルータ共用)
tukudani 0:65f9be2858c9 3779 入力:const char *key セキュリティ共通キー16文字まで
tukudani 0:65f9be2858c9 3780 出力:byte 戻り値 = 0xFF失敗 0x00書き込み 0x01何もせず(設定されていた)
tukudani 0:65f9be2858c9 3781 */
tukudani 0:65f9be2858c9 3782 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3783 byte i;
tukudani 0:65f9be2858c9 3784 byte ret = 0xFF;
tukudani 0:65f9be2858c9 3785 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3786
tukudani 0:65f9be2858c9 3787 /* 現在のモードを確認(あとでATWRする。フラッシュ書き換え回数低減のため) */
tukudani 0:65f9be2858c9 3788 // xbee_tx_rx( "ATVR", data ,0 );
tukudani 0:65f9be2858c9 3789 // vr = data[8]; // xbee VRを取得
tukudani 0:65f9be2858c9 3790 if( xbee_tx_rx( "ATEE", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3791 if( data[8] == 0x00 ){ // ATEEの応答が00の時
tukudani 0:65f9be2858c9 3792 data[0] = 0x01; // ATEE = 0x01に設定
tukudani 0:65f9be2858c9 3793 if( xbee_tx_rx( "ATEE", data ,1 ) > 0 ){
tukudani 0:65f9be2858c9 3794 if( DEVICE_TYPE == ZB_TYPE_COORD ){
tukudani 0:65f9be2858c9 3795 wait_millisec(1000);
tukudani 0:65f9be2858c9 3796 sci_clear();
tukudani 0:65f9be2858c9 3797 wait_millisec(5000);
tukudani 0:65f9be2858c9 3798 xbee_at_rx( data );
tukudani 0:65f9be2858c9 3799 if( data[3] == MODE_MODM && data[4] == MODM_STARTED ){
tukudani 0:65f9be2858c9 3800 data[0] = 0x00; // ATNK = 0x00 random network key
tukudani 0:65f9be2858c9 3801 ret = xbee_tx_rx( "ATNK", data ,1 );
tukudani 0:65f9be2858c9 3802 }
tukudani 0:65f9be2858c9 3803 }else ret = 0x01;
tukudani 0:65f9be2858c9 3804 /* KY 設定 */
tukudani 0:65f9be2858c9 3805 for(i=0;i<16;i++){
tukudani 0:65f9be2858c9 3806 if( key[i] == '\0' ) break;
tukudani 0:65f9be2858c9 3807 data[i] = (byte)key[i];
tukudani 0:65f9be2858c9 3808 } // ここで使ったiはキーの文字数として使用する
tukudani 0:65f9be2858c9 3809 if( i == 0 ){
tukudani 0:65f9be2858c9 3810 #ifdef LCD_H
tukudani 0:65f9be2858c9 3811 xbee_log( 3, "CAUTION:ATKY=" , 0 );
tukudani 0:65f9be2858c9 3812 #endif
tukudani 0:65f9be2858c9 3813 data[0] = 0x00;
tukudani 0:65f9be2858c9 3814 i = 1;
tukudani 0:65f9be2858c9 3815 }
tukudani 0:65f9be2858c9 3816 if( xbee_tx_rx( "ATKY", data ,i ) > 0 ){
tukudani 0:65f9be2858c9 3817 if( xbee_tx_rx( "ATWR", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3818 if( ret != 0xFF ) ret = 0x00;
tukudani 0:65f9be2858c9 3819 }
tukudani 0:65f9be2858c9 3820 }
tukudani 0:65f9be2858c9 3821 }
tukudani 0:65f9be2858c9 3822 }else ret = 0x01;
tukudani 0:65f9be2858c9 3823 }
tukudani 0:65f9be2858c9 3824 #ifdef LCD_H
tukudani 0:65f9be2858c9 3825 xbee_log( 1, "done:xbee_atee_on" , ret );
tukudani 0:65f9be2858c9 3826 #endif
tukudani 0:65f9be2858c9 3827 return(ret);
tukudani 0:65f9be2858c9 3828 #else
tukudani 0:65f9be2858c9 3829 return( 0x00 );
tukudani 0:65f9be2858c9 3830 #endif
tukudani 0:65f9be2858c9 3831 }
tukudani 0:65f9be2858c9 3832
tukudani 0:65f9be2858c9 3833 byte xbee_atee_off( void ){
tukudani 0:65f9be2858c9 3834 /*
tukudani 0:65f9be2858c9 3835 暗号化通信のオフ(コーディネータ、ルータ共用)
tukudani 0:65f9be2858c9 3836 出力:byte 戻り値 = 0xFF失敗 0x00書き込み 0x01何もせず(設定されていた)
tukudani 0:65f9be2858c9 3837 */
tukudani 0:65f9be2858c9 3838 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3839 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3840 byte ret = 0xFF;
tukudani 0:65f9be2858c9 3841
tukudani 0:65f9be2858c9 3842 if( xbee_tx_rx( "ATEE", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3843 if( data[8] == 0x01 ){ // ATEEの応答が01(enable)の時
tukudani 0:65f9be2858c9 3844 data[0] = 0x00; // ATEE = 0x00に設定
tukudani 0:65f9be2858c9 3845 if( xbee_tx_rx( "ATEE", data ,1 ) > 0 ){
tukudani 0:65f9be2858c9 3846 if( DEVICE_TYPE == ZB_TYPE_COORD ){ // ZigBeeコーディネータの場合は再起動がある
tukudani 0:65f9be2858c9 3847 wait_millisec(1000);
tukudani 0:65f9be2858c9 3848 sci_clear();
tukudani 0:65f9be2858c9 3849 wait_millisec(5000);
tukudani 0:65f9be2858c9 3850 xbee_at_rx( data );
tukudani 0:65f9be2858c9 3851 if( data[3] == MODE_MODM && data[4] == MODM_STARTED ){
tukudani 0:65f9be2858c9 3852 data[0] = 0x00; // ATNK = 0x00 random network key
tukudani 0:65f9be2858c9 3853 ret = xbee_tx_rx( "ATNK", data ,1 );
tukudani 0:65f9be2858c9 3854 }
tukudani 0:65f9be2858c9 3855 }else ret = 0x01;
tukudani 0:65f9be2858c9 3856 if( xbee_tx_rx( "ATWR", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3857 if( ret != 0xFF ) ret = 0x00;
tukudani 0:65f9be2858c9 3858 }
tukudani 0:65f9be2858c9 3859 }
tukudani 0:65f9be2858c9 3860 }else ret= 0x01;
tukudani 0:65f9be2858c9 3861 }
tukudani 0:65f9be2858c9 3862 #ifdef LCD_H
tukudani 0:65f9be2858c9 3863 xbee_log( 1, "done:xbee_atee_off" , ret );
tukudani 0:65f9be2858c9 3864 #endif
tukudani 0:65f9be2858c9 3865 return(ret);
tukudani 0:65f9be2858c9 3866 #else
tukudani 0:65f9be2858c9 3867 return( 0x00 );
tukudani 0:65f9be2858c9 3868 #endif
tukudani 0:65f9be2858c9 3869 }
tukudani 0:65f9be2858c9 3870
tukudani 0:65f9be2858c9 3871 byte xbee_atnc(void){
tukudani 0:65f9be2858c9 3872 /*
tukudani 0:65f9be2858c9 3873 XBeeモジュールのEnd Device子機のジョイン可能な台数を取得するコマンドです。
tukudani 0:65f9be2858c9 3874 出力:ジョイン可能数(0~)、0xFFで取得エラー
tukudani 0:65f9be2858c9 3875
tukudani 0:65f9be2858c9 3876 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 3877 */
tukudani 0:65f9be2858c9 3878 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3879 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3880 byte ret=0xFF;
tukudani 0:65f9be2858c9 3881 if( xbee_tx_rx( "ATNC", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3882 if( data[7] == 0x00 ) ret = data[8];
tukudani 0:65f9be2858c9 3883 }
tukudani 0:65f9be2858c9 3884 #ifdef LCD_H
tukudani 0:65f9be2858c9 3885 if(ret<3) xbee_log( 3, "CAUTION:Many Children" , ret );
tukudani 0:65f9be2858c9 3886 if(ret==0) xbee_log( 3, "CAUTION:Cannot Join More" , ret );
tukudani 0:65f9be2858c9 3887 xbee_log( 1, "done:xbee_atnc" , ret );
tukudani 0:65f9be2858c9 3888 #endif
tukudani 0:65f9be2858c9 3889 return( ret );
tukudani 0:65f9be2858c9 3890 #else
tukudani 0:65f9be2858c9 3891 return( 0 );
tukudani 0:65f9be2858c9 3892 #endif
tukudani 0:65f9be2858c9 3893 }
tukudani 0:65f9be2858c9 3894
tukudani 0:65f9be2858c9 3895 byte xbee_ratnc( const byte *address ){
tukudani 0:65f9be2858c9 3896 /*
tukudani 0:65f9be2858c9 3897 XBeeモジュールのEnd Device子機のジョイン可能な台数を取得するコマンドです。
tukudani 0:65f9be2858c9 3898 出力:ジョイン可能数(0~)、0xFFで取得エラー
tukudani 0:65f9be2858c9 3899
tukudani 0:65f9be2858c9 3900 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 3901    本コマンドをXBeeに送ると、XBeeからの応答用キャッシュがクリアされる場合があります。
tukudani 0:65f9be2858c9 3902 */
tukudani 0:65f9be2858c9 3903 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3904 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3905 byte ret=0xFF;
tukudani 0:65f9be2858c9 3906
tukudani 0:65f9be2858c9 3907 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 3908 if( xbee_tx_rx( "RATNC", data ,0 ) > 0 ){
tukudani 0:65f9be2858c9 3909 if( data[17] == 0x00 ) ret = data[18];
tukudani 0:65f9be2858c9 3910 }
tukudani 0:65f9be2858c9 3911 #ifdef LCD_H
tukudani 0:65f9be2858c9 3912 if(ret<3) xbee_log( 3, "CAUTION:Many Children" , ret );
tukudani 0:65f9be2858c9 3913 xbee_log( 1, "done:xbee_ratnc" , ret );
tukudani 0:65f9be2858c9 3914 #endif
tukudani 0:65f9be2858c9 3915 return( ret );
tukudani 0:65f9be2858c9 3916 #else
tukudani 0:65f9be2858c9 3917 return( 0 );
tukudani 0:65f9be2858c9 3918 #endif
tukudani 0:65f9be2858c9 3919 }
tukudani 0:65f9be2858c9 3920
tukudani 0:65f9be2858c9 3921 byte xbee_atnj(const byte timeout){
tukudani 0:65f9be2858c9 3922 /*
tukudani 0:65f9be2858c9 3923 制限時間timeoutで設定した間にネットワークに参加したデバイスを通信相手に設定する関数
tukudani 0:65f9be2858c9 3924 AT Node Join Time
tukudani 0:65f9be2858c9 3925 入力:timeout = 時間(6~254秒)、0x00で参加拒否、0xFFの場合は常に参加許可
tukudani 0:65f9be2858c9 3926 出力:byte 戻り値 = 0x00失敗、成功時はMODE_IDNT(0x95)が代入される
tukudani 0:65f9be2858c9 3927    xbee_atとエラー時の戻り値が異なる。
tukudani 0:65f9be2858c9 3928    atnjは正常が複数あるので異常時がNULL
tukudani 0:65f9be2858c9 3929    また、atnjはfopenと似た記述になるので、特殊な応答でもない(と思う)
tukudani 0:65f9be2858c9 3930
tukudani 0:65f9be2858c9 3931 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 3932 */
tukudani 0:65f9be2858c9 3933 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 3934 byte data[API_SIZE];
tukudani 0:65f9be2858c9 3935 byte i=0, ret, timer;
tukudani 0:65f9be2858c9 3936
tukudani 0:65f9be2858c9 3937 #ifdef LCD_H
tukudani 0:65f9be2858c9 3938 xbee_log( 1, "started:xbee_atnj" , timeout );
tukudani 0:65f9be2858c9 3939 #endif
tukudani 0:65f9be2858c9 3940 data[0]=timeout;
tukudani 0:65f9be2858c9 3941 ret = xbee_tx_rx( "ATNJ", data ,1 );
tukudani 0:65f9be2858c9 3942 if( ret > 0 ){
tukudani 0:65f9be2858c9 3943 wait_millisec(100);
tukudani 0:65f9be2858c9 3944 if( timeout == 0xFF ){
tukudani 0:65f9be2858c9 3945 /*常に参加待ち受けするテスト専用(非実用)*/
tukudani 0:65f9be2858c9 3946 ret=0xFF;
tukudani 0:65f9be2858c9 3947 #ifdef LCD_H
tukudani 0:65f9be2858c9 3948 xbee_log( 3, "CAUTION:ATNJ=" , timeout );
tukudani 0:65f9be2858c9 3949 #endif
tukudani 0:65f9be2858c9 3950 wait_millisec(1000); // NJ値の書込み時間待ち
tukudani 0:65f9be2858c9 3951 }else{
tukudani 0:65f9be2858c9 3952 if( timeout > 0x09 ){
tukudani 0:65f9be2858c9 3953 /*参加待ち受けの開始*/
tukudani 0:65f9be2858c9 3954 timer = TIMER_SEC + timeout + 0x01 ;
tukudani 0:65f9be2858c9 3955 ret = 0x00;
tukudani 0:65f9be2858c9 3956 while( timer != TIMER_SEC && ret != MODE_IDNT){ // MODE_IDNT(0x95)はネットワーク参加
tukudani 0:65f9be2858c9 3957 #ifdef H3694
tukudani 0:65f9be2858c9 3958 if( (timera()>>6)&0x01 ){
tukudani 0:65f9be2858c9 3959 led_green( 0 ); // LEDのトグル1秒に4回
tukudani 0:65f9be2858c9 3960 } else {
tukudani 0:65f9be2858c9 3961 led_green( 1 );
tukudani 0:65f9be2858c9 3962 }
tukudani 0:65f9be2858c9 3963 #endif
tukudani 0:65f9be2858c9 3964 if( xbee_at_rx( data ) > 0x00 ){ // データの受信(戻り値は受信種別mode値)
tukudani 0:65f9be2858c9 3965 ret = xbee_from_acum(data);
tukudani 0:65f9be2858c9 3966 if( ret == MODE_IDNT ){ // 受信モードがMODE_IDNT(0x95) Node Identifyの場合
tukudani 0:65f9be2858c9 3967 #ifdef LCD_H
tukudani 0:65f9be2858c9 3968 xbee_log( 1, "Found device " , ret );
tukudani 0:65f9be2858c9 3969 #endif
tukudani 0:65f9be2858c9 3970 for(i=0;i<8;i++) ADR_DEST[i]=ADR_FROM[i]; // 宛先を発見デバイスに設定
tukudani 0:65f9be2858c9 3971 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 3972 sci_write_check();
tukudani 0:65f9be2858c9 3973 #endif
tukudani 0:65f9be2858c9 3974 sci_clear();
tukudani 0:65f9be2858c9 3975 }else ret=0x00;
tukudani 0:65f9be2858c9 3976 }
tukudani 0:65f9be2858c9 3977 }
tukudani 0:65f9be2858c9 3978 #ifdef LCD_H
tukudani 0:65f9be2858c9 3979 if( ret == 0x00 ) xbee_log( 1, "no new device" , ret );
tukudani 0:65f9be2858c9 3980 #endif
tukudani 0:65f9be2858c9 3981 }
tukudani 0:65f9be2858c9 3982 /*ジョイン拒否設定*/
tukudani 0:65f9be2858c9 3983 data[0]=0x00;
tukudani 0:65f9be2858c9 3984 xbee_tx_rx( "ATNJ", data ,1 );
tukudani 0:65f9be2858c9 3985 #ifdef LCD_H
tukudani 0:65f9be2858c9 3986 xbee_log( 1, "done:atnj=0x00" , 0x00 );
tukudani 0:65f9be2858c9 3987 #endif
tukudani 0:65f9be2858c9 3988 }
tukudani 0:65f9be2858c9 3989 }else{
tukudani 0:65f9be2858c9 3990 #ifdef LCD_H
tukudani 0:65f9be2858c9 3991 xbee_log( 5, "ERROR:xbee_atnj" , ret );
tukudani 0:65f9be2858c9 3992 #endif
tukudani 0:65f9be2858c9 3993 }
tukudani 0:65f9be2858c9 3994 #ifdef LCD_H
tukudani 0:65f9be2858c9 3995 xbee_log( 1, "done:xbee_atnj" , ret );
tukudani 0:65f9be2858c9 3996 #endif
tukudani 0:65f9be2858c9 3997 return( ret );
tukudani 0:65f9be2858c9 3998 #else
tukudani 0:65f9be2858c9 3999 return( 0x95 );
tukudani 0:65f9be2858c9 4000 #endif
tukudani 0:65f9be2858c9 4001 }
tukudani 0:65f9be2858c9 4002
tukudani 0:65f9be2858c9 4003 byte xbee_ratnj(const byte *address, const byte timeout){
tukudani 0:65f9be2858c9 4004 /*
tukudani 0:65f9be2858c9 4005 前記xbee_atnjをリモート先(子機)に対して設定する関数
tukudani 0:65f9be2858c9 4006 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 4007 入力:timeout = 0でジョイン拒否、1~254で待ち秒数、255で常時ジョイン許可
tukudani 0:65f9be2858c9 4008 出力:byte 戻り値 = 0x00失敗、0xFF成功
tukudani 0:65f9be2858c9 4009
tukudani 0:65f9be2858c9 4010 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4011 */
tukudani 0:65f9be2858c9 4012 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 4013 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4014 byte ret=0x00;
tukudani 0:65f9be2858c9 4015
tukudani 0:65f9be2858c9 4016 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4017 data[0]=timeout;
tukudani 0:65f9be2858c9 4018 if( xbee_tx_rx( "RATNJ", data ,1 ) > 0 ){
tukudani 0:65f9be2858c9 4019 #ifdef LCD_H
tukudani 0:65f9be2858c9 4020 xbee_log( 1, "done:ratnj" , xbee_tx_rx( "ATAC", data ,0 ) );
tukudani 0:65f9be2858c9 4021 #endif
tukudani 0:65f9be2858c9 4022 ret=0xFF;
tukudani 0:65f9be2858c9 4023 }
tukudani 0:65f9be2858c9 4024 #ifdef LCD_H
tukudani 0:65f9be2858c9 4025 xbee_log( 1, "done:xbee_ratnj" , ret );
tukudani 0:65f9be2858c9 4026 #endif
tukudani 0:65f9be2858c9 4027 return( ret );
tukudani 0:65f9be2858c9 4028 #else
tukudani 0:65f9be2858c9 4029 return( 0x95 );
tukudani 0:65f9be2858c9 4030 #endif
tukudani 0:65f9be2858c9 4031 }
tukudani 0:65f9be2858c9 4032
tukudani 0:65f9be2858c9 4033 byte xbee_ping(const byte *address ){
tukudani 0:65f9be2858c9 4034 /*
tukudani 0:65f9be2858c9 4035 入力:IEEEアドレスaddress
tukudani 0:65f9be2858c9 4036 出力: 0xFF エラー
tukudani 0:65f9be2858c9 4037 DEV_TYPE_XBEE 0x00 // XBeeモジュール
tukudani 0:65f9be2858c9 4038 DEV_TYPE_RS232 0x05 // RS-232Cアダプタ
tukudani 0:65f9be2858c9 4039 DEV_TYPE_SENS 0x07 // Sensor (1wire専用)
tukudani 0:65f9be2858c9 4040 DEV_TYPE_PLUG 0x0F // SmartPlug
tukudani 0:65f9be2858c9 4041
tukudani 0:65f9be2858c9 4042 byte 戻り値 = 0xFF失敗、成功時は0x00などのデバイス名(ATDD)の下2桁
tukudani 0:65f9be2858c9 4043
tukudani 0:65f9be2858c9 4044 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4045 */
tukudani 0:65f9be2858c9 4046 byte i;
tukudani 0:65f9be2858c9 4047 byte ret=0xFF; // デバイス名
tukudani 0:65f9be2858c9 4048 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4049
tukudani 0:65f9be2858c9 4050 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4051 if( xbee_tx_rx( "RATDD", data ,0 ) > 0 ){ // デバイス名を取得
tukudani 0:65f9be2858c9 4052 #ifdef XBEE_WIFI // Device Type Identifier. Stores a device type value
tukudani 0:65f9be2858c9 4053 // 20 00 20 24 ← 電源投入した後は、こんな応答がある。モジュールによらず。
tukudani 0:65f9be2858c9 4054 // 00 90 00 00 仕様書に書かれていないと思う
tukudani 0:65f9be2858c9 4055 if( (data[18]==0x00 && data[19]==0x09 && data[20]==0x00 && data[21]==0x00) ||
tukudani 0:65f9be2858c9 4056 (data[18]==0x20 && data[19]==0x00 && data[20]==0x20 && data[21]==0x24) ){ // XBee WiFiデバイス
tukudani 0:65f9be2858c9 4057 data[21]=0x00;
tukudani 0:65f9be2858c9 4058 #else
tukudani 0:65f9be2858c9 4059 if( data[18]==0x00 && data[19]==0x03 && data[20]==0x00 ){ // XBeeデバイス
tukudani 0:65f9be2858c9 4060 #endif
tukudani 0:65f9be2858c9 4061 for(i=0;i<8;i++) ADR_FROM[i]=data[5+i]; // 2013.9.15 追加
tukudani 0:65f9be2858c9 4062 ret=data[21]; // デバイス名をddに代入
tukudani 0:65f9be2858c9 4063 }
tukudani 0:65f9be2858c9 4064 #ifdef DEBUG
tukudani 0:65f9be2858c9 4065 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 4066 for(i=18;i<=21;i++) _xbee_debug.printf("%2X ",data[i]);
tukudani 0:65f9be2858c9 4067 _xbee_debug.printf("\n");
tukudani 0:65f9be2858c9 4068 #else //PC
tukudani 0:65f9be2858c9 4069 for(i=18;i<=21;i++) printf("%2X ",data[i]);
tukudani 0:65f9be2858c9 4070 printf("\n");
tukudani 0:65f9be2858c9 4071 #endif
tukudani 0:65f9be2858c9 4072 #endif
tukudani 0:65f9be2858c9 4073 }
tukudani 0:65f9be2858c9 4074 #ifdef LCD_H
tukudani 0:65f9be2858c9 4075 xbee_log( 1, "done:xbee_ping" , ret );
tukudani 0:65f9be2858c9 4076 #endif
tukudani 0:65f9be2858c9 4077 #ifdef XBEE_WIFI_DEBUG
tukudani 0:65f9be2858c9 4078 Serial.print("DD=");
tukudani 0:65f9be2858c9 4079 for(i=18;i<=21;i++){
tukudani 0:65f9be2858c9 4080 Serial.print(data[i],HEX);
tukudani 0:65f9be2858c9 4081 Serial.print(" ");
tukudani 0:65f9be2858c9 4082 }
tukudani 0:65f9be2858c9 4083 Serial.println("");
tukudani 0:65f9be2858c9 4084 #endif
tukudani 0:65f9be2858c9 4085 return( ret );
tukudani 0:65f9be2858c9 4086 }
tukudani 0:65f9be2858c9 4087
tukudani 0:65f9be2858c9 4088 int xbee_batt(const byte *address ){
tukudani 0:65f9be2858c9 4089 /*
tukudani 0:65f9be2858c9 4090 電池電圧の確認
tukudani 0:65f9be2858c9 4091 入力:IEEEアドレスaddress
tukudani 0:65f9be2858c9 4092 出力:約2100~3600[mV]、-1=エラー
tukudani 0:65f9be2858c9 4093 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4094 */
tukudani 0:65f9be2858c9 4095 int ret=-1;
tukudani 0:65f9be2858c9 4096 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4097
tukudani 0:65f9be2858c9 4098 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4099 if( xbee_tx_rx( "RAT%V", data ,0 ) > 0 ){ // 電池電圧を取得
tukudani 0:65f9be2858c9 4100 ret = (int)(((float)data[18] * 256.0f + (float)data[19] ) * 1.17f);
tukudani 0:65f9be2858c9 4101 }
tukudani 0:65f9be2858c9 4102 #ifdef LCD_H
tukudani 0:65f9be2858c9 4103 xbee_log( 1, "done:xbee_batt" , (byte)(ret/100) );
tukudani 0:65f9be2858c9 4104 #endif
tukudani 0:65f9be2858c9 4105 return( ret );
tukudani 0:65f9be2858c9 4106 }
tukudani 0:65f9be2858c9 4107
tukudani 0:65f9be2858c9 4108 byte xbee_batt_force(const byte *address ){
tukudani 0:65f9be2858c9 4109 /*
tukudani 0:65f9be2858c9 4110 電池電圧の確認
tukudani 0:65f9be2858c9 4111 入力:IEEEアドレスaddress
tukudani 0:65f9be2858c9 4112 xbee_batt_forceで測定指示を出して、xbee_rx_callで受け取ります。
tukudani 0:65f9be2858c9 4113 XBEE_RESULT.MODEに「MODE_BATT(0xE1)」が設定されるので他のリモートATレスポンスと区別することが出来ます。
tukudani 0:65f9be2858c9 4114 電圧値はXBEE_RESULT.ADC[0]に約2100~3600[mV]の範囲で応答します。
tukudani 0:65f9be2858c9 4115 */
tukudani 0:65f9be2858c9 4116 byte data[XB_AT_SIZE];
tukudani 0:65f9be2858c9 4117 byte ret = 0x00;
tukudani 0:65f9be2858c9 4118
tukudani 0:65f9be2858c9 4119 xbee_address( address ); // 送信先を指定
tukudani 0:65f9be2858c9 4120 ret = xbee_at_tx("RAT%V", data , 0); // ATISを送信
tukudani 0:65f9be2858c9 4121 if( ret > 0 ) ret = PACKET_ID;
tukudani 0:65f9be2858c9 4122 #ifdef LCD_H
tukudani 0:65f9be2858c9 4123 xbee_log( 1, "done:xbee_batt_force" , ret );
tukudani 0:65f9be2858c9 4124 #endif
tukudani 0:65f9be2858c9 4125 return ( ret );
tukudani 0:65f9be2858c9 4126 }
tukudani 0:65f9be2858c9 4127
tukudani 0:65f9be2858c9 4128 #ifdef ARDUINO
tukudani 0:65f9be2858c9 4129 byte xbee_gpio_config(const byte *address, const byte port, byte type ){
tukudani 0:65f9be2858c9 4130 #else
tukudani 0:65f9be2858c9 4131 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 4132 byte xbee_gpio_config(const byte *address, const byte port, byte type ){
tukudani 0:65f9be2858c9 4133 #else
tukudani 0:65f9be2858c9 4134 byte xbee_gpio_config(const byte *address, const byte port, const enum xbee_port_type type ){
tukudani 0:65f9be2858c9 4135 #endif
tukudani 0:65f9be2858c9 4136 #endif
tukudani 0:65f9be2858c9 4137 /*
tukudani 0:65f9be2858c9 4138 入力:IEEEアドレスaddress、GPIOポート番号port、設定値type
tukudani 0:65f9be2858c9 4139 enum xbee_port_type{ DISABLE=0, VENDER=1, AIN=2, DIN=3, DOUT_L=4, DOUT_H=5 };
tukudani 0:65f9be2858c9 4140
tukudani 0:65f9be2858c9 4141 port: port指定 IO名 ピン番号 共用 USB評価ボード(XBIB-U-Dev)
tukudani 0:65f9be2858c9 4142 port= 0 DIO0 XBee_pin 20 (Cms) SW Commision コミッションボタン
tukudani 0:65f9be2858c9 4143 port= 1 DIO1 XBee_pin 19 (AD1) SW2 汎用入力用(DIN or AIN)
tukudani 0:65f9be2858c9 4144 port= 2 DIO2 XBee_pin 18 (AD2) SW3 汎用入力用(DIN or AIN)
tukudani 0:65f9be2858c9 4145 port= 3 DIO3 XBee_pin 17 (AD3) SW4 汎用入力用(DIN or AIN)
tukudani 0:65f9be2858c9 4146 port= 4 DIO4 XBee_pin 16 (RTS) LED3 汎用出力用
tukudani 0:65f9be2858c9 4147 port= 5 DIO5 XBee_pin 15 (Ass) ASSOSIATE indication
tukudani 0:65f9be2858c9 4148 port=10 DIO10 XBee_pin 6 (RSSI) RSSI indication (PWM)
tukudani 0:65f9be2858c9 4149 port=11 DIO11 XBee_pin 7 LED2 汎用出力用
tukudani 0:65f9be2858c9 4150 port=12 DIO12 XBee_pin 4 LED1 汎用出力用
tukudani 0:65f9be2858c9 4151
tukudani 0:65f9be2858c9 4152 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4153 */
tukudani 0:65f9be2858c9 4154 byte ret=0xFF;
tukudani 0:65f9be2858c9 4155 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4156 char at[] = "RATD0" ; // ATコマンド
tukudani 0:65f9be2858c9 4157 byte dd=0xFF; // デバイス名
tukudani 0:65f9be2858c9 4158
tukudani 0:65f9be2858c9 4159 switch( type ){
tukudani 0:65f9be2858c9 4160 case DISABLE:
tukudani 0:65f9be2858c9 4161 if( (port >= 1 && port <= 5) || (port >= 10 && port <= 12) ) {
tukudani 0:65f9be2858c9 4162 at[4] = '0' + port;
tukudani 0:65f9be2858c9 4163 ret=0;
tukudani 0:65f9be2858c9 4164 }
tukudani 0:65f9be2858c9 4165 break;
tukudani 0:65f9be2858c9 4166 case VENDER:
tukudani 0:65f9be2858c9 4167 if( port == 0 || port == 5 || port == 10) ret=0;
tukudani 0:65f9be2858c9 4168 break;
tukudani 0:65f9be2858c9 4169 case AIN:
tukudani 0:65f9be2858c9 4170 if( port >= 1 && port <= 4 ){
tukudani 0:65f9be2858c9 4171 at[4] = '0' + port;
tukudani 0:65f9be2858c9 4172 ret=0;
tukudani 0:65f9be2858c9 4173 }else if( port == 0xFF ){
tukudani 0:65f9be2858c9 4174 ret=0;
tukudani 0:65f9be2858c9 4175 }
tukudani 0:65f9be2858c9 4176 break;
tukudani 0:65f9be2858c9 4177 case DIN:
tukudani 0:65f9be2858c9 4178 case DOUT_L:
tukudani 0:65f9be2858c9 4179 case DOUT_H:
tukudani 0:65f9be2858c9 4180 if( port >= 1 && port <= 5 ){
tukudani 0:65f9be2858c9 4181 at[4] = '0' + port;
tukudani 0:65f9be2858c9 4182 ret=0;
tukudani 0:65f9be2858c9 4183 }else if( port >= 10 && port <= 12 ){
tukudani 0:65f9be2858c9 4184 at[3] = 'P';
tukudani 0:65f9be2858c9 4185 at[4] = '0' + (port - 10);
tukudani 0:65f9be2858c9 4186 ret=0;
tukudani 0:65f9be2858c9 4187 }else if( port == 0xFF ){
tukudani 0:65f9be2858c9 4188 ret=0;
tukudani 0:65f9be2858c9 4189 }
tukudani 0:65f9be2858c9 4190 break;
tukudani 0:65f9be2858c9 4191 default:
tukudani 0:65f9be2858c9 4192 break;
tukudani 0:65f9be2858c9 4193 }
tukudani 0:65f9be2858c9 4194 if( ret==0 ){
tukudani 0:65f9be2858c9 4195 dd = xbee_ratd_myaddress(address);
tukudani 0:65f9be2858c9 4196 if( dd != 0xFF ){ // アドレス設定が正常に実施された時
tukudani 0:65f9be2858c9 4197 /*
tukudani 0:65f9be2858c9 4198 入力されたtypeがDINの時にRATICを変更する処理に対応予定(将来)
tukudani 0:65f9be2858c9 4199 RATICで状態を取得して状態or設定を追加(ポート1~5と10~12)
tukudani 0:65f9be2858c9 4200 */
tukudani 0:65f9be2858c9 4201 data[0]=(byte)type;
tukudani 0:65f9be2858c9 4202 xbee_tx_rx( at , data ,1 );
tukudani 0:65f9be2858c9 4203 }
tukudani 0:65f9be2858c9 4204 }
tukudani 0:65f9be2858c9 4205 #ifdef LCD_H
tukudani 0:65f9be2858c9 4206 xbee_log( 1, "done:xbee_gpio_config" , dd );
tukudani 0:65f9be2858c9 4207 #endif
tukudani 0:65f9be2858c9 4208 return( dd );
tukudani 0:65f9be2858c9 4209 }
tukudani 0:65f9be2858c9 4210
tukudani 0:65f9be2858c9 4211 byte xbee_gpio_init(const byte *address){
tukudani 0:65f9be2858c9 4212 /*
tukudani 0:65f9be2858c9 4213 子機XBeeデバイス(リモート先)のGPIOを入力に設定する関数
tukudani 0:65f9be2858c9 4214 リモート端末の宛先を本機アドレスに設定(リモート側のボタンで親機に送信するために)
tukudani 0:65f9be2858c9 4215 入力:なし
tukudani 0:65f9be2858c9 4216 出力:デバイス名(ATDDの下2桁)、FFでエラー
tukudani 0:65f9be2858c9 4217 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4218 */
tukudani 0:65f9be2858c9 4219 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4220 byte dd; // デバイス名
tukudani 0:65f9be2858c9 4221
tukudani 0:65f9be2858c9 4222 dd = xbee_ratd_myaddress(address);
tukudani 0:65f9be2858c9 4223 if( dd != 0xFF){
tukudani 0:65f9be2858c9 4224 data[0]=0x01; xbee_tx_rx( "RATD0", data ,1 ); // ポート0をコミッションボタンに設定
tukudani 0:65f9be2858c9 4225 switch(dd){
tukudani 0:65f9be2858c9 4226 case DEV_TYPE_XBEE:
tukudani 0:65f9be2858c9 4227 data[0]=0x03; xbee_tx_rx( "RATD1", data ,1 ); // ポート1をデジタル入力に設定
tukudani 0:65f9be2858c9 4228 data[0]=0x03; xbee_tx_rx( "RATD2", data ,1 ); // ポート2をデジタル入力に設定
tukudani 0:65f9be2858c9 4229 data[0]=0x03; xbee_tx_rx( "RATD3", data ,1 ); // ポート3をデジタル入力に設定
tukudani 0:65f9be2858c9 4230 data[0]=0x05; xbee_tx_rx( "RATD4", data ,1 ); // ポート4をデジタル出力に設定
tukudani 0:65f9be2858c9 4231 data[0]=0x05; xbee_tx_rx( "RATP1", data ,1 ); // ポート11をデジタル出力に設定
tukudani 0:65f9be2858c9 4232 data[0]=0x05; xbee_tx_rx( "RATP2", data ,1 ); // ポート12をデジタル出力に設定
tukudani 0:65f9be2858c9 4233 data[0]=0x00;
tukudani 0:65f9be2858c9 4234 data[1]=0x0E; // ポート1~3の↓
tukudani 0:65f9be2858c9 4235 xbee_tx_rx( "RATIC", data ,2 ); // 入力値が変化したら通知するIC設定
tukudani 0:65f9be2858c9 4236 break;
tukudani 0:65f9be2858c9 4237 case DEV_TYPE_PLUG:
tukudani 0:65f9be2858c9 4238 data[0]=0x02; xbee_tx_rx( "RATD3", data ,1 ); // ポート3をアナログ入力に設定
tukudani 0:65f9be2858c9 4239 data[0]=0x04; xbee_tx_rx( "RATD4", data ,1 ); // ポート4をデジタル出力に設定
tukudani 0:65f9be2858c9 4240 // breakしない
tukudani 0:65f9be2858c9 4241 case DEV_TYPE_WALL:
tukudani 0:65f9be2858c9 4242 data[0]=0x02; xbee_tx_rx( "RATD1", data ,1 ); // ポート1をアナログ入力に設定
tukudani 0:65f9be2858c9 4243 data[0]=0x02; xbee_tx_rx( "RATD2", data ,1 ); // ポート2をアナログ入力に設定
tukudani 0:65f9be2858c9 4244 break;
tukudani 0:65f9be2858c9 4245 default:
tukudani 0:65f9be2858c9 4246 #ifdef LCD_H
tukudani 0:65f9be2858c9 4247 xbee_log( 4, "ERR:gpio_init DD" , dd );
tukudani 0:65f9be2858c9 4248 #endif
tukudani 0:65f9be2858c9 4249 break;
tukudani 0:65f9be2858c9 4250 }
tukudani 0:65f9be2858c9 4251 }
tukudani 0:65f9be2858c9 4252 #ifdef LCD_H
tukudani 0:65f9be2858c9 4253 xbee_log( 1, "done:gpio init" , dd );
tukudani 0:65f9be2858c9 4254 #endif
tukudani 0:65f9be2858c9 4255 return( dd );
tukudani 0:65f9be2858c9 4256 }
tukudani 0:65f9be2858c9 4257
tukudani 0:65f9be2858c9 4258 byte xbee_gpo(const byte *address, const byte port,const byte out){
tukudani 0:65f9be2858c9 4259 /*
tukudani 0:65f9be2858c9 4260 子機XBeeデバイス(リモート先)のGPIOへ出力する関数
tukudani 0:65f9be2858c9 4261 portは1~4と11~12が指定できる→port0~7と10~12に拡大
tukudani 0:65f9be2858c9 4262 出力:byte 戻り値 = 送信パケット番号PACKET_ID。0x00は失敗。
tukudani 0:65f9be2858c9 4263 port: port指定 IO名 ピン番号 USB評価ボード(XBIB-U-Dev)
tukudani 0:65f9be2858c9 4264 port= 0 DIO0 XBee_pin 20 (Cms) SW1 Commision
tukudani 0:65f9be2858c9 4265 port= 1 DIO1 XBee_pin 19 (AD1) SW2
tukudani 0:65f9be2858c9 4266 port= 2 DIO2 XBee_pin 18 (AD2) SW3
tukudani 0:65f9be2858c9 4267 port= 3 DIO3 XBee_pin 17 (AD3) SW4
tukudani 0:65f9be2858c9 4268 port= 4 DIO4 XBee_pin 11 LED3 ※port4 がLED3
tukudani 0:65f9be2858c9 4269 port=10 DIO10 XBee_pin 6 RSSI
tukudani 0:65f9be2858c9 4270 port=11 DIO11 XBee_pin 7 LED2 ※port11がLED2
tukudani 0:65f9be2858c9 4271 port=12 DIO12 XBee_pin 4 LED1 ※port12がLED1
tukudani 0:65f9be2858c9 4272 */
tukudani 0:65f9be2858c9 4273 char at[6];
tukudani 0:65f9be2858c9 4274 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4275 // byte i=0;
tukudani 0:65f9be2858c9 4276 byte ret=0;
tukudani 0:65f9be2858c9 4277
tukudani 0:65f9be2858c9 4278 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4279 at[0]='S'; at[1]='A'; at[2]='T'; at[3]=0; at[5]='\0';
tukudani 0:65f9be2858c9 4280 // at[0]='R'; // RAT(応答待ち)の場合は本行のコメントを外してRATに変更する(かなり遅くなる)
tukudani 0:65f9be2858c9 4281 if( port <= 7 ){ // port >= 0 &&
tukudani 0:65f9be2858c9 4282 at[3] = 'D';
tukudani 0:65f9be2858c9 4283 at[4] = '0' + port;
tukudani 0:65f9be2858c9 4284 }else if( port >= 10 && port <= 12 ){
tukudani 0:65f9be2858c9 4285 at[3] = 'P';
tukudani 0:65f9be2858c9 4286 at[4] = '0' + (port - 10);
tukudani 0:65f9be2858c9 4287 }
tukudani 0:65f9be2858c9 4288 if( at[3] ){
tukudani 0:65f9be2858c9 4289 data[0]=0x05; // output = H
tukudani 0:65f9be2858c9 4290 if( out == 0 ) data[0]=0x04; // output = L
tukudani 0:65f9be2858c9 4291 data[1]=0x00;
tukudani 0:65f9be2858c9 4292 ret = xbee_at_tx( at, data ,1 );
tukudani 0:65f9be2858c9 4293 if( ret > 0 ) ret = PACKET_ID;
tukudani 0:65f9be2858c9 4294 // ret = xbee_tx_rx( at, data ,1 ); // RAT(応答待ち)の場合にコメントを外す
tukudani 0:65f9be2858c9 4295 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 4296 if( DEVICE_TYPE == XB_TYPE_WIFI20) delay(10);
tukudani 0:65f9be2858c9 4297 // S6Bで安定動作させるために必要(理由は不明)
tukudani 0:65f9be2858c9 4298 /*
tukudani 0:65f9be2858c9 4299 XBee Wi-Fiの場合はPACKET IDを0にしても応答がある。
tukudani 0:65f9be2858c9 4300 したがってSATでは応答なしATコマンドを明示して送信している。
tukudani 0:65f9be2858c9 4301 ところが、その場合ACを受信するまでコマンドが確定しない。
tukudani 0:65f9be2858c9 4302 その対策としてATACを追加で送信する。
tukudani 0:65f9be2858c9 4303 XBee ZBでもATACで応答遅延が無くなる利点があるが、パケット増加で
tukudani 0:65f9be2858c9 4304 かえって高速動作が出来なくなるので、Wi-Fiのみの対応とする。
tukudani 0:65f9be2858c9 4305 */
tukudani 0:65f9be2858c9 4306 if(ret){ // 送信データを即確定する
tukudani 0:65f9be2858c9 4307 at[0]='S'; at[1]='A'; at[2]='T';
tukudani 0:65f9be2858c9 4308 at[3]='A'; at[4]='C';
tukudani 0:65f9be2858c9 4309 xbee_at_tx( at, data ,0 );
tukudani 0:65f9be2858c9 4310 }
tukudani 0:65f9be2858c9 4311 #endif
tukudani 0:65f9be2858c9 4312 }
tukudani 0:65f9be2858c9 4313 #ifdef DEBUG
tukudani 0:65f9be2858c9 4314 lcd_goto(LCD_ROW_3);
tukudani 0:65f9be2858c9 4315 if( ret==0 ){
tukudani 0:65f9be2858c9 4316 lcd_putstr("GPO ERR:");
tukudani 0:65f9be2858c9 4317 }else{
tukudani 0:65f9be2858c9 4318 lcd_putstr("GPO OK:");
tukudani 0:65f9be2858c9 4319 }
tukudani 0:65f9be2858c9 4320 lcd_disp_hex( data[17] );
tukudani 0:65f9be2858c9 4321 #endif
tukudani 0:65f9be2858c9 4322 #ifdef LCD_H
tukudani 0:65f9be2858c9 4323 xbee_log( 1, "done:xbee_gpo" , ret );
tukudani 0:65f9be2858c9 4324 #endif
tukudani 0:65f9be2858c9 4325 return(ret);
tukudani 0:65f9be2858c9 4326 }
tukudani 0:65f9be2858c9 4327
tukudani 0:65f9be2858c9 4328 #ifdef EASY_GPI
tukudani 0:65f9be2858c9 4329 byte xbee_gpi(const byte *address, const byte port){
tukudani 0:65f9be2858c9 4330 /* リモート先(ADR_FROMに入力)のGPIOの入力値を応答するAPI
tukudani 0:65f9be2858c9 4331
tukudani 0:65f9be2858c9 4332 ★注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4333 本コマンドでは無くxbee_forceとxbee_rx_callの組み合わせでの利用を推奨します。
tukudani 0:65f9be2858c9 4334
tukudani 0:65f9be2858c9 4335 portは1~4と11~12と0xFFが指定できる。
tukudani 0:65f9be2858c9 4336 port=0xFFでポート0~7をHEXで応答する。
tukudani 0:65f9be2858c9 4337 AINはxbee_adcを使用する。
tukudani 0:65f9be2858c9 4338 port: port指定 IO名 ピン番号 USB評価ボード(XBIB-U-Dev)
tukudani 0:65f9be2858c9 4339 port= 1 DIO1 XBee_pin 19 (AD1) SW2
tukudani 0:65f9be2858c9 4340 port= 2 DIO2 XBee_pin 18 (AD2) SW3
tukudani 0:65f9be2858c9 4341 port= 3 DIO3 XBee_pin 17 (AD3) SW4
tukudani 0:65f9be2858c9 4342 out: 指定ポートの状態 0または1、エラー時は0xFF
tukudani 0:65f9be2858c9 4343
tukudani 0:65f9be2858c9 4344 設計情報
tukudani 0:65f9be2858c9 4345 リターン信号例
tukudani 0:65f9be2858c9 4346 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
tukudani 0:65f9be2858c9 4347 7E 00 15 97 01 00 13 A2 00 -- -- -- -- -- -- 49 53 00 01 18 1E 00 00 0E 8A
tukudani 0:65f9be2858c9 4348 | len |mo|FI|送信元IEEEアドレス |Short|AT(IS) |01|MASK |AD|DATA |CS|
tukudani 0:65f9be2858c9 4349 | |de|
tukudani 0:65f9be2858c9 4350 | |__|__________________ MODE_RESP(0x97)= ATIS応答
tukudani 0:65f9be2858c9 4351 |_____|_____________________ Data Length
tukudani 0:65f9be2858c9 4352 */
tukudani 0:65f9be2858c9 4353 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4354 byte ret=0xFF;
tukudani 0:65f9be2858c9 4355
tukudani 0:65f9be2858c9 4356 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4357 if( xbee_tx_rx("RATIS", data , 0) > 0 ){
tukudani 0:65f9be2858c9 4358 // if( data[3]==MODE_RESP ){ // d[3] flame ID = 97(RAT応答)
tukudani 0:65f9be2858c9 4359 // tx_rxで data[3]はチェック済み
tukudani 0:65f9be2858c9 4360 if( port == 0xFF) ret = (data[23] & data[20]);
tukudani 0:65f9be2858c9 4361 else{
tukudani 0:65f9be2858c9 4362 if( (port >= 1) && (port <= 4) ){
tukudani 0:65f9be2858c9 4363 if( ((data[20]>>port)&0x01) == 0x00 ){ // MASK_L(20)の該当ビットが否の時
tukudani 0:65f9be2858c9 4364 data[0]=0x03; // リモート端末のポート1~3をデジタル入力(0x03)に設定
tukudani 0:65f9be2858c9 4365 if( port ==1 ) xbee_tx_rx( "RATD1", data ,1 );
tukudani 0:65f9be2858c9 4366 if( port ==2 ) xbee_tx_rx( "RATD2", data ,1 );
tukudani 0:65f9be2858c9 4367 if( port ==3 ) xbee_tx_rx( "RATD3", data ,1 );
tukudani 0:65f9be2858c9 4368 if( port ==4 ) xbee_tx_rx( "RATD4", data ,1 );
tukudani 0:65f9be2858c9 4369 wait_millisec(200);
tukudani 0:65f9be2858c9 4370 xbee_tx_rx("RATIS", data , 0); // 再度ISを実行
tukudani 0:65f9be2858c9 4371 }
tukudani 0:65f9be2858c9 4372 if( data[3]==MODE_RESP ) ret= ((data[23] >> port ) & 0x01); // 取得データDATA_L(23)を戻り値に
tukudani 0:65f9be2858c9 4373 } else if( (port >= 11) && (port <= 12) ){
tukudani 0:65f9be2858c9 4374 if( ((data[19]>>(port-8))&0x01) == 0x00 ){ // MASK_H(19)の該当ビットが否の時
tukudani 0:65f9be2858c9 4375 data[0]=0x03; // リモート端末のポート11~12をデジタル入力(0x03)に設定
tukudani 0:65f9be2858c9 4376 if( port ==11 ) xbee_tx_rx( "RATP1", data ,1 );
tukudani 0:65f9be2858c9 4377 if( port ==12 ) xbee_tx_rx( "RATP2", data ,1 );
tukudani 0:65f9be2858c9 4378 wait_millisec(200);
tukudani 0:65f9be2858c9 4379 xbee_tx_rx("RATIS", data , 0); // 再度ISを実行
tukudani 0:65f9be2858c9 4380 }
tukudani 0:65f9be2858c9 4381 if( data[3]==MODE_RESP ) ret= ((data[22] >> (port-8))& 0x01); // 取得データDATA_H(22)を戻り値に
tukudani 0:65f9be2858c9 4382 }
tukudani 0:65f9be2858c9 4383 }
tukudani 0:65f9be2858c9 4384 // }
tukudani 0:65f9be2858c9 4385 }
tukudani 0:65f9be2858c9 4386 #ifdef DEBUG
tukudani 0:65f9be2858c9 4387 lcd_goto(LCD_ROW_3);
tukudani 0:65f9be2858c9 4388 lcd_putstr("port"); lcd_disp_hex( port ); lcd_putstr("="); lcd_disp_hex( ret ); // 戻り値
tukudani 0:65f9be2858c9 4389 lcd_goto(LCD_ROW_4);
tukudani 0:65f9be2858c9 4390 lcd_putstr(" f:"); lcd_disp_hex( data[3]);
tukudani 0:65f9be2858c9 4391 lcd_putstr(" M:"); lcd_disp_hex( data[19] ); lcd_disp_hex( data[20] ); // MASK表示
tukudani 0:65f9be2858c9 4392 lcd_putstr(" D:"); lcd_disp_hex( data[22] ); lcd_disp_hex( data[23] ); // デジタルサンプル値
tukudani 0:65f9be2858c9 4393 #endif
tukudani 0:65f9be2858c9 4394 #ifdef LCD_H
tukudani 0:65f9be2858c9 4395 xbee_log( 1, "done:xbee_gpi" , ret );
tukudani 0:65f9be2858c9 4396 #endif
tukudani 0:65f9be2858c9 4397 return( ret );
tukudani 0:65f9be2858c9 4398 }
tukudani 0:65f9be2858c9 4399 #endif // EASY_GPI
tukudani 0:65f9be2858c9 4400
tukudani 0:65f9be2858c9 4401 #ifdef EASY_ADC
tukudani 0:65f9be2858c9 4402 unsigned int xbee_adc(const byte *address, const byte port){
tukudani 0:65f9be2858c9 4403 /* リモート先(addressに入力)のADCの入力値を応答するAPI
tukudani 0:65f9be2858c9 4404 portは1~3が指定できる。指定したportがADC入力でない場合はADC入力に切り替える。
tukudani 0:65f9be2858c9 4405 port: port指定 IO名 ピン番号 USB評価ボード(XBIB-U-Dev)
tukudani 0:65f9be2858c9 4406 port= 1 DIO1 XBee_pin 19 (AD1) SW2
tukudani 0:65f9be2858c9 4407 port= 2 DIO2 XBee_pin 18 (AD2) SW3
tukudani 0:65f9be2858c9 4408 port= 3 DIO3 XBee_pin 17 (AD3) SW4
tukudani 0:65f9be2858c9 4409 out: ADC値
tukudani 0:65f9be2858c9 4410
tukudani 0:65f9be2858c9 4411 設計情報
tukudani 0:65f9be2858c9 4412 リターン信号例
tukudani 0:65f9be2858c9 4413 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
tukudani 0:65f9be2858c9 4414 7E 00 17 97 01 00 13 A2 00 -- -- -- -- -- -- 49 53 00 01 18 16 08 18 06 00 00 7A
tukudani 0:65f9be2858c9 4415 | len |mo|FI|送信元IEEEアドレス |Short|AT(IS) |01|GPIO |AD|GPIO |ADC |CS
tukudani 0:65f9be2858c9 4416 | |de| | |Adrss| |01|MASK |MS|DATA |DATA |
tukudani 0:65f9be2858c9 4417 | | | | | |
tukudani 0:65f9be2858c9 4418 | | | ADC MASK________________|__|
tukudani 0:65f9be2858c9 4419 | |__|__________________ MODE_RESP(0x97)= ATIS応答
tukudani 0:65f9be2858c9 4420 |_____|_____________________ Data Length
tukudani 0:65f9be2858c9 4421
tukudani 0:65f9be2858c9 4422 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4423 */
tukudani 0:65f9be2858c9 4424 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4425 byte ports=1;
tukudani 0:65f9be2858c9 4426 unsigned int ret=0xFFFF;
tukudani 0:65f9be2858c9 4427
tukudani 0:65f9be2858c9 4428 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4429 if( xbee_tx_rx("RATIS", data , 0) > 0 ){
tukudani 0:65f9be2858c9 4430 if( data[3]==MODE_RESP ){ // d[3] flame ID = 97(MODE_RESP)
tukudani 0:65f9be2858c9 4431 if( (port >= 1) && (port <= 3) ){
tukudani 0:65f9be2858c9 4432 if( ((data[21]>>port ) & 0x01) == 0x00 ){ // MASKの該当ビットが否の時
tukudani 0:65f9be2858c9 4433 data[0]=0x02; // リモート端末のポート1~3をADC入力(0x02)に設定
tukudani 0:65f9be2858c9 4434 if( port == 1 ) xbee_tx_rx( "RATD1", data ,1 );
tukudani 0:65f9be2858c9 4435 if( port == 2 ) xbee_tx_rx( "RATD2", data ,1 );
tukudani 0:65f9be2858c9 4436 if( port == 3 ) xbee_tx_rx( "RATD3", data ,1 );
tukudani 0:65f9be2858c9 4437 wait_millisec(1000);
tukudani 0:65f9be2858c9 4438 xbee_tx_rx("RATIS", data , 0); // 再度ISを実行
tukudani 0:65f9be2858c9 4439 }
tukudani 0:65f9be2858c9 4440 if( (port == 2) && ((data[21]>>1)&0x01) ) ports =2; // port2指定でport1がADCならデータは2個
tukudani 0:65f9be2858c9 4441 else if ( port == 3 ){
tukudani 0:65f9be2858c9 4442 if( (data[21]>>2)&(data[21]>>1)&0x01 ) ports =3; // port3指定でport1と2がADCならデータは3個
tukudani 0:65f9be2858c9 4443 else if( ((data[21]>>2)|(data[21]>>1))&0x01 ) ports =2; // port3指定でport1か2の片方がADCならデータは2個
tukudani 0:65f9be2858c9 4444 }
tukudani 0:65f9be2858c9 4445 if( data[3]==MODE_RESP ) ret= (unsigned int)data[22+ports*2]*256 + (unsigned int)data[23+ports*2];
tukudani 0:65f9be2858c9 4446 // 取得データを戻り値に
tukudani 0:65f9be2858c9 4447 }
tukudani 0:65f9be2858c9 4448 }
tukudani 0:65f9be2858c9 4449 }
tukudani 0:65f9be2858c9 4450 #ifdef DEBUG
tukudani 0:65f9be2858c9 4451 lcd_goto(LCD_ROW_4); lcd_putstr("ADC "); lcd_disp_hex( port ); lcd_putstr("=");
tukudani 0:65f9be2858c9 4452 lcd_disp_hex( data[21] ); lcd_putstr(" "); lcd_disp_5( ret );
tukudani 0:65f9be2858c9 4453 #endif
tukudani 0:65f9be2858c9 4454 #ifdef LCD_H
tukudani 0:65f9be2858c9 4455 xbee_log( 1, "done:xbee_adc" , ret );
tukudani 0:65f9be2858c9 4456 #endif
tukudani 0:65f9be2858c9 4457 return( ret );
tukudani 0:65f9be2858c9 4458 }
tukudani 0:65f9be2858c9 4459 #endif // EASY_ADC
tukudani 0:65f9be2858c9 4460
tukudani 0:65f9be2858c9 4461 byte xbee_force(const byte *address ){
tukudani 0:65f9be2858c9 4462 /*
tukudani 0:65f9be2858c9 4463 GPIOやXBee Sensor(XBee Sensor /L/T または /L/T/H) へ測定指示(ATIS)を送るコマンド
tukudani 0:65f9be2858c9 4464 本xbee_forceで測定指示を出して、xbee_rx_callで受け取ることを推奨します。
tukudani 0:65f9be2858c9 4465 XBee Sensor はxbee_sensor_resultでセンサー値を計算できます。
tukudani 0:65f9be2858c9 4466 入力:送信アドレス
tukudani 0:65f9be2858c9 4467 出力:送信結果(送信したAPIサービス長。送信しなかった場合は0)
tukudani 0:65f9be2858c9 4468 */
tukudani 0:65f9be2858c9 4469 byte ret = 0x00;
tukudani 0:65f9be2858c9 4470 byte null_data [] = { 0x00 };
tukudani 0:65f9be2858c9 4471
tukudani 0:65f9be2858c9 4472 xbee_address( address ); // 送信先を指定
tukudani 0:65f9be2858c9 4473 ret = xbee_at_tx("RATIS", null_data , 0); // ATISを送信
tukudani 0:65f9be2858c9 4474 if( ret > 0 ) ret = PACKET_ID;
tukudani 0:65f9be2858c9 4475 #ifdef LCD_H
tukudani 0:65f9be2858c9 4476 xbee_log( 1, "done:xbee_force" , ret );
tukudani 0:65f9be2858c9 4477 #endif
tukudani 0:65f9be2858c9 4478 return ( ret );
tukudani 0:65f9be2858c9 4479 }
tukudani 0:65f9be2858c9 4480
tukudani 0:65f9be2858c9 4481 #ifdef ARDUINO
tukudani 0:65f9be2858c9 4482 float xbee_sensor_result( XBEE_RESULT *xbee_result, const byte type){
tukudani 0:65f9be2858c9 4483 #else
tukudani 0:65f9be2858c9 4484 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 4485 float xbee_sensor_result( XBEE_RESULT *xbee_result, const byte type){
tukudani 0:65f9be2858c9 4486 #else
tukudani 0:65f9be2858c9 4487 float xbee_sensor_result( XBEE_RESULT *xbee_result, const enum xbee_sensor_type type ){
tukudani 0:65f9be2858c9 4488 #endif
tukudani 0:65f9be2858c9 4489 #endif
tukudani 0:65f9be2858c9 4490 /*
tukudani 0:65f9be2858c9 4491 xbee_rx_callで取得したxbee_sensorデータを値に変換するAPI(非通信)
tukudani 0:65f9be2858c9 4492 call_result情報から変換
tukudani 0:65f9be2858c9 4493 LIGHTはXBee Sensor用
tukudani 0:65f9be2858c9 4494 WATTはXBee Smart Plug用
tukudani 0:65f9be2858c9 4495
tukudani 0:65f9be2858c9 4496 入力:enum xbee_sensor_type {LIGHT,TEMP,HUMIDITY,WATT}; // センサタイプの型
tukudani 0:65f9be2858c9 4497 出力:float センサ読み取り値
tukudani 0:65f9be2858c9 4498
tukudani 0:65f9be2858c9 4499 XBee Sensor(XBee Sensor /L/T または /L/T/H) ※ 1-wire Sensor Adapterではない
tukudani 0:65f9be2858c9 4500
tukudani 0:65f9be2858c9 4501 設計情報
tukudani 0:65f9be2858c9 4502 リターン信号例
tukudani 0:65f9be2858c9 4503 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
tukudani 0:65f9be2858c9 4504 7E 00 1B 97 01 00 13 A2 00 -- -- -- -- -- -- 49 53 00 01 08 00 0E 08 00 03 E2 02 62 02 16 A1
tukudani 0:65f9be2858c9 4505 | len |mo|FI|送信元IEEEアドレス |Short|AT(IS) |01|GPIO |AD|GPIO |ADC1 |ADC2 |ADC3 |CS
tukudani 0:65f9be2858c9 4506 | |de| | |Adrss| |01|MASK |MS|DATA |DATA |DATA |DATA |
tukudani 0:65f9be2858c9 4507 | | | | | | |Light|Temp |Humidity
tukudani 0:65f9be2858c9 4508 | | | ADC MASK________________|__|
tukudani 0:65f9be2858c9 4509 | |__|__________________ MODE_RESP(0x97)= ATIS応答 [手間!]この先はMASKによって
tukudani 0:65f9be2858c9 4510 |_____|_____________________ Data Length データの無い部分は省略される
tukudani 0:65f9be2858c9 4511 */
tukudani 0:65f9be2858c9 4512
tukudani 0:65f9be2858c9 4513 float ret = -1.0f;
tukudani 0:65f9be2858c9 4514
tukudani 0:65f9be2858c9 4515 switch(type){
tukudani 0:65f9be2858c9 4516 case LIGHT:
tukudani 0:65f9be2858c9 4517 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 4518 ret = ((float)(xbee_result->ADCIN[1]))*1.173f;
tukudani 0:65f9be2858c9 4519 // (ADC1) /1023.0) * 1200
tukudani 0:65f9be2858c9 4520 #else
tukudani 0:65f9be2858c9 4521 ret = ((float)(xbee_result->ADCIN[1]))*2.444f;
tukudani 0:65f9be2858c9 4522 // (ADC1) /1023.0) * 2500
tukudani 0:65f9be2858c9 4523 #endif
tukudani 0:65f9be2858c9 4524 break;
tukudani 0:65f9be2858c9 4525 case TEMP:
tukudani 0:65f9be2858c9 4526 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 4527 ret = ((float)(xbee_result->ADCIN[2]))*0.1173f-50.0f;
tukudani 0:65f9be2858c9 4528 // (((ADC2/1023.0) * 1200) - 500.0)/ 10.0
tukudani 0:65f9be2858c9 4529 #else
tukudani 0:65f9be2858c9 4530 ret = ((float)(xbee_result->ADCIN[2]))*0.2444f-50.0f;
tukudani 0:65f9be2858c9 4531 #endif
tukudani 0:65f9be2858c9 4532 /*
tukudani 0:65f9be2858c9 4533 XBee ルータの温度測定結果は内部温度による上昇分が加算されます。
tukudani 0:65f9be2858c9 4534 Digi社によると約4℃の上昇があるので4℃を減算して使用することを推奨している。
tukudani 0:65f9be2858c9 4535 本ライブラリでは減算していませんが、実際の温度差を測定したところ
tukudani 0:65f9be2858c9 4536 平均7.12℃の差がありました。(27.2℃~28.0℃の環境下での平均誤差)
tukudani 0:65f9be2858c9 4537 */
tukudani 0:65f9be2858c9 4538 break;
tukudani 0:65f9be2858c9 4539 case HUMIDITY:
tukudani 0:65f9be2858c9 4540 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 4541 ret = ((float)(xbee_result->ADCIN[3]))*1.173f;
tukudani 0:65f9be2858c9 4542 // mVanalog = (ADC3/1023.0) * 1200
tukudani 0:65f9be2858c9 4543 #else
tukudani 0:65f9be2858c9 4544 ret = ((float)(xbee_result->ADCIN[3]))*2.444f;
tukudani 0:65f9be2858c9 4545 #endif
tukudani 0:65f9be2858c9 4546 ret = ( ((ret * 108.2f / 33.2f) / 5000.0f - 0.16f) / 0.0062f );
tukudani 0:65f9be2858c9 4547 // hum = (((mVanalog * 108.2 / 33.2) / 5000 - 0.16) / 0.0062)
tukudani 0:65f9be2858c9 4548 break;
tukudani 0:65f9be2858c9 4549 case WATT:
tukudani 0:65f9be2858c9 4550 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 4551 ret = ((float)(xbee_result->ADCIN[3]))*1.17302f;
tukudani 0:65f9be2858c9 4552 #else
tukudani 0:65f9be2858c9 4553 ret = ((float)(xbee_result->ADCIN[3]))*2.44379f;
tukudani 0:65f9be2858c9 4554 #endif
tukudani 0:65f9be2858c9 4555 ret = (ret * 156.0f / 47.0f - 520.0f) / 180.0f * 70.71f ;
tukudani 0:65f9be2858c9 4556 if( ret < 0 ) ret = 0;
tukudani 0:65f9be2858c9 4557 // current = (mV*(156.0/47.0) - 520.0) /180.0 * .7071
tukudani 0:65f9be2858c9 4558 /* 電圧を測定していないのでワットへの変換は100Vを乗算している。*/
tukudani 0:65f9be2858c9 4559 /* XBee Smart Plugは誘導負荷が考慮されないので測定値は参考値にしかならない。
tukudani 0:65f9be2858c9 4560 (モーターなどの力率の悪いものは測れない)
tukudani 0:65f9be2858c9 4561 The XBee Smart Plug uses a hall-effect current sensor IC to detect RMS current draw
tukudani 0:65f9be2858c9 4562 from the user controlled outlet. As there is no power factor correction for inductive loads,
tukudani 0:65f9be2858c9 4563 sensor accuracy is only specified for resistive loads. Current measurements can still be
tukudani 0:65f9be2858c9 4564 made on inductive loads that fall within the product load rating, but accuracy is not
tukudani 0:65f9be2858c9 4565 guaranteed. */
tukudani 0:65f9be2858c9 4566 break;
tukudani 0:65f9be2858c9 4567 default:
tukudani 0:65f9be2858c9 4568 break;
tukudani 0:65f9be2858c9 4569 }
tukudani 0:65f9be2858c9 4570 #ifdef LCD_H
tukudani 0:65f9be2858c9 4571 xbee_log( 1, "done:xbee_sensor_result" , (byte)ret );
tukudani 0:65f9be2858c9 4572 #endif
tukudani 0:65f9be2858c9 4573 return( ret );
tukudani 0:65f9be2858c9 4574 }
tukudani 0:65f9be2858c9 4575
tukudani 0:65f9be2858c9 4576 unsigned int xbee_batt_acum(byte a1, byte a2 ){
tukudani 0:65f9be2858c9 4577 return( (unsigned int)( ( (float)a1 * 256.0f + (float)a2 ) * 1.17f ) );
tukudani 0:65f9be2858c9 4578 }
tukudani 0:65f9be2858c9 4579
tukudani 0:65f9be2858c9 4580 byte xbee_rx_call( XBEE_RESULT *xbee_result ){
tukudani 0:65f9be2858c9 4581 /*
tukudani 0:65f9be2858c9 4582 XBeeからの受信を行うための待ち受け関数
tukudani 0:65f9be2858c9 4583 Ver 1.70以降はモード指定での待ち受け機能を削除した。
tukudani 0:65f9be2858c9 4584 mode MODE_AUTO 0x00 // 自動選択(modeに選択後のモード名が入る)
tukudani 0:65f9be2858c9 4585 mode MODE_UART 0x90 // UART Receive
tukudani 0:65f9be2858c9 4586 mode MODE_UAR2 0x91 // UART AO=0
tukudani 0:65f9be2858c9 4587 mode MODE_GPIN 0x92 // GPI data
tukudani 0:65f9be2858c9 4588 mode MODE_SENS 0x94 // XB Sensor
tukudani 0:65f9be2858c9 4589 mode MODE_IDNT 0x95 // Node Identify
tukudani 0:65f9be2858c9 4590 mode MODE_RES 0x88 // ローカルATコマンドの結果を受信
tukudani 0:65f9be2858c9 4591 mode MODE_RESP 0x97 // リモートATコマンドの結果を受信
tukudani 0:65f9be2858c9 4592 mode MODE_MODM 0x8A // Modem Statusを受信
tukudani 0:65f9be2858c9 4593 mode MODE_TXST 0x8B // UART Transmit Status を受信
tukudani 0:65f9be2858c9 4594 mode MODE_BATT 0xE1 // (独自定義)バッテリステータス RAT%Vの応答時
tukudani 0:65f9be2858c9 4595
tukudani 0:65f9be2858c9 4596 出力   :byte データ
tukudani 0:65f9be2858c9 4597 入力・出力:byte *mode モード値
tukudani 0:65f9be2858c9 4598 出力   :byte *xbee_result データの戻り値
tukudani 0:65f9be2858c9 4599       GPIデータ(無いGPIデータは0になるADCはFFFFになる)
tukudani 0:65f9be2858c9 4600        00 01 02 03 04 05 06 07 08 09 0A 0B 0C ....
tukudani 0:65f9be2858c9 4601        |GPI |ADC0 |ADC1 |ADC2 |ADC3 |\0|\0|\0|....
tukudani 0:65f9be2858c9 4602       UARTデータは最後が\0で戻り値がlength
tukudani 0:65f9be2858c9 4603 */
tukudani 0:65f9be2858c9 4604 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4605 byte ret=0;
tukudani 0:65f9be2858c9 4606 byte return_MODE = 0x00;
tukudani 0:65f9be2858c9 4607 byte cache_en = 0x00;
tukudani 0:65f9be2858c9 4608 byte i, j;
tukudani 0:65f9be2858c9 4609
tukudani 0:65f9be2858c9 4610 // 各種APIによるエラー発生の解除処理
tukudani 0:65f9be2858c9 4611 #ifdef ERRLOG
tukudani 0:65f9be2858c9 4612 if( TIMER_ERR ){
tukudani 0:65f9be2858c9 4613 #ifdef LCD_H
tukudani 0:65f9be2858c9 4614 xbee_log( 5, ERR_LOG , ERR_CODE ); // エラーログを出力
tukudani 0:65f9be2858c9 4615 #endif
tukudani 0:65f9be2858c9 4616 xbee_reset(); // リセットに約3秒がかかる
tukudani 0:65f9be2858c9 4617 #ifdef LCD_H
tukudani 0:65f9be2858c9 4618 xbee_log( 3, "XBee Restarted" , TIMER_ERR );
tukudani 0:65f9be2858c9 4619 #endif
tukudani 0:65f9be2858c9 4620 #ifdef H3694
tukudani 0:65f9be2858c9 4621 led_red( 0 );
tukudani 0:65f9be2858c9 4622 #endif
tukudani 0:65f9be2858c9 4623 TIMER_ERR = 0;
tukudani 0:65f9be2858c9 4624 }
tukudani 0:65f9be2858c9 4625 #endif
tukudani 0:65f9be2858c9 4626
tukudani 0:65f9be2858c9 4627 // 応答値の初期化
tukudani 0:65f9be2858c9 4628 xbee_result->MODE = 0x00; // Ver 1.70から自動受信を廃止。そのための追加
tukudani 0:65f9be2858c9 4629 xbee_result->ID = 0x00;
tukudani 0:65f9be2858c9 4630 for( i=0 ; i<8 ; i++ ) xbee_result->FROM[i] = 0x00;
tukudani 0:65f9be2858c9 4631 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4632 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4633 for( i=0 ; i<2 ; i++ ) xbee_result->SHORT[i] = 0x00;
tukudani 0:65f9be2858c9 4634 #endif
tukudani 0:65f9be2858c9 4635 #endif
tukudani 0:65f9be2858c9 4636 for( i=0 ; i<2 ; i++ ){
tukudani 0:65f9be2858c9 4637 xbee_result->AT[i] = 0xFF;
tukudani 0:65f9be2858c9 4638 xbee_result->GPI.BYTE[i] = 0xFF;
tukudani 0:65f9be2858c9 4639 }
tukudani 0:65f9be2858c9 4640 xbee_result->STATUS = 0xFF;
tukudani 0:65f9be2858c9 4641 for( i=1 ; i<4 ; i++ ) xbee_result->ADCIN[i] = (unsigned int)(0xFFFF);
tukudani 0:65f9be2858c9 4642 xbee_result->ADCIN[0] = 0;
tukudani 0:65f9be2858c9 4643 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4644 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4645 for( i=0 ; i<6 ; i++ ) xbee_result->ZCL[i] = 0xFF;
tukudani 0:65f9be2858c9 4646 #endif
tukudani 0:65f9be2858c9 4647 #endif
tukudani 0:65f9be2858c9 4648 for( i=0 ; i<CALL_SIZE ; i++ ) xbee_result->DATA[i] = 0x00;
tukudani 0:65f9be2858c9 4649
tukudani 0:65f9be2858c9 4650 j = 0;
tukudani 0:65f9be2858c9 4651 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 4652 if( CACHE_COUNTER > 0 ){
tukudani 0:65f9be2858c9 4653 for( i=0 ; i < API_SIZE ; i++) data[i] = CACHE_MEM[0][i];
tukudani 0:65f9be2858c9 4654 /* FIFO 処理 面倒なのでメモリーをコピー */
tukudani 0:65f9be2858c9 4655 for( j=1 ; j < CACHE_COUNTER ; j++)
tukudani 0:65f9be2858c9 4656 for( i=0 ; i < API_SIZE ; i++) CACHE_MEM[j-1][i] = CACHE_MEM[j][i];
tukudani 0:65f9be2858c9 4657 CACHE_COUNTER--;
tukudani 0:65f9be2858c9 4658 return_MODE = 0xFF;
tukudani 0:65f9be2858c9 4659 cache_en = 0xFF; // キャッシュ有効 フラグ
tukudani 0:65f9be2858c9 4660 #ifdef DEBUG
tukudani 0:65f9be2858c9 4661 lcd_putstr("### CACHE_OUT ### -> "); // DEBUG
tukudani 0:65f9be2858c9 4662 lcd_disp_hex( CACHE_COUNTER );
tukudani 0:65f9be2858c9 4663 lcd_putch('\n');
tukudani 0:65f9be2858c9 4664 #endif
tukudani 0:65f9be2858c9 4665 }
tukudani 0:65f9be2858c9 4666 #endif
tukudani 0:65f9be2858c9 4667 if( cache_en == 0x00 ) return_MODE = xbee_at_rx( data ); // 受信
tukudani 0:65f9be2858c9 4668 if( return_MODE > 0x00 ){ // キャッシュがある、または受信データがある時
tukudani 0:65f9be2858c9 4669 // if( xbee_result->MODE == 0x00 || // 指定(入力)されたモード値が00(オート)
tukudani 0:65f9be2858c9 4670 // xbee_result->MODE == data[3] || // または、受信結果と一致
tukudani 0:65f9be2858c9 4671 // cache_en ){ // キャッシュ動作の時
tukudani 0:65f9be2858c9 4672 return_MODE = xbee_from_acum( data );
tukudani 0:65f9be2858c9 4673 switch( return_MODE ){
tukudani 0:65f9be2858c9 4674 case MODE_RESP: // 0x97 リモートATコマンドの結果を受信 Remote Command Response
tukudani 0:65f9be2858c9 4675 for(i=0;i<8;i++) xbee_result->FROM[i] = data[5+i]; // アドレスはdata[5]から
tukudani 0:65f9be2858c9 4676 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4677 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4678 for(i=0;i<2;i++) xbee_result->SHORT[i] = data[13+i];// アドレスはdata[13]から
tukudani 0:65f9be2858c9 4679 #endif
tukudani 0:65f9be2858c9 4680 #endif
tukudani 0:65f9be2858c9 4681 // for( i=0 ; i<2 ; i++ ) xbee_result->AT[i] = data[15+i]; // ATはdata[15]から
tukudani 0:65f9be2858c9 4682 xbee_result->AT[0] = data[15];
tukudani 0:65f9be2858c9 4683 xbee_result->AT[1] = data[16];
tukudani 0:65f9be2858c9 4684 xbee_result->ID = data[4]; // PACKET_ID
tukudani 0:65f9be2858c9 4685 xbee_result->STATUS = data[17]; // statusはdata[17]
tukudani 0:65f9be2858c9 4686 ret = data[17];
tukudani 0:65f9be2858c9 4687 if( xbee_result->STATUS==0x00 ){
tukudani 0:65f9be2858c9 4688 if( xbee_result->AT[0]=='I' && xbee_result->AT[1]=='S' ){ // ATISの受信
tukudani 0:65f9be2858c9 4689 ret=(data[23] & data[20]); // GPIOの下位を受信マスクで&して応答(要約応答)
tukudani 0:65f9be2858c9 4690 if( data[19] == 0x00 && data[20] == 0x00 ){
tukudani 0:65f9be2858c9 4691 j = 1; // デジタルデータが無いとき
tukudani 0:65f9be2858c9 4692 }else{
tukudani 0:65f9be2858c9 4693 j = 0; // デジタルデータがある時
tukudani 0:65f9be2858c9 4694 xbee_result->GPI.BYTE[1] = (data[22] & data[19]); // 上位をマスクして応答
tukudani 0:65f9be2858c9 4695 xbee_result->GPI.BYTE[0] = (data[23] & data[20]); // 下位をマスクして応答
tukudani 0:65f9be2858c9 4696 for( i = 0 ; i<2 ; i++) xbee_result->DATA[i] = xbee_result->GPI.BYTE[i];
tukudani 0:65f9be2858c9 4697 }
tukudani 0:65f9be2858c9 4698 for( i=0; i < 4 ; i++ ){ // この中でjを使用している
tukudani 0:65f9be2858c9 4699 if( (data[21]>>i) & 0x01 ){ // data[21] ADCマスク
tukudani 0:65f9be2858c9 4700 xbee_result->ADCIN[i] = (unsigned int)(data[(2*i+24-2*j)]);
tukudani 0:65f9be2858c9 4701 xbee_result->ADCIN[i] *= (unsigned int)256;
tukudani 0:65f9be2858c9 4702 xbee_result->ADCIN[i] += (unsigned int)(data[2*i+25-2*j]);
tukudani 0:65f9be2858c9 4703 xbee_result->DATA[i*2+2] = data[24+2*i-2*j];
tukudani 0:65f9be2858c9 4704 xbee_result->DATA[i*2+3] = data[25+2*i-2*j];
tukudani 0:65f9be2858c9 4705 }else{
tukudani 0:65f9be2858c9 4706 xbee_result->DATA[i*2+2] = 0xFF;
tukudani 0:65f9be2858c9 4707 xbee_result->DATA[i*2+3] = 0xFF;
tukudani 0:65f9be2858c9 4708 j++;
tukudani 0:65f9be2858c9 4709 }
tukudani 0:65f9be2858c9 4710 }
tukudani 0:65f9be2858c9 4711 }else if( xbee_result->AT[0] == '%' && xbee_result->AT[1] == 'V'){ // BATTの受信
tukudani 0:65f9be2858c9 4712 return_MODE = MODE_BATT;
tukudani 0:65f9be2858c9 4713 xbee_result->GPI.BYTE[1] = data[18];
tukudani 0:65f9be2858c9 4714 xbee_result->GPI.BYTE[0] = data[19];
tukudani 0:65f9be2858c9 4715 // xbee_result->DATA[1] = data[18];
tukudani 0:65f9be2858c9 4716 // xbee_result->DATA[0] = data[19];
tukudani 0:65f9be2858c9 4717 xbee_result->ADCIN[0] = xbee_batt_acum(data[18],data[19]);
tukudani 0:65f9be2858c9 4718 }
tukudani 0:65f9be2858c9 4719 }
tukudani 0:65f9be2858c9 4720 break;
tukudani 0:65f9be2858c9 4721 case MODE_GPIN: // 0x92 GPI data を受信するモード (ZigBee IO Data Sample Ex Indicator)
tukudani 0:65f9be2858c9 4722 ret=xbee_gpi_acum( data );
tukudani 0:65f9be2858c9 4723 for(i=0;i<8;i++) xbee_result->FROM[i] = data[4+i]; // アドレスはdata[4]から
tukudani 0:65f9be2858c9 4724 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4725 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4726 for(i=0;i<2;i++) xbee_result->SHORT[i] = data[12+i];// アドレスはdata[12]から
tukudani 0:65f9be2858c9 4727 #endif
tukudani 0:65f9be2858c9 4728 #endif
tukudani 0:65f9be2858c9 4729 xbee_result->STATUS = data[14]; // statusはdata[14] 0x01=Ack 0x02=broadcast
tukudani 0:65f9be2858c9 4730 if( data[16] == 0x00 && data[17] == 0x00 ){
tukudani 0:65f9be2858c9 4731 j = 1; // デジタルデータが無いとき
tukudani 0:65f9be2858c9 4732 }else{
tukudani 0:65f9be2858c9 4733 j = 0; // デジタルデータがある時
tukudani 0:65f9be2858c9 4734 xbee_result->GPI.BYTE[1] = (data[16] & data[19]); // 上位をマスクして応答
tukudani 0:65f9be2858c9 4735 xbee_result->GPI.BYTE[0] = (data[17] & data[20]); // 下位をマスクして応答
tukudani 0:65f9be2858c9 4736 for( i = 0 ; i<2 ; i++) xbee_result->DATA[i] = xbee_result->GPI.BYTE[i];
tukudani 0:65f9be2858c9 4737 }
tukudani 0:65f9be2858c9 4738 for( i=0; i < 4 ; i++ ){ // この中でjを使用している
tukudani 0:65f9be2858c9 4739 if( (data[18]>>i) & 0x01 ){
tukudani 0:65f9be2858c9 4740 xbee_result->ADCIN[i] = (unsigned int)(data[2*i+21-2*j]);
tukudani 0:65f9be2858c9 4741 xbee_result->ADCIN[i] *= (unsigned int)256;
tukudani 0:65f9be2858c9 4742 xbee_result->ADCIN[i] += (unsigned int)(data[2*i+22-2*j]);
tukudani 0:65f9be2858c9 4743 xbee_result->DATA[i*2+2] = data[21+2*i-2*j];
tukudani 0:65f9be2858c9 4744 xbee_result->DATA[i*2+3] = data[22+2*i-2*j];
tukudani 0:65f9be2858c9 4745 }else {
tukudani 0:65f9be2858c9 4746 xbee_result->DATA[i*2+2] = 0xFF;
tukudani 0:65f9be2858c9 4747 xbee_result->DATA[i*2+3] = 0xFF;
tukudani 0:65f9be2858c9 4748 j++;
tukudani 0:65f9be2858c9 4749 }
tukudani 0:65f9be2858c9 4750 //printf("ADC[%d]=%d \n",i, (int)(xbee_result->ADC[i]) );
tukudani 0:65f9be2858c9 4751 }
tukudani 0:65f9be2858c9 4752 if( (data[18]>>7) & 0x01 ){
tukudani 0:65f9be2858c9 4753 xbee_result->ADCIN[0] = xbee_batt_acum(data[29-2*j],data[30-2*j]);
tukudani 0:65f9be2858c9 4754 }
tukudani 0:65f9be2858c9 4755 // xbee_result->DATA[10]=data[2]; // 仮(受信バイト数を確認するため)
tukudani 0:65f9be2858c9 4756 break;
tukudani 0:65f9be2858c9 4757 case MODE_UART: // 0x90 UART data を受信するモード 戻り値はlength
tukudani 0:65f9be2858c9 4758 for(i=0;i<8;i++) xbee_result->FROM[i] = data[4+i]; // アドレスはdata[4]から
tukudani 0:65f9be2858c9 4759 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4760 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4761 for(i=0;i<2;i++) xbee_result->SHORT[i] = data[12+i];// アドレスはdata[12]から
tukudani 0:65f9be2858c9 4762 #endif
tukudani 0:65f9be2858c9 4763 #endif
tukudani 0:65f9be2858c9 4764 xbee_result->STATUS = data[14]; // statusはdata[14]
tukudani 0:65f9be2858c9 4765 if( ( xbee_uart_acum( data )) > 0 ){ // 以上の値は仕様書に記述誤り
tukudani 0:65f9be2858c9 4766 ret = data[2]-0x0C; // 12バイト減算
tukudani 0:65f9be2858c9 4767 if( ret >= CALL_SIZE ) ret = CALL_SIZE -1;
tukudani 0:65f9be2858c9 4768 for( i=0; i<ret ; i++ ) xbee_result->DATA[i] = data[15+i];
tukudani 0:65f9be2858c9 4769 // xbee_result->DATA[i] = 0x00;
tukudani 0:65f9be2858c9 4770 xbee_result->STATUS = data[14];
tukudani 0:65f9be2858c9 4771 }
tukudani 0:65f9be2858c9 4772 break;
tukudani 0:65f9be2858c9 4773 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 4774 case MODE_UAR2: // 0x91 UART data 2を受信するモード
tukudani 0:65f9be2858c9 4775 for(i=0;i<8;i++) xbee_result->FROM[i] = data[4+i]; // アドレスはdata[4]から
tukudani 0:65f9be2858c9 4776 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4777 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4778 for(i=0;i<2;i++) xbee_result->SHORT[i] = data[12+i];// アドレスはdata[12]から
tukudani 0:65f9be2858c9 4779 for(i=0;i<6;i++) xbee_result->ZCL[i] = data[14+i]; // ZCL情報data[14]から
tukudani 0:65f9be2858c9 4780 #endif
tukudani 0:65f9be2858c9 4781 #endif
tukudani 0:65f9be2858c9 4782 xbee_result->STATUS = data[20]; // statusはdata[20]
tukudani 0:65f9be2858c9 4783 if( ( xbee_uart_acum( data )) > 0 ){
tukudani 0:65f9be2858c9 4784 ret = data[2]-0x12; // 18バイト減算
tukudani 0:65f9be2858c9 4785 if( ret >= CALL_SIZE ) ret = CALL_SIZE -1;
tukudani 0:65f9be2858c9 4786 for( i=0; i<ret ; i++ ) xbee_result->DATA[i] = data[21+i];
tukudani 0:65f9be2858c9 4787 // xbee_result->DATA[i] = 0x00;
tukudani 0:65f9be2858c9 4788 }
tukudani 0:65f9be2858c9 4789 break;
tukudani 0:65f9be2858c9 4790 #endif // LITE
tukudani 0:65f9be2858c9 4791 #ifndef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 4792 case MODE_SENS: // 0x94 XB Sensor(1-wire)を受信するモード(未確認)
tukudani 0:65f9be2858c9 4793 for(i=0;i<8;i++) xbee_result->FROM[i] = data[4+i]; // アドレスはdata[4]から
tukudani 0:65f9be2858c9 4794 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4795 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4796 for(i=0;i<2;i++) xbee_result->SHORT[i] = data[12+i];// アドレスはdata[12]から
tukudani 0:65f9be2858c9 4797 #endif
tukudani 0:65f9be2858c9 4798 #endif
tukudani 0:65f9be2858c9 4799 xbee_result->STATUS = data[14]; // statusはdata[14]
tukudani 0:65f9be2858c9 4800 xbee_result->ADCIN[0] = 256*(unsigned int)data[24] + (unsigned int)data[25];
tukudani 0:65f9be2858c9 4801 for( i=1; i < 4 ; i++ ) // ADC値を代入、ただしAIN[0]へは代入しない
tukudani 0:65f9be2858c9 4802 xbee_result->ADCIN[i] = 256*(unsigned int)data[2*i+16] + (unsigned int)data[2*i+16];
tukudani 0:65f9be2858c9 4803 for( i=0; i<10 ; i++ ) xbee_result->DATA[i] = data[16+i];
tukudani 0:65f9be2858c9 4804 xbee_result->DATA[10] = data[16+i]; // sensor 01:ADC 02:Temp. 60:water present
tukudani 0:65f9be2858c9 4805 break;
tukudani 0:65f9be2858c9 4806 #endif // LITE
tukudani 0:65f9be2858c9 4807 case MODE_IDNT: // 0x95 Node Identifyを受信するモード
tukudani 0:65f9be2858c9 4808 for(i=0;i<8;i++) xbee_result->FROM[i] = data[4+i]; // アドレスはdata[4]から
tukudani 0:65f9be2858c9 4809 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4810 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4811 for(i=0;i<2;i++) xbee_result->SHORT[i] = data[12+i]; // アドレスはdata[12]から
tukudani 0:65f9be2858c9 4812 for(i=0;i<CALL_SIZE && i<20;i++){
tukudani 0:65f9be2858c9 4813 xbee_result->DATA[i] = data[15+i];
tukudani 0:65f9be2858c9 4814 /*
tukudani 0:65f9be2858c9 4815 0 15 Source 16-bit address
tukudani 0:65f9be2858c9 4816 2 17 64-bit Network address
tukudani 0:65f9be2858c9 4817 10 25 NI Strings
tukudani 0:65f9be2858c9 4818 12 27 Parent address
tukudani 0:65f9be2858c9 4819 14 29 Device Type
tukudani 0:65f9be2858c9 4820 15 30 Source Event
tukudani 0:65f9be2858c9 4821 16 31 Profile ID C1 05
tukudani 0:65f9be2858c9 4822 18 33 Manufacture ID 10 1F
tukudani 0:65f9be2858c9 4823 */
tukudani 0:65f9be2858c9 4824 }
tukudani 0:65f9be2858c9 4825 #endif
tukudani 0:65f9be2858c9 4826 #endif
tukudani 0:65f9be2858c9 4827 xbee_result->STATUS = data[14]; // // statusはdata[14] 0x01=Ack 0x02=broadcast
tukudani 0:65f9be2858c9 4828 ret = data[14];
tukudani 0:65f9be2858c9 4829 switch(data[29]){
tukudani 0:65f9be2858c9 4830 case 0x00: ret=ZB_TYPE_COORD; break;
tukudani 0:65f9be2858c9 4831 case 0x01: ret=ZB_TYPE_ROUTER; break;
tukudani 0:65f9be2858c9 4832 case 0x02: ret=ZB_TYPE_ENDDEV; break;
tukudani 0:65f9be2858c9 4833 }
tukudani 0:65f9be2858c9 4834 break;
tukudani 0:65f9be2858c9 4835 case MODE_RES: // 0x88 ローカルATコマンドの結果を受信(モードのみ応答)
tukudani 0:65f9be2858c9 4836 // for( i=0 ; i<2 ; i++ ) xbee_result->AT[i] = data[5+i]; // ATはdata[15]から
tukudani 0:65f9be2858c9 4837 xbee_result->AT[0] = data[5];
tukudani 0:65f9be2858c9 4838 xbee_result->AT[1] = data[6];
tukudani 0:65f9be2858c9 4839 xbee_result->ID = data[4]; // Frame ID
tukudani 0:65f9be2858c9 4840 xbee_result->STATUS = data[7]; // statusはdata[7]
tukudani 0:65f9be2858c9 4841 ret = data[7];
tukudani 0:65f9be2858c9 4842 break;
tukudani 0:65f9be2858c9 4843 case MODE_MODM: // 0x8A Modem Statusを受信
tukudani 0:65f9be2858c9 4844 xbee_result->STATUS = data[4]; // statusはdata[4]
tukudani 0:65f9be2858c9 4845 ret = data[4];
tukudani 0:65f9be2858c9 4846 break;
tukudani 0:65f9be2858c9 4847 case MODE_TXST: // 0x8B UART Transmit Status を受信
tukudani 0:65f9be2858c9 4848 xbee_result->STATUS = data[8]; // delivery statusはdata[8] 0x00=Success
tukudani 0:65f9be2858c9 4849 ret = data[9]; // Discovery status data[9]
tukudani 0:65f9be2858c9 4850 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4851 #ifndef ARM_MBED
tukudani 0:65f9be2858c9 4852 case 0x00:
tukudani 0:65f9be2858c9 4853 break;
tukudani 0:65f9be2858c9 4854 #endif
tukudani 0:65f9be2858c9 4855 #endif
tukudani 0:65f9be2858c9 4856 default:
tukudani 0:65f9be2858c9 4857 #ifdef LCD_H
tukudani 0:65f9be2858c9 4858 xbee_log( 3, "CAUTION:recieved unknown pckt" , xbee_result->MODE );
tukudani 0:65f9be2858c9 4859 #endif
tukudani 0:65f9be2858c9 4860 ret = xbee_result->MODE;
tukudani 0:65f9be2858c9 4861 break;
tukudani 0:65f9be2858c9 4862 }
tukudani 0:65f9be2858c9 4863 xbee_result->MODE = return_MODE;
tukudani 0:65f9be2858c9 4864 }
tukudani 0:65f9be2858c9 4865 #ifdef LCD_H
tukudani 0:65f9be2858c9 4866 // xbee_log( 1, "done:xbee_rx_call" , ret );
tukudani 0:65f9be2858c9 4867 #endif
tukudani 0:65f9be2858c9 4868 return( ret );
tukudani 0:65f9be2858c9 4869 }
tukudani 0:65f9be2858c9 4870
tukudani 0:65f9be2858c9 4871 void xbee_clear_cache(void){
tukudani 0:65f9be2858c9 4872 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 4873 CACHE_COUNTER=0;
tukudani 0:65f9be2858c9 4874 #endif
tukudani 0:65f9be2858c9 4875 }
tukudani 0:65f9be2858c9 4876
tukudani 0:65f9be2858c9 4877 #ifdef EASY_SENSOR
tukudani 0:65f9be2858c9 4878 float xbee_sensor(const byte *address, const enum xbee_sensor_type type ){
tukudani 0:65f9be2858c9 4879 /* XBee Sensor(XBee Sensor /L/T または /L/T/H) の測定API (1-wire Sensor Adapterではない)
tukudani 0:65f9be2858c9 4880
tukudani 0:65f9be2858c9 4881 注意:本コマンドは応答待ちを行うので干渉によるパケット損失があります。
tukudani 0:65f9be2858c9 4882    また、センサーがスリープになっている場合はエラーになる場合が多い(タイムアウトする)
tukudani 0:65f9be2858c9 4883
tukudani 0:65f9be2858c9 4884 これらのような場合は、xbee_forceで測定指示を出して、xbee_rx_callで受信結果を得ます。
tukudani 0:65f9be2858c9 4885 XBee Sensorはxbee_sensor_resultで受信結果を温度などに変換できます。
tukudani 0:65f9be2858c9 4886
tukudani 0:65f9be2858c9 4887 入力:enum xbee_sensor_type {LIGHT,TEMP,HUMIDITY,WATT}; // センサタイプの型
tukudani 0:65f9be2858c9 4888 出力:float センサ読み取り値
tukudani 0:65f9be2858c9 4889
tukudani 0:65f9be2858c9 4890 設計情報
tukudani 0:65f9be2858c9 4891 リターン信号例
tukudani 0:65f9be2858c9 4892 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
tukudani 0:65f9be2858c9 4893 7E 00 1B 97 01 00 13 A2 00 -- -- -- -- -- -- 49 53 00 01 08 00 0E 08 00 03 E2 02 62 02 16 A1
tukudani 0:65f9be2858c9 4894 | len |mo|FI|送信元IEEEアドレス |Short|AT(IS) |01|GPIO |AD|GPIO |ADC1 |ADC2 |ADC3 |CS
tukudani 0:65f9be2858c9 4895 | |de| | |Adrss| |01|MASK |MS|DATA |DATA |DATA |DATA |
tukudani 0:65f9be2858c9 4896 | | | | | | |Light|Temp |Humidity
tukudani 0:65f9be2858c9 4897 | | | ADC MASK________________|__|
tukudani 0:65f9be2858c9 4898 | |__|__________________ MODE_RESP(0x97)= ATIS応答
tukudani 0:65f9be2858c9 4899 |_____|_____________________ Data Length
tukudani 0:65f9be2858c9 4900 */
tukudani 0:65f9be2858c9 4901 XBEE_RESULT xbee_result;
tukudani 0:65f9be2858c9 4902 byte data[API_SIZE];
tukudani 0:65f9be2858c9 4903 byte j;
tukudani 0:65f9be2858c9 4904 float ret = -1.0f;
tukudani 0:65f9be2858c9 4905
tukudani 0:65f9be2858c9 4906 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 4907 if( xbee_tx_rx("RATIS", data , 0) > 0 ){
tukudani 0:65f9be2858c9 4908 if( data[3]==MODE_RESP ){ // d[3] flame ID = 97(RAT応答)
tukudani 0:65f9be2858c9 4909 if( data[19] == 0x00 && data[20] == 0x00 ) j = 0; else j = 2;
tukudani 0:65f9be2858c9 4910 xbee_result.ADCIN[1] = (unsigned int)data[22+j] * 256 + (unsigned int)data[23+j];
tukudani 0:65f9be2858c9 4911 xbee_result.ADCIN[2] = (unsigned int)data[24+j] * 256 + (unsigned int)data[25+j];
tukudani 0:65f9be2858c9 4912 xbee_result.ADCIN[3] = (unsigned int)data[26+j] * 256 + (unsigned int)data[27+j];
tukudani 0:65f9be2858c9 4913 ret = xbee_sensor_result( &xbee_result, type );
tukudani 0:65f9be2858c9 4914 }
tukudani 0:65f9be2858c9 4915 }
tukudani 0:65f9be2858c9 4916 #ifdef LCD_H
tukudani 0:65f9be2858c9 4917 xbee_log( 1, "done:xbee_sensor" , ret );
tukudani 0:65f9be2858c9 4918 #endif
tukudani 0:65f9be2858c9 4919 return( ret );
tukudani 0:65f9be2858c9 4920 }
tukudani 0:65f9be2858c9 4921 #endif // EASY_SENSOR
tukudani 0:65f9be2858c9 4922
tukudani 0:65f9be2858c9 4923 byte xbee_init( const byte port ){
tukudani 0:65f9be2858c9 4924 /*
tukudani 0:65f9be2858c9 4925 port:PCの場合、オープンするシリアルポート番号
tukudani 0:65f9be2858c9 4926  Arduinoの場合リトライ回数、0で永久にリトライを繰り返す
tukudani 0:65f9be2858c9 4927 戻り値:0=エラー 1=成功までのトライ回数
tukudani 0:65f9be2858c9 4928 Arduino版 portが0で無い時は、portの指定回数リトライしてエラーでもreturnする
tukudani 0:65f9be2858c9 4929 */
tukudani 0:65f9be2858c9 4930 byte i=1; // iはリセット実行リトライ回数、0で成功するまで永久
tukudani 0:65f9be2858c9 4931 byte j; // jは色々
tukudani 0:65f9be2858c9 4932 byte k=0; // kはリセット成功可否フラグ,戻り値
tukudani 0:65f9be2858c9 4933 #ifndef LITE
tukudani 0:65f9be2858c9 4934 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 4935 byte address[8]; //={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tukudani 0:65f9be2858c9 4936 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 4937 byte address[8]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
tukudani 0:65f9be2858c9 4938 // xbee_address(address);
tukudani 0:65f9be2858c9 4939 #endif
tukudani 0:65f9be2858c9 4940 #endif
tukudani 0:65f9be2858c9 4941
tukudani 0:65f9be2858c9 4942 #ifdef LCD_H
tukudani 0:65f9be2858c9 4943 xbee_log( 1, "started:xbee_init" , port );
tukudani 0:65f9be2858c9 4944 #endif
tukudani 0:65f9be2858c9 4945 #ifdef H3694
tukudani 0:65f9be2858c9 4946 port_init(); // 汎用入出力ポートの初期化
tukudani 0:65f9be2858c9 4947 timer_sec_init(); // 秒タイマー初期化
tukudani 0:65f9be2858c9 4948 led_green( 1 );
tukudani 0:65f9be2858c9 4949 #endif
tukudani 0:65f9be2858c9 4950
tukudani 0:65f9be2858c9 4951 #ifdef H3694
tukudani 0:65f9be2858c9 4952 sci_init( 0 ); // シリアル初期化
tukudani 0:65f9be2858c9 4953 i=port;
tukudani 0:65f9be2858c9 4954 #elif ARDUINO
tukudani 0:65f9be2858c9 4955 sci_init( 0 ); // シリアル初期化
tukudani 0:65f9be2858c9 4956 i=port;
tukudani 0:65f9be2858c9 4957 #else
tukudani 0:65f9be2858c9 4958 #ifdef ARM_MBED
tukudani 0:65f9be2858c9 4959 sci_init( 0 ); // シリアル初期化
tukudani 0:65f9be2858c9 4960 _xbee_time_init(); // Initialize Timer
tukudani 0:65f9be2858c9 4961 i=port;
tukudani 0:65f9be2858c9 4962 #else // PC
tukudani 0:65f9be2858c9 4963 if( port != 0 ){
tukudani 0:65f9be2858c9 4964 j = sci_init( port ); // シリアル初期化
tukudani 0:65f9be2858c9 4965 }else{
tukudani 0:65f9be2858c9 4966 // ポート検索
tukudani 0:65f9be2858c9 4967 for( i=10 ; i>0; i--){
tukudani 0:65f9be2858c9 4968 j = sci_init( i );
tukudani 0:65f9be2858c9 4969 if( j != 0 ){
tukudani 0:65f9be2858c9 4970 k = xbee_reset();
tukudani 0:65f9be2858c9 4971 if( k > 0 ) i = 1; else j = 0;
tukudani 0:65f9be2858c9 4972 }
tukudani 0:65f9be2858c9 4973 }
tukudani 0:65f9be2858c9 4974 }
tukudani 0:65f9be2858c9 4975 if( j == 0 ){
tukudani 0:65f9be2858c9 4976 fprintf(stderr,"EXIT:Serial Open Error\n");
tukudani 0:65f9be2858c9 4977 exit(-1);
tukudani 0:65f9be2858c9 4978 }
tukudani 0:65f9be2858c9 4979 i=1;
tukudani 0:65f9be2858c9 4980 #endif
tukudani 0:65f9be2858c9 4981 #endif
tukudani 0:65f9be2858c9 4982
tukudani 0:65f9be2858c9 4983 #ifdef LCD_H
tukudani 0:65f9be2858c9 4984 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4985 #ifdef H3694
tukudani 0:65f9be2858c9 4986 lcd_enable( 1 ); // 液晶の電源投入
tukudani 0:65f9be2858c9 4987 wait_millisec(15); // 起動待ち
tukudani 0:65f9be2858c9 4988 lcd_init(); // 液晶制御ICのプレ初期化
tukudani 0:65f9be2858c9 4989 #endif
tukudani 0:65f9be2858c9 4990 #endif
tukudani 0:65f9be2858c9 4991 wait_millisec(15); // 初期化待ち
tukudani 0:65f9be2858c9 4992 lcd_init(); // 2回目の初期化(確実な初期化)
tukudani 0:65f9be2858c9 4993 #ifndef ARDUINO
tukudani 0:65f9be2858c9 4994 lcd_control(1,0,0); // 液晶制御 表示ON,カーソルOFF,点滅OFF
tukudani 0:65f9be2858c9 4995 #endif
tukudani 0:65f9be2858c9 4996 #ifdef H3694
tukudani 0:65f9be2858c9 4997 lcd_cls(); // 液晶消去
tukudani 0:65f9be2858c9 4998 lcd_goto(LCD_ROW_1);
tukudani 0:65f9be2858c9 4999 #endif
tukudani 0:65f9be2858c9 5000 lcd_putstr( NAME );
tukudani 0:65f9be2858c9 5001 lcd_putstr(" ");
tukudani 0:65f9be2858c9 5002 lcd_putstr( VERSION );
tukudani 0:65f9be2858c9 5003 #ifdef H3694
tukudani 0:65f9be2858c9 5004 lcd_goto(LCD_ROW_2);
tukudani 0:65f9be2858c9 5005 #else
tukudani 0:65f9be2858c9 5006 lcd_putch('\n');
tukudani 0:65f9be2858c9 5007 #endif
tukudani 0:65f9be2858c9 5008 #ifdef LITE
tukudani 0:65f9be2858c9 5009 lcd_putstr(COPYRIGHT);
tukudani 0:65f9be2858c9 5010 lcd_putch('\n');
tukudani 0:65f9be2858c9 5011 #else
tukudani 0:65f9be2858c9 5012 lcd_putstr(COPYRIGHT);
tukudani 0:65f9be2858c9 5013 #endif
tukudani 0:65f9be2858c9 5014 xbee_log( 1, "xbee_reset" , 0 );
tukudani 0:65f9be2858c9 5015 #endif
tukudani 0:65f9be2858c9 5016 #ifdef LITE // by 蘭
tukudani 0:65f9be2858c9 5017 k = xbee_reset();
tukudani 0:65f9be2858c9 5018 #ifndef H3694
tukudani 0:65f9be2858c9 5019 #ifndef ARDUINO
tukudani 0:65f9be2858c9 5020 #ifndef ARM_MBED // PC
tukudani 0:65f9be2858c9 5021 if(k==0){
tukudani 0:65f9be2858c9 5022 #ifdef LCD_H
tukudani 0:65f9be2858c9 5023 xbee_log( 5, "EXIT:xbee_init:myaddress" , 0 );
tukudani 0:65f9be2858c9 5024 #endif
tukudani 0:65f9be2858c9 5025 exit(-1);
tukudani 0:65f9be2858c9 5026 }
tukudani 0:65f9be2858c9 5027 #endif
tukudani 0:65f9be2858c9 5028 #endif
tukudani 0:65f9be2858c9 5029 #endif
tukudani 0:65f9be2858c9 5030 #else // Normal mode
tukudani 0:65f9be2858c9 5031 k=1;
tukudani 0:65f9be2858c9 5032 if(i==0) while( !xbee_reset() ) wait_millisec(1000);
tukudani 0:65f9be2858c9 5033 else for(j=0;j<i;j++){
tukudani 0:65f9be2858c9 5034 k=xbee_reset(); // 0だとシリアル異常
tukudani 0:65f9be2858c9 5035 if( k ) break;
tukudani 0:65f9be2858c9 5036 wait_millisec(1000);
tukudani 0:65f9be2858c9 5037 }
tukudani 0:65f9be2858c9 5038 if(k==0){
tukudani 0:65f9be2858c9 5039 #ifndef H3694
tukudani 0:65f9be2858c9 5040 #ifndef ARDUINO
tukudani 0:65f9be2858c9 5041 #ifndef ARM_MBED // PC
tukudani 0:65f9be2858c9 5042 #ifdef LCD_H
tukudani 0:65f9be2858c9 5043 xbee_log( 5, "EXIT:xbee_init:myaddress" , 0 );
tukudani 0:65f9be2858c9 5044 #endif
tukudani 0:65f9be2858c9 5045 exit(-1);
tukudani 0:65f9be2858c9 5046 #endif
tukudani 0:65f9be2858c9 5047 #endif
tukudani 0:65f9be2858c9 5048 #endif
tukudani 0:65f9be2858c9 5049 }else{ // k>0 すなわち reset成功時 以下、kは使用しないこと(戻り値にする)
tukudani 0:65f9be2858c9 5050 #ifdef LCD_H
tukudani 0:65f9be2858c9 5051 #ifdef H3694
tukudani 0:65f9be2858c9 5052 lcd_cls();
tukudani 0:65f9be2858c9 5053 lcd_goto(LCD_ROW_1);
tukudani 0:65f9be2858c9 5054 #else
tukudani 0:65f9be2858c9 5055 lcd_putstr("\n--------------------\n");
tukudani 0:65f9be2858c9 5056 #endif
tukudani 0:65f9be2858c9 5057 #endif
tukudani 0:65f9be2858c9 5058 #ifdef LCD_H
tukudani 0:65f9be2858c9 5059 xbee_log( 1, "xbee_init:myaddress" , port );
tukudani 0:65f9be2858c9 5060 #endif
tukudani 0:65f9be2858c9 5061 xbee_myaddress( address ); // 自分自身のアドレスを取得
tukudani 0:65f9be2858c9 5062 #ifdef LCD_H
tukudani 0:65f9be2858c9 5063 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 5064 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 5065 lcd_disp_hex(address[4+i]);
tukudani 0:65f9be2858c9 5066 #else // XBEE_WIFI
tukudani 0:65f9be2858c9 5067 /*
tukudani 0:65f9be2858c9 5068 printf("%d",address[i]);
tukudani 0:65f9be2858c9 5069 if(i<3)printf(".");
tukudani 0:65f9be2858c9 5070 */
tukudani 0:65f9be2858c9 5071 lcd_disp_hex(address[i]);
tukudani 0:65f9be2858c9 5072 #endif
tukudani 0:65f9be2858c9 5073 }
tukudani 0:65f9be2858c9 5074 switch( DEVICE_TYPE ){
tukudani 0:65f9be2858c9 5075 #ifndef XBEE_WIFI
tukudani 0:65f9be2858c9 5076 case ZB_TYPE_COORD: lcd_putstr( " COORD."); break;
tukudani 0:65f9be2858c9 5077 case ZB_TYPE_ROUTER: lcd_putstr( " ROUTER"); break;
tukudani 0:65f9be2858c9 5078 case ZB_TYPE_ENDDEV: lcd_putstr( " ENDDEV"); break;
tukudani 0:65f9be2858c9 5079 #else
tukudani 0:65f9be2858c9 5080 case XB_TYPE_NULL: lcd_putstr( " XBee Wi-Fi"); break;
tukudani 0:65f9be2858c9 5081 case XB_TYPE_WIFI10: lcd_putstr( " XBee Wi-Fi Ver 1.0"); break;
tukudani 0:65f9be2858c9 5082 case XB_TYPE_WIFI20: lcd_putstr( " XBee Wi-Fi Ver 2.0"); break;
tukudani 0:65f9be2858c9 5083 #endif
tukudani 0:65f9be2858c9 5084 default: lcd_putstr( " UNKNWON"); break;
tukudani 0:65f9be2858c9 5085 }
tukudani 0:65f9be2858c9 5086 #ifdef H3694
tukudani 0:65f9be2858c9 5087 lcd_goto(LCD_ROW_1);
tukudani 0:65f9be2858c9 5088 #else
tukudani 0:65f9be2858c9 5089 lcd_putstr("\n\n");
tukudani 0:65f9be2858c9 5090 #endif
tukudani 0:65f9be2858c9 5091 xbee_log( 1, "done:xbee_init" , 0 );
tukudani 0:65f9be2858c9 5092 #endif
tukudani 0:65f9be2858c9 5093 }
tukudani 0:65f9be2858c9 5094 #endif
tukudani 0:65f9be2858c9 5095 return(k);
tukudani 0:65f9be2858c9 5096 }
tukudani 0:65f9be2858c9 5097
tukudani 0:65f9be2858c9 5098
tukudani 0:65f9be2858c9 5099 byte xbee_end_device(const byte *address, byte sp, byte ir, const byte pin){
tukudani 0:65f9be2858c9 5100 /*
tukudani 0:65f9be2858c9 5101 XBee子機(エンドデバイス)をスリープモードに設定する
tukudani 0:65f9be2858c9 5102 入力:byte *address = 宛先(子機)アドレス
tukudani 0:65f9be2858c9 5103 入力:byte sp = 1~28:スリープ間隔(秒)
tukudani 0:65f9be2858c9 5104 入力:byte ir = 0:自動送信切、1~65:自動送信間隔(秒)
tukudani 0:65f9be2858c9 5105 入力:byte pin = 0:通常のスリープ、 1:SLEEP_RQ端子を有効に設定
tukudani 0:65f9be2858c9 5106 出力:戻り値 = 0x00 指示成功、 その他=エラー理由
tukudani 0:65f9be2858c9 5107 1: 対応デバイス以外に設定しようとした
tukudani 0:65f9be2858c9 5108 2: スリープ解除しようとしたのに失敗した
tukudani 0:65f9be2858c9 5109 4: スリープ移行待ち時間ST設定の失敗
tukudani 0:65f9be2858c9 5110 8: スリープ間隔SP設定の失敗
tukudani 0:65f9be2858c9 5111 16: ローカル親機へのスリープ時間SP設定の失敗
tukudani 0:65f9be2858c9 5112 32: データ取得間隔IR設定の失敗
tukudani 0:65f9be2858c9 5113 64: スリープ状態設定の失敗
tukudani 0:65f9be2858c9 5114 128: スリープオプション設定の失敗
tukudani 0:65f9be2858c9 5115 */
tukudani 0:65f9be2858c9 5116 byte ret=0x00;
tukudani 0:65f9be2858c9 5117 byte data[API_SIZE];
tukudani 0:65f9be2858c9 5118 unsigned int time;
tukudani 0:65f9be2858c9 5119
tukudani 0:65f9be2858c9 5120 xbee_address( address ); // 送信先を指定
tukudani 0:65f9be2858c9 5121 if( ir > 65 ) ir = 65;
tukudani 0:65f9be2858c9 5122 time = (unsigned int)ir * 1000; // ms単位の数字に変換
tukudani 0:65f9be2858c9 5123
tukudani 0:65f9be2858c9 5124 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 5125 /* XBee子機から自動送信する周期を設定します */
tukudani 0:65f9be2858c9 5126 data[0] = (byte)(time / 256);
tukudani 0:65f9be2858c9 5127 data[1] = (byte)(time % 256); // スリープ時間 sleep_time
tukudani 0:65f9be2858c9 5128 if( !xbee_tx_rx("RATIR",data,2) ) ret|=32;
tukudani 0:65f9be2858c9 5129
tukudani 0:65f9be2858c9 5130 /* エンドデバイス以外の時に終了する */
tukudani 0:65f9be2858c9 5131 if( xbee_tx_rx("RATVR",data,0) ) if(data[18] != 0x28){
tukudani 0:65f9be2858c9 5132 return( ret );
tukudani 0:65f9be2858c9 5133 }
tukudani 0:65f9be2858c9 5134 #endif
tukudani 0:65f9be2858c9 5135
tukudani 0:65f9be2858c9 5136 if( sp == 0x00 ){
tukudani 0:65f9be2858c9 5137 data[0] = 0x00;
tukudani 0:65f9be2858c9 5138 if( !xbee_tx_rx("RATSM",data,1) ) ret|=2; // スリープ解除
tukudani 0:65f9be2858c9 5139 }else{
tukudani 0:65f9be2858c9 5140 #ifdef XBEE_WIFI
tukudani 0:65f9be2858c9 5141 if(DEVICE_TYPE == XB_TYPE_WIFI20){
tukudani 0:65f9be2858c9 5142 return(1);
tukudani 0:65f9be2858c9 5143 }
tukudani 0:65f9be2858c9 5144 data[0] = 0x01;
tukudani 0:65f9be2858c9 5145 data[1] = 0x40; // スリープ時間 sleep_time
tukudani 0:65f9be2858c9 5146 if( !xbee_tx_rx("RATSO",data,2) ) ret|=128;
tukudani 0:65f9be2858c9 5147 #endif
tukudani 0:65f9be2858c9 5148
tukudani 0:65f9be2858c9 5149 #ifndef XBEE_WIFI // ZigBee
tukudani 0:65f9be2858c9 5150 /* XBee★親機★にXBee子機のデータ保持期間(28秒)を設定 */
tukudani 0:65f9be2858c9 5151 data[0] = 0x0A;
tukudani 0:65f9be2858c9 5152 data[1] = 0xF0; // スリープ時間 sleep_time
tukudani 0:65f9be2858c9 5153 if( !xbee_tx_rx("ATSP",data,2) ) ret|=16; // 保持時間 sleep_time
tukudani 0:65f9be2858c9 5154 #endif
tukudani 0:65f9be2858c9 5155
tukudani 0:65f9be2858c9 5156 /* XBee子機のスリープ移行待ち時間を設定します */
tukudani 0:65f9be2858c9 5157 data[0]=0x01; data[1]=0xF4; // 保持時間 500 ms
tukudani 0:65f9be2858c9 5158 if( !xbee_tx_rx("RATST",data,2) ) ret|=4;
tukudani 0:65f9be2858c9 5159
tukudani 0:65f9be2858c9 5160 /* XBee子機のスリープ間隔を設定します */
tukudani 0:65f9be2858c9 5161 if( sp > 28 ) sp = 28;
tukudani 0:65f9be2858c9 5162 time = (unsigned int)sp * 100; // 10ms単位の数字に変換
tukudani 0:65f9be2858c9 5163 data[0] = (byte)(time / 256);
tukudani 0:65f9be2858c9 5164 data[1] = (byte)(time % 256); // スリープ時間 sleep_time
tukudani 0:65f9be2858c9 5165 if( !xbee_tx_rx("RATSP",data,2) ) ret|=8;
tukudani 0:65f9be2858c9 5166
tukudani 0:65f9be2858c9 5167 /* XBee子機をサイクリックスリープに設定します */
tukudani 0:65f9be2858c9 5168 if( pin ) data[0]=0x05; else data[0]=0x04; // 0x00:常に動作 0x04:低消費電力動作
tukudani 0:65f9be2858c9 5169 if( !xbee_tx_rx("RATSM",data,1) ) ret|=64; // 0x05:SLEEP_RQ有効
tukudani 0:65f9be2858c9 5170 }
tukudani 0:65f9be2858c9 5171
tukudani 0:65f9be2858c9 5172 #ifdef LCD_H
tukudani 0:65f9be2858c9 5173 xbee_log( 1, "done:end_device" , ret );
tukudani 0:65f9be2858c9 5174 #endif
tukudani 0:65f9be2858c9 5175 return( ret );
tukudani 0:65f9be2858c9 5176 }
tukudani 0:65f9be2858c9 5177
tukudani 0:65f9be2858c9 5178 #ifndef LITE
tukudani 0:65f9be2858c9 5179 byte _xbee_i2c(byte dio,byte level,byte *data){
tukudani 0:65f9be2858c9 5180 /* dio = 11:SCL 12:SDA
tukudani 0:65f9be2858c9 5181 level = 0:L 1:Open
tukudani 0:65f9be2858c9 5182 data ATコマンド応答用データ
tukudani 0:65f9be2858c9 5183
tukudani 0:65f9be2858c9 5184 data[0]=0x03; xbee_at_tx("SATP1",data,1); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5185 data[0]=0x04; xbee_at_tx("SATP1",data,1); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5186 data[0]=0x03; xbee_at_tx("SATP2",data,1); // Port 12(SDA) H Imp
tukudani 0:65f9be2858c9 5187 data[0]=0x04; xbee_at_tx("SATP2",data,1); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5188 */
tukudani 0:65f9be2858c9 5189 char at[6]="SATPX";
tukudani 0:65f9be2858c9 5190 if(dio>=10) dio = dio%10;
tukudani 0:65f9be2858c9 5191 if(dio>=2) dio=2; else dio=1;
tukudani 0:65f9be2858c9 5192 at[4]='0'+dio;
tukudani 0:65f9be2858c9 5193 data[0]=level;
tukudani 0:65f9be2858c9 5194 if(level>=10) data[0]=level%10;
tukudani 0:65f9be2858c9 5195 if(level==0) data[0]=4; // Out Low
tukudani 0:65f9be2858c9 5196 if(level==1) data[0]=3; // High Imp
tukudani 0:65f9be2858c9 5197 return( xbee_at_tx(at,data,1) ); // 戻り値:0ならエラー発生
tukudani 0:65f9be2858c9 5198 }
tukudani 0:65f9be2858c9 5199
tukudani 0:65f9be2858c9 5200 byte _xbee_i2c_start(byte *data){
tukudani 0:65f9be2858c9 5201 byte ret=1; // 戻り値:0ならエラー発生
tukudani 0:65f9be2858c9 5202 ret *= _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5203 ret *= xbee_tx_rx("RATAC",data,0); // クロックHレベル固定
tukudani 0:65f9be2858c9 5204 ret *= _xbee_i2c(2/*SDA*/,1,data); // Port 12(SDA) H Imp
tukudani 0:65f9be2858c9 5205 ret *= _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5206 ret *= _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5207 if(ret==0) xbee_log( 5, "ERROR:Failed I2C TX Output" , ret );
tukudani 0:65f9be2858c9 5208 return(ret);
tukudani 0:65f9be2858c9 5209 }
tukudani 0:65f9be2858c9 5210
tukudani 0:65f9be2858c9 5211 byte _xbee_i2c_tx(const byte in, byte *data){
tukudani 0:65f9be2858c9 5212 byte i;
tukudani 0:65f9be2858c9 5213 for(i=0;i<8;i++){
tukudani 0:65f9be2858c9 5214 if( (in>>(7-i))&0x01 ){
tukudani 0:65f9be2858c9 5215 _xbee_i2c(2/*SDA*/,1,data); // Port 12(SDA) H Imp
tukudani 0:65f9be2858c9 5216 }else _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5217 /*Clock*/
tukudani 0:65f9be2858c9 5218 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5219 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5220 }
tukudani 0:65f9be2858c9 5221 /* ACK受信待ち(ACKはスレーブから) */
tukudani 0:65f9be2858c9 5222 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5223 _xbee_i2c(2/*SDA*/,1,data); // Port 12(SDA) H Imp 逆
tukudani 0:65f9be2858c9 5224 // xbee_tx_rx("RATAC",data,0); // 本来は不要だがEnd Deviceの時にRATISを確実に受信するために入れてみた
tukudani 0:65f9be2858c9 5225 i=xbee_tx_rx("RATIS",data,0);
tukudani 0:65f9be2858c9 5226 if( i==0) xbee_log( 5, "ERROR:I2C no RAT RESP" , i );
tukudani 0:65f9be2858c9 5227 if( (data[22]>>4)&0x01 ){
tukudani 0:65f9be2858c9 5228 xbee_log( 5, "ERROR:I2C no ACK" , data[22] );
tukudani 0:65f9be2858c9 5229 i=0;
tukudani 0:65f9be2858c9 5230 }
tukudani 0:65f9be2858c9 5231 // printf("ACK=%d ATIS=%d\n",(data[22]>>4)&0x01,i);
tukudani 0:65f9be2858c9 5232 return(i);
tukudani 0:65f9be2858c9 5233 }
tukudani 0:65f9be2858c9 5234
tukudani 0:65f9be2858c9 5235 byte xbee_i2c_init(const byte *address){
tukudani 0:65f9be2858c9 5236 /*
tukudani 0:65f9be2858c9 5237 XBee I2Cインタフェースとして使用するための初期設定
tukudani 0:65f9be2858c9 5238 入力:byte *address リモートXBee子機のIEEEアドレス
tukudani 0:65f9be2858c9 5239 */
tukudani 0:65f9be2858c9 5240 byte data[API_SIZE];
tukudani 0:65f9be2858c9 5241 byte i;
tukudani 0:65f9be2858c9 5242 for(i=3;i>0;i--){ // リトライ3回まで
tukudani 0:65f9be2858c9 5243 xbee_address(address);
tukudani 0:65f9be2858c9 5244 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5245 _xbee_i2c(2/*SDA*/,1,data); // Port 12(SDA) H Imp
tukudani 0:65f9be2858c9 5246 if( xbee_tx_rx("RATIS",data,0) ){
tukudani 0:65f9be2858c9 5247 if( ((data[22]>>3)&0x03) == 0 ) break;
tukudani 0:65f9be2858c9 5248 }
tukudani 0:65f9be2858c9 5249 }
tukudani 0:65f9be2858c9 5250 delay(200);
tukudani 0:65f9be2858c9 5251 return(i);
tukudani 0:65f9be2858c9 5252 }
tukudani 0:65f9be2858c9 5253
tukudani 0:65f9be2858c9 5254 byte xbee_i2c_read(const byte *address, byte adr, byte *rx, byte len){
tukudani 0:65f9be2858c9 5255 /*
tukudani 0:65f9be2858c9 5256 子機XBeeデバイス(リモート先)のI2Cから指定バイトの情報を読み取る関数
tukudani 0:65f9be2858c9 5257 入力:byte *address = リモートXBee子機のIEEEアドレス
tukudani 0:65f9be2858c9 5258 入力:byte adr = I2Cアドレス /SHT 温度測定時0x01 湿度測定時0x02
tukudani 0:65f9be2858c9 5259 出力:byte *rx = 受信データ用ポインタ
tukudani 0:65f9be2858c9 5260 入力:byte len = 受信長、0の時は Sensirion SHT用2バイト受信
tukudani 0:65f9be2858c9 5261 port=11 DIO11 XBee_pin 7 SCL
tukudani 0:65f9be2858c9 5262 port=12 DIO12 XBee_pin 4 SDA
tukudani 0:65f9be2858c9 5263 ・I2Cマスタ機能のみを実装(スレーブとしては動作しない)
tukudani 0:65f9be2858c9 5264 ・7ビットアドレスのみ対応
tukudani 0:65f9be2858c9 5265 ・1バイトの受信に5秒以上かかる(かなり遅いし、多量のXBee通信が発生する)
tukudani 0:65f9be2858c9 5266 */
tukudani 0:65f9be2858c9 5267 byte data[API_SIZE];
tukudani 0:65f9be2858c9 5268 byte ret,i,sht=0;
tukudani 0:65f9be2858c9 5269
tukudani 0:65f9be2858c9 5270 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 5271 if(len){ // stop シーケンス + start シーケンス
tukudani 0:65f9be2858c9 5272 _xbee_i2c_start(data);
tukudani 0:65f9be2858c9 5273 }else{ // Sensirion SHT用 TS シーケンス
tukudani 0:65f9be2858c9 5274 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5275 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5276 _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5277 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5278 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5279 _xbee_i2c(2/*SDA*/,1,data); // Port 12(SDA) H Imp
tukudani 0:65f9be2858c9 5280 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5281 _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5282 sht=1; len=2; // SHTモード
tukudani 0:65f9be2858c9 5283 xbee_tx_rx("RATAC",data,0);
tukudani 0:65f9be2858c9 5284 adr = adr<<1;
tukudani 0:65f9be2858c9 5285 }
tukudani 0:65f9be2858c9 5286 adr |= 0x01; // RW=1 受信モード
tukudani 0:65f9be2858c9 5287 if( _xbee_i2c_tx(adr, data)==0 ) return(0); // アドレス設定
tukudani 0:65f9be2858c9 5288 if( !sht ){ // SHTセンサで無い時
tukudani 0:65f9be2858c9 5289 _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5290 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5291 }
tukudani 0:65f9be2858c9 5292
tukudani 0:65f9be2858c9 5293 /* スレーブ待機状態待ち */
tukudani 0:65f9be2858c9 5294 if(!sht){ // I2C
tukudani 0:65f9be2858c9 5295 /*
tukudani 0:65f9be2858c9 5296 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 5297 ret=xbee_tx_rx("RATIS",data,0);
tukudani 0:65f9be2858c9 5298 if( (((data[22]>>4)&0x01) == 1) && // ACKが解除された //data[22]b4=Port 12(SDA)
tukudani 0:65f9be2858c9 5299 (((data[22]>>3)&0x01) == 1) ) break;// スレーブがCLKホールドしていない // data[22]b3=Port 11(SCL)
tukudani 0:65f9be2858c9 5300 wait_millisec(500);
tukudani 0:65f9be2858c9 5301 }
tukudani 0:65f9be2858c9 5302 // printf("i=%d\n",i);
tukudani 0:65f9be2858c9 5303 if(i==4){
tukudani 0:65f9be2858c9 5304 xbee_log( 5, "ERROR:I2C Clock Holded" , data[22] );
tukudani 0:65f9be2858c9 5305 return(0);
tukudani 0:65f9be2858c9 5306 }
tukudani 0:65f9be2858c9 5307 */
tukudani 0:65f9be2858c9 5308 }else{ // SHT
tukudani 0:65f9be2858c9 5309 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5310 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 5311 ret=xbee_tx_rx("RATIS",data,0);
tukudani 0:65f9be2858c9 5312 if( ((data[22]>>4)&0x01) == 0 ) break;// 測定完了 //data[22]b4=Port 12(SDA)
tukudani 0:65f9be2858c9 5313 } // printf("Slave Holds SDA i=%d\n",i);
tukudani 0:65f9be2858c9 5314 if(i==4){
tukudani 0:65f9be2858c9 5315 xbee_log( 5, "ERROR:I2C SDA Holded" , data[22] );
tukudani 0:65f9be2858c9 5316 return(0);
tukudani 0:65f9be2858c9 5317 }
tukudani 0:65f9be2858c9 5318 }
tukudani 0:65f9be2858c9 5319 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5320
tukudani 0:65f9be2858c9 5321 /* 受信データ */
tukudani 0:65f9be2858c9 5322 for(ret=0;ret<len;ret++){
tukudani 0:65f9be2858c9 5323 if(ret){ // 初回以外の時はACKを応答する
tukudani 0:65f9be2858c9 5324 _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5325 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5326 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5327 _xbee_i2c(2/*SDA*/,1,data); // Port 12(SDA) H Imp
tukudani 0:65f9be2858c9 5328 }
tukudani 0:65f9be2858c9 5329 rx[ret]=0x00;
tukudani 0:65f9be2858c9 5330 for(i=0;i<8;i++){
tukudani 0:65f9be2858c9 5331 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5332 // xbee_tx_rx("RATAC",data,0);
tukudani 0:65f9be2858c9 5333 if( xbee_tx_rx("RATIS",data,0)==0x00) break; // error
tukudani 0:65f9be2858c9 5334 rx[ret] |= ((data[22]>>4)&0x01)<<(7-i); //data[22] b4=Port 12(SDA)
tukudani 0:65f9be2858c9 5335 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5336 }
tukudani 0:65f9be2858c9 5337 if(i!=8) break; // error
tukudani 0:65f9be2858c9 5338 }
tukudani 0:65f9be2858c9 5339 _xbee_i2c(1/*SCL*/,1,data); // Port 11(SCL) H Imp
tukudani 0:65f9be2858c9 5340 if(sht) _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5341 xbee_tx_rx("RATAC",data,0);
tukudani 0:65f9be2858c9 5342 return(ret);
tukudani 0:65f9be2858c9 5343 }
tukudani 0:65f9be2858c9 5344
tukudani 0:65f9be2858c9 5345 byte xbee_i2c_write(const byte *address, byte adr, byte *tx, byte len){
tukudani 0:65f9be2858c9 5346 /*
tukudani 0:65f9be2858c9 5347 子機XBeeデバイス(リモート先)のI2Cから指定バイトの情報を書き込む関数
tukudani 0:65f9be2858c9 5348 入力:byte *address = リモートXBee子機のIEEEアドレス
tukudani 0:65f9be2858c9 5349 入力:byte adr = I2Cアドレス /SHT 温度測定時0x01 湿度測定時0x02
tukudani 0:65f9be2858c9 5350 入力:byte *tx = 送信データ用ポインタ
tukudani 0:65f9be2858c9 5351 入力:byte len = 送信データ長、0の時は Sensirion SHT用2バイト受信
tukudani 0:65f9be2858c9 5352 port=11 DIO11 XBee_pin 7 SCL
tukudani 0:65f9be2858c9 5353 port=12 DIO12 XBee_pin 4 SDA
tukudani 0:65f9be2858c9 5354 */
tukudani 0:65f9be2858c9 5355 byte data[API_SIZE];
tukudani 0:65f9be2858c9 5356 byte ret;
tukudani 0:65f9be2858c9 5357
tukudani 0:65f9be2858c9 5358 xbee_address(address); // 宛先のアドレスを設定
tukudani 0:65f9be2858c9 5359 if(len) _xbee_i2c_start(data);
tukudani 0:65f9be2858c9 5360 else return(0);
tukudani 0:65f9be2858c9 5361 adr &= 0xFE; // RW=0 送信モード
tukudani 0:65f9be2858c9 5362 if( _xbee_i2c_tx(adr, data)==0 ) return(0); // アドレス設定
tukudani 0:65f9be2858c9 5363 /* スレーブ待機状態待ち */
tukudani 0:65f9be2858c9 5364 /*
tukudani 0:65f9be2858c9 5365 for(i=0;i<4;i++){
tukudani 0:65f9be2858c9 5366 ret=xbee_tx_rx("RATIS",data,0);
tukudani 0:65f9be2858c9 5367 if( (((data[22]>>4)&0x01) == 1) && // ACKが解除された //data[22]b4=Port 12(SDA)
tukudani 0:65f9be2858c9 5368 (((data[22]>>3)&0x01) == 1) ) break;// スレーブがCLKホールドしていない // data[22]b3=Port 11(SCL)
tukudani 0:65f9be2858c9 5369 wait_millisec(500);
tukudani 0:65f9be2858c9 5370 }
tukudani 0:65f9be2858c9 5371 // printf("i=%d\n",i);
tukudani 0:65f9be2858c9 5372 if(i==4){
tukudani 0:65f9be2858c9 5373 xbee_log( 5, "ERROR:I2C Clock Holded" , data[22] );
tukudani 0:65f9be2858c9 5374 return(0);
tukudani 0:65f9be2858c9 5375 }
tukudani 0:65f9be2858c9 5376 */
tukudani 0:65f9be2858c9 5377 _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5378 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5379
tukudani 0:65f9be2858c9 5380 /* 送信データ */
tukudani 0:65f9be2858c9 5381 for(ret=0;ret<len;ret++){
tukudani 0:65f9be2858c9 5382 _xbee_i2c_tx(tx[ret],data);
tukudani 0:65f9be2858c9 5383 _xbee_i2c(2/*SDA*/,0,data); // Port 12(SDA) L Out
tukudani 0:65f9be2858c9 5384 _xbee_i2c(1/*SCL*/,0,data); // Port 11(SCL) L Out
tukudani 0:65f9be2858c9 5385 }
tukudani 0:65f9be2858c9 5386 return(ret);
tukudani 0:65f9be2858c9 5387 }
tukudani 0:65f9be2858c9 5388 #endif // LITE
tukudani 0:65f9be2858c9 5389
tukudani 0:65f9be2858c9 5390 byte xbee_delay(unsigned int ms){
tukudani 0:65f9be2858c9 5391 /*
tukudani 0:65f9be2858c9 5392 待ち時間。受信値はバッファへ保存します。
tukudani 0:65f9be2858c9 5393 入力:unsigned int(shortを想定) ms = 時間[ms]
tukudani 0:65f9be2858c9 5394 戻り値:受信パケット数
tukudani 0:65f9be2858c9 5395 */
tukudani 0:65f9be2858c9 5396 #ifdef LITE // BeeBee Lite by 蘭
tukudani 0:65f9be2858c9 5397 wait_millisec( ms );
tukudani 0:65f9be2858c9 5398 return 0;
tukudani 0:65f9be2858c9 5399 #else
tukudani 0:65f9be2858c9 5400 byte i,ret=0;
tukudani 0:65f9be2858c9 5401 byte data[API_SIZE];
tukudani 0:65f9be2858c9 5402
tukudani 0:65f9be2858c9 5403 while(ms>0){
tukudani 0:65f9be2858c9 5404 #ifdef CACHE_RES
tukudani 0:65f9be2858c9 5405 if( CACHE_COUNTER < CACHE_RES ){
tukudani 0:65f9be2858c9 5406 if( xbee_at_rx( data ) > 0){
tukudani 0:65f9be2858c9 5407 for( i=0 ; i < API_SIZE ; i++) CACHE_MEM[CACHE_COUNTER][i] = data[i];
tukudani 0:65f9be2858c9 5408 CACHE_COUNTER++;
tukudani 0:65f9be2858c9 5409 ret++;
tukudani 0:65f9be2858c9 5410 #ifdef DEBUG
tukudani 0:65f9be2858c9 5411 lcd_putstr("### CACHE_RES ### <- "); // DEBUG
tukudani 0:65f9be2858c9 5412 lcd_disp_hex( CACHE_COUNTER );
tukudani 0:65f9be2858c9 5413 lcd_putch('\n');
tukudani 0:65f9be2858c9 5414 #endif
tukudani 0:65f9be2858c9 5415 }
tukudani 0:65f9be2858c9 5416 }
tukudani 0:65f9be2858c9 5417 #endif
tukudani 0:65f9be2858c9 5418 if( ms > 100 ){
tukudani 0:65f9be2858c9 5419 wait_millisec( 100 );
tukudani 0:65f9be2858c9 5420 ms -= 100;
tukudani 0:65f9be2858c9 5421 }else{
tukudani 0:65f9be2858c9 5422 wait_millisec( ms );
tukudani 0:65f9be2858c9 5423 ms = 0;
tukudani 0:65f9be2858c9 5424 }
tukudani 0:65f9be2858c9 5425 }
tukudani 0:65f9be2858c9 5426 return(ret);
tukudani 0:65f9be2858c9 5427 #endif
tukudani 0:65f9be2858c9 5428 }