Home automation using Xbee radios
Dependencies: EthernetNetIf HTTPServer RPCInterface mbed C12832_lcd
main.cpp@2:9503a713b648, 2013-11-27 (annotated)
- Committer:
- hpham33
- Date:
- Wed Nov 27 18:36:31 2013 +0000
- Revision:
- 2:9503a713b648
- Parent:
- 1:230c1ec1c9bb
- Child:
- 3:c4bec8e7cc07
upload Hai's code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chrisisthefish | 0:c498b8bcfc46 | 1 | #include "mbed.h" |
chrisisthefish | 0:c498b8bcfc46 | 2 | |
hpham33 | 2:9503a713b648 | 3 | DigitalIn pb(p8); |
hpham33 | 2:9503a713b648 | 4 | |
hpham33 | 2:9503a713b648 | 5 | |
hpham33 | 2:9503a713b648 | 6 | |
hpham33 | 2:9503a713b648 | 7 | struct xbee { // radio prototype with addresss, location, pointer to sensor list |
hpham33 | 2:9503a713b648 | 8 | unsigned int addrHigh; // upper 16 bits address of sensor |
hpham33 | 2:9503a713b648 | 9 | unsigned int addrLow; // lower address of sensor |
hpham33 | 2:9503a713b648 | 10 | unsigned short digitalData; |
hpham33 | 2:9503a713b648 | 11 | unsigned short digitalDataOutput; |
hpham33 | 2:9503a713b648 | 12 | int digitalType[10]; |
hpham33 | 2:9503a713b648 | 13 | float analogData[4]; |
hpham33 | 2:9503a713b648 | 14 | int analogType[4]; |
hpham33 | 2:9503a713b648 | 15 | struct xbee * next; // pointer to next struct |
hpham33 | 2:9503a713b648 | 16 | }; |
hpham33 | 2:9503a713b648 | 17 | |
hpham33 | 2:9503a713b648 | 18 | struct xbee* addnode(struct xbee*,unsigned int,unsigned int ); |
hpham33 | 2:9503a713b648 | 19 | int getDigitalValue(int , short ); |
hpham33 | 2:9503a713b648 | 20 | void digitalInputHandle(struct xbee* ,unsigned int , unsigned int , unsigned short ); |
hpham33 | 2:9503a713b648 | 21 | void analogInputHandle(struct xbee* ,unsigned int , unsigned int , int , float ); |
chrisisthefish | 0:c498b8bcfc46 | 22 | |
chrisisthefish | 0:c498b8bcfc46 | 23 | int main() { |
hpham33 | 2:9503a713b648 | 24 | /* This won't change, or we would lose the list in memory */ |
hpham33 | 2:9503a713b648 | 25 | struct xbee *root; |
hpham33 | 2:9503a713b648 | 26 | root->next = NULL; |
hpham33 | 2:9503a713b648 | 27 | /* The node root points to has its next pointer equal to a null pointer |
hpham33 | 2:9503a713b648 | 28 | set */ |
hpham33 | 2:9503a713b648 | 29 | struct xbee* xbee1; |
hpham33 | 2:9503a713b648 | 30 | struct xbee* xbee2; |
hpham33 | 2:9503a713b648 | 31 | struct xbee* xbee3; |
hpham33 | 2:9503a713b648 | 32 | |
hpham33 | 2:9503a713b648 | 33 | xbee1 = addnode(root,0,1); |
hpham33 | 2:9503a713b648 | 34 | xbee2 = addnode(root,0,2); |
hpham33 | 2:9503a713b648 | 35 | xbee3 = addnode(root,0,3); |
hpham33 | 2:9503a713b648 | 36 | } |
hpham33 | 2:9503a713b648 | 37 | |
hpham33 | 2:9503a713b648 | 38 | struct xbee* addnode(struct xbee* root,unsigned int addrhigh,unsigned int addrlow){ |
hpham33 | 2:9503a713b648 | 39 | |
hpham33 | 2:9503a713b648 | 40 | struct xbee* node; |
hpham33 | 2:9503a713b648 | 41 | node = root; |
hpham33 | 2:9503a713b648 | 42 | |
hpham33 | 2:9503a713b648 | 43 | if ( node != 0 ) { |
hpham33 | 2:9503a713b648 | 44 | while ( node->next != 0) |
hpham33 | 2:9503a713b648 | 45 | { |
hpham33 | 2:9503a713b648 | 46 | node = node->next; |
hpham33 | 2:9503a713b648 | 47 | } |
chrisisthefish | 0:c498b8bcfc46 | 48 | } |
hpham33 | 2:9503a713b648 | 49 | node = (struct xbee *) malloc( sizeof(struct xbee) ); |
hpham33 | 2:9503a713b648 | 50 | node->next = NULL; |
hpham33 | 2:9503a713b648 | 51 | node->addrHigh =addrhigh; |
hpham33 | 2:9503a713b648 | 52 | node->addrLow =addrlow; |
hpham33 | 2:9503a713b648 | 53 | return node; |
chrisisthefish | 0:c498b8bcfc46 | 54 | } |
hpham33 | 2:9503a713b648 | 55 | int getDigitalValue(int i, short pins){ |
hpham33 | 2:9503a713b648 | 56 | return ((pins>>i)&1); |
hpham33 | 2:9503a713b648 | 57 | } |
hpham33 | 2:9503a713b648 | 58 | float analogInputFormat(float data, int type){ |
hpham33 | 2:9503a713b648 | 59 | switch (type){ |
hpham33 | 2:9503a713b648 | 60 | case 1: |
hpham33 | 2:9503a713b648 | 61 | data = data; |
hpham33 | 2:9503a713b648 | 62 | break; |
hpham33 | 2:9503a713b648 | 63 | case 2: |
hpham33 | 2:9503a713b648 | 64 | data = data; |
hpham33 | 2:9503a713b648 | 65 | break; |
hpham33 | 2:9503a713b648 | 66 | case 3: |
hpham33 | 2:9503a713b648 | 67 | data = data; |
hpham33 | 2:9503a713b648 | 68 | break; |
hpham33 | 2:9503a713b648 | 69 | default: |
hpham33 | 2:9503a713b648 | 70 | data = data; |
hpham33 | 2:9503a713b648 | 71 | break; |
hpham33 | 2:9503a713b648 | 72 | } |
hpham33 | 2:9503a713b648 | 73 | return(data); |
hpham33 | 2:9503a713b648 | 74 | } |
hpham33 | 2:9503a713b648 | 75 | |
hpham33 | 2:9503a713b648 | 76 | void digitalInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, unsigned short data){ |
hpham33 | 2:9503a713b648 | 77 | struct xbee* node; |
hpham33 | 2:9503a713b648 | 78 | node = root; |
hpham33 | 2:9503a713b648 | 79 | |
hpham33 | 2:9503a713b648 | 80 | if ( node != 0 ) { |
hpham33 | 2:9503a713b648 | 81 | while ( node->addrHigh != addrhigh) |
hpham33 | 2:9503a713b648 | 82 | { |
hpham33 | 2:9503a713b648 | 83 | node = node->next; |
hpham33 | 2:9503a713b648 | 84 | while(node->addrLow !=addrlow){ |
hpham33 | 2:9503a713b648 | 85 | node = node->next; |
hpham33 | 2:9503a713b648 | 86 | } |
hpham33 | 2:9503a713b648 | 87 | } |
hpham33 | 2:9503a713b648 | 88 | } |
hpham33 | 2:9503a713b648 | 89 | node->digitalData = data; |
hpham33 | 2:9503a713b648 | 90 | } |
hpham33 | 2:9503a713b648 | 91 | void analogInputHandle(struct xbee* root,unsigned int addrhigh, unsigned int addrlow, int index, float data){ |
hpham33 | 2:9503a713b648 | 92 | struct xbee* node; |
hpham33 | 2:9503a713b648 | 93 | node = root; |
hpham33 | 2:9503a713b648 | 94 | |
hpham33 | 2:9503a713b648 | 95 | if ( node != 0 ) { |
hpham33 | 2:9503a713b648 | 96 | while ( node->addrHigh != addrhigh) |
hpham33 | 2:9503a713b648 | 97 | { |
hpham33 | 2:9503a713b648 | 98 | node = node->next; |
hpham33 | 2:9503a713b648 | 99 | while(node->addrLow !=addrlow){ |
hpham33 | 2:9503a713b648 | 100 | node = node->next; |
hpham33 | 2:9503a713b648 | 101 | } |
hpham33 | 2:9503a713b648 | 102 | } |
hpham33 | 2:9503a713b648 | 103 | } |
hpham33 | 2:9503a713b648 | 104 | node->analogData[index] = data; |
hpham33 | 2:9503a713b648 | 105 | } |