test
Dependencies: MTS-Serial libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
zigbee_write.cpp@6:f87d57498fd0, 2017-11-08 (annotated)
- Committer:
- hmiot
- Date:
- Wed Nov 08 12:10:51 2017 +0000
- Revision:
- 6:f87d57498fd0
- Parent:
- 3:9287b58b18e5
Updated for Zigo Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hariom | 2:20a5d4681abf | 1 | #include "RawSerial.h" |
hariom | 2:20a5d4681abf | 2 | #include "RawSerial.h" |
hariom | 2:20a5d4681abf | 3 | #include "mbed.h" |
hariom | 2:20a5d4681abf | 4 | #include "macro.h" |
hariom | 2:20a5d4681abf | 5 | #include "mbed.h" |
hariom | 2:20a5d4681abf | 6 | #include "MTSSerial.h" |
hariom | 2:20a5d4681abf | 7 | #include "rtos.h" |
hariom | 2:20a5d4681abf | 8 | |
hariom | 2:20a5d4681abf | 9 | int zigbee_flag; |
hariom | 2:20a5d4681abf | 10 | extern Mutex my_mutex; |
hariom | 2:20a5d4681abf | 11 | extern int device_buffer[8]; |
hariom | 2:20a5d4681abf | 12 | |
hariom | 2:20a5d4681abf | 13 | Serial log_serial(PA_2,PA_3); |
hariom | 2:20a5d4681abf | 14 | |
hariom | 2:20a5d4681abf | 15 | void recv_cb(); |
hariom | 2:20a5d4681abf | 16 | |
hariom | 2:20a5d4681abf | 17 | /* wait to initialize zigbee devie */ |
hariom | 2:20a5d4681abf | 18 | void init_func() |
hariom | 2:20a5d4681abf | 19 | { |
hariom | 2:20a5d4681abf | 20 | printf("\r.............Initializing ...............\n\r"); |
hariom | 2:20a5d4681abf | 21 | wait(20); |
hariom | 2:20a5d4681abf | 22 | } |
hariom | 2:20a5d4681abf | 23 | |
hariom | 2:20a5d4681abf | 24 | void zigbee_func(int deviceId,int state) |
hariom | 2:20a5d4681abf | 25 | { |
hariom | 2:20a5d4681abf | 26 | printf("device id is %d, state is %d\r\n", deviceId, state); |
hariom | 2:20a5d4681abf | 27 | /* PA_3 for transmission |
hariom | 2:20a5d4681abf | 28 | PA_2 for receiving */ |
hariom | 2:20a5d4681abf | 29 | static int init =0; |
hariom | 2:20a5d4681abf | 30 | if( init == 0){ |
hariom | 2:20a5d4681abf | 31 | init++; |
hariom | 2:20a5d4681abf | 32 | init_func(); |
hariom | 2:20a5d4681abf | 33 | } |
hariom | 2:20a5d4681abf | 34 | printf("\r.............Zigbee Initializing ...............\n\r"); |
hariom | 3:9287b58b18e5 | 35 | //if (deviceId == 1) |
hariom | 3:9287b58b18e5 | 36 | // wait(20); |
hariom | 3:9287b58b18e5 | 37 | //else |
hariom | 3:9287b58b18e5 | 38 | //wait(20); |
hariom | 2:20a5d4681abf | 39 | int i = 0; |
hariom | 2:20a5d4681abf | 40 | log_serial.baud(9600); |
hariom | 2:20a5d4681abf | 41 | unsigned char requestMsg[19]; |
hariom | 2:20a5d4681abf | 42 | |
hariom | 2:20a5d4681abf | 43 | requestMsg[0] = START_BYTE; |
hariom | 2:20a5d4681abf | 44 | requestMsg[1] = 0x12; // Length |
hariom | 2:20a5d4681abf | 45 | requestMsg[2] = 0x01; // Packet type |
hariom | 2:20a5d4681abf | 46 | |
hariom | 2:20a5d4681abf | 47 | if(deviceId == Light_bulb) |
hariom | 2:20a5d4681abf | 48 | memcpy(&requestMsg[3], IEEE_SM1, 8); |
hariom | 2:20a5d4681abf | 49 | else if(deviceId == Light_lamp) |
hariom | 2:20a5d4681abf | 50 | memcpy(&requestMsg[3], IEEE_SM2, 8); |
hariom | 2:20a5d4681abf | 51 | else if (deviceId == Tv) |
hariom | 2:20a5d4681abf | 52 | memcpy(&requestMsg[3], IEEE_SM3, 8); |
hariom | 2:20a5d4681abf | 53 | else { |
hariom | 2:20a5d4681abf | 54 | printf("Invalid Device ID"); |
hariom | 2:20a5d4681abf | 55 | return ; |
hariom | 2:20a5d4681abf | 56 | } |
hariom | 2:20a5d4681abf | 57 | requestMsg[11] = MSB(NETWORK_ADDRESS); |
hariom | 2:20a5d4681abf | 58 | requestMsg[12] = LSB(NETWORK_ADDRESS); |
hariom | 2:20a5d4681abf | 59 | requestMsg[13] = SOURCE_ENDPOINT; |
hariom | 2:20a5d4681abf | 60 | requestMsg[14] = MSB(CLUSTER_ID); |
hariom | 2:20a5d4681abf | 61 | requestMsg[15] = LSB(CLUSTER_ID); |
hariom | 2:20a5d4681abf | 62 | requestMsg[16] = MSB(PAYLOAD); |
hariom | 2:20a5d4681abf | 63 | requestMsg[17] = LSB(PAYLOAD); |
hariom | 2:20a5d4681abf | 64 | |
hariom | 2:20a5d4681abf | 65 | if( state == ON) |
hariom | 2:20a5d4681abf | 66 | requestMsg[18] = 0x01; |
hariom | 2:20a5d4681abf | 67 | else |
hariom | 2:20a5d4681abf | 68 | requestMsg[18] = 0x0; |
hariom | 2:20a5d4681abf | 69 | wait(5); |
hariom | 2:20a5d4681abf | 70 | for( i = 0 ; i < 19; i ++){ |
hariom | 2:20a5d4681abf | 71 | log_serial.putc(requestMsg[i]); |
hariom | 2:20a5d4681abf | 72 | } |
hariom | 2:20a5d4681abf | 73 | if (deviceId == 1) |
hariom | 2:20a5d4681abf | 74 | wait(20); |
hariom | 2:20a5d4681abf | 75 | else |
hariom | 3:9287b58b18e5 | 76 | wait(5); |
hariom | 2:20a5d4681abf | 77 | } |
hariom | 2:20a5d4681abf | 78 | |
hariom | 2:20a5d4681abf | 79 | |
hariom | 2:20a5d4681abf | 80 | |
hariom | 2:20a5d4681abf | 81 | void zigbee_task(void const *args) |
hariom | 2:20a5d4681abf | 82 | { |
hariom | 2:20a5d4681abf | 83 | printf("started zigbee task\r\n"); |
hariom | 2:20a5d4681abf | 84 | int zigbee_buffer[8]; |
hariom | 2:20a5d4681abf | 85 | int i; |
hariom | 2:20a5d4681abf | 86 | while (true) { |
hariom | 2:20a5d4681abf | 87 | if (zigbee_flag) { |
hariom | 2:20a5d4681abf | 88 | my_mutex.lock(); |
hariom | 2:20a5d4681abf | 89 | for (i = 0; i < 2; i++) |
hariom | 2:20a5d4681abf | 90 | zigbee_buffer[i] = device_buffer[i]; |
hariom | 2:20a5d4681abf | 91 | my_mutex.unlock(); |
hariom | 2:20a5d4681abf | 92 | printf("zigbee_func called\r\n"); |
hariom | 2:20a5d4681abf | 93 | zigbee_func(zigbee_buffer[0], zigbee_buffer[1]); |
hariom | 2:20a5d4681abf | 94 | zigbee_flag = 0; |
hariom | 2:20a5d4681abf | 95 | } |
hariom | 2:20a5d4681abf | 96 | osDelay(1000); |
hariom | 2:20a5d4681abf | 97 | } |
hariom | 2:20a5d4681abf | 98 | } |