テスト
Dependencies: RemoteIR TB6612FNG2 mbed
Fork of RemoteIR_TestProgram by
main.cpp@6:9bddde38e25e, 2015-12-23 (annotated)
- Committer:
- mbed_Cookbook_SE
- Date:
- Wed Dec 23 16:59:17 2015 +0000
- Revision:
- 6:9bddde38e25e
- Parent:
- 5:40750d5779ca
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_Cookbook_SE | 6:9bddde38e25e | 1 | #include "mbed.h" |
shintamainjp | 2:9e3081533f70 | 2 | |
shintamainjp | 2:9e3081533f70 | 3 | #include "ReceiverIR.h" |
shintamainjp | 2:9e3081533f70 | 4 | #include "TransmitterIR.h" |
mbed_Cookbook_SE | 6:9bddde38e25e | 5 | #include "TB6612.h" |
shintamainjp | 2:9e3081533f70 | 6 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 7 | ReceiverIR ir_rx(p14); |
mbed_Cookbook_SE | 6:9bddde38e25e | 8 | TransmitterIR ir_tx(p23); |
mbed_Cookbook_SE | 6:9bddde38e25e | 9 | TB6612 MOTOR_A(p21,p19,p20); // PWM IN1 IN2 |
mbed_Cookbook_SE | 6:9bddde38e25e | 10 | TB6612 MOTOR_B(p22,p29,p30); // PWM IN1 IN2 |
mbed_Cookbook_SE | 6:9bddde38e25e | 11 | |
shintamainjp | 2:9e3081533f70 | 12 | int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) { |
shintamainjp | 2:9e3081533f70 | 13 | int cnt = 0; |
shintamainjp | 2:9e3081533f70 | 14 | while (ir_rx.getState() != ReceiverIR::Received) { |
shintamainjp | 2:9e3081533f70 | 15 | cnt++; |
shintamainjp | 2:9e3081533f70 | 16 | if (timeout < cnt) { |
shintamainjp | 2:9e3081533f70 | 17 | return -1; |
shintamainjp | 2:9e3081533f70 | 18 | } |
shintamainjp | 2:9e3081533f70 | 19 | } |
shintamainjp | 2:9e3081533f70 | 20 | return ir_rx.getData(format, buf, bufsiz * 8); |
shintamainjp | 2:9e3081533f70 | 21 | } |
shintamainjp | 2:9e3081533f70 | 22 | |
shintamainjp | 2:9e3081533f70 | 23 | int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) { |
shintamainjp | 2:9e3081533f70 | 24 | int cnt = 0; |
shintamainjp | 2:9e3081533f70 | 25 | while (ir_tx.getState() != TransmitterIR::Idle) { |
shintamainjp | 2:9e3081533f70 | 26 | cnt++; |
shintamainjp | 2:9e3081533f70 | 27 | if (timeout < cnt) { |
shintamainjp | 2:9e3081533f70 | 28 | return -1; |
shintamainjp | 2:9e3081533f70 | 29 | } |
shintamainjp | 2:9e3081533f70 | 30 | } |
shintamainjp | 2:9e3081533f70 | 31 | return ir_tx.setData(format, buf, bitlength); |
shintamainjp | 2:9e3081533f70 | 32 | } |
shintamainjp | 2:9e3081533f70 | 33 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 34 | int SearchCode( uint8_t *code ,uint8_t **buf , int size) |
mbed_Cookbook_SE | 6:9bddde38e25e | 35 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 36 | int ret = -1; |
mbed_Cookbook_SE | 6:9bddde38e25e | 37 | for(int i=0;i<5;i++) |
mbed_Cookbook_SE | 6:9bddde38e25e | 38 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 39 | if(memcmp( code , buf[i] , size ) == 0 ){ |
mbed_Cookbook_SE | 6:9bddde38e25e | 40 | ret = i; |
mbed_Cookbook_SE | 6:9bddde38e25e | 41 | break; |
mbed_Cookbook_SE | 6:9bddde38e25e | 42 | } |
mbed_Cookbook_SE | 6:9bddde38e25e | 43 | } |
mbed_Cookbook_SE | 6:9bddde38e25e | 44 | return(ret); |
shintamainjp | 2:9e3081533f70 | 45 | } |
shintamainjp | 2:9e3081533f70 | 46 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 47 | void RegistrationCode(uint8_t *forward ,uint8_t *back ,uint8_t *left ,uint8_t *right ,uint8_t *stop) |
mbed_Cookbook_SE | 6:9bddde38e25e | 48 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 49 | uint8_t buf[32]; |
mbed_Cookbook_SE | 6:9bddde38e25e | 50 | int bitlength; |
mbed_Cookbook_SE | 6:9bddde38e25e | 51 | RemoteIR::Format format; |
mbed_Cookbook_SE | 6:9bddde38e25e | 52 | uint8_t *ir_buf[5] = {forward,back,left,right,stop}; |
mbed_Cookbook_SE | 6:9bddde38e25e | 53 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 54 | memset(buf, 0x00, sizeof(buf)); |
mbed_Cookbook_SE | 6:9bddde38e25e | 55 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 56 | for(int i=0;i<5;i++) |
mbed_Cookbook_SE | 6:9bddde38e25e | 57 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 58 | bitlength = -1; |
mbed_Cookbook_SE | 6:9bddde38e25e | 59 | while(bitlength < 0) |
mbed_Cookbook_SE | 6:9bddde38e25e | 60 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 61 | bitlength = receive(&format, buf, sizeof(buf)); |
mbed_Cookbook_SE | 6:9bddde38e25e | 62 | if (bitlength < 0) { |
mbed_Cookbook_SE | 6:9bddde38e25e | 63 | continue; |
mbed_Cookbook_SE | 6:9bddde38e25e | 64 | } |
mbed_Cookbook_SE | 6:9bddde38e25e | 65 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 66 | if( SearchCode(buf,ir_buf,sizeof(buf)) != -1 ) |
mbed_Cookbook_SE | 6:9bddde38e25e | 67 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 68 | bitlength = -1; |
mbed_Cookbook_SE | 6:9bddde38e25e | 69 | continue; |
mbed_Cookbook_SE | 6:9bddde38e25e | 70 | } |
shintamainjp | 2:9e3081533f70 | 71 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 72 | memcpy( ir_buf[i] , buf , sizeof(buf)); |
mbed_Cookbook_SE | 6:9bddde38e25e | 73 | } |
shintamainjp | 2:9e3081533f70 | 74 | } |
shintamainjp | 2:9e3081533f70 | 75 | } |
shintamainjp | 2:9e3081533f70 | 76 | |
shintamainjp | 2:9e3081533f70 | 77 | int main(void) { |
mbed_Cookbook_SE | 6:9bddde38e25e | 78 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 79 | uint8_t forward[32],back[32],left[32],right[32],stop[32]; |
mbed_Cookbook_SE | 6:9bddde38e25e | 80 | uint8_t *ir_buf[5] = {forward,back,left,right,stop}; |
shintamainjp | 2:9e3081533f70 | 81 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 82 | RegistrationCode( forward ,back ,left ,right ,stop); |
shintamainjp | 2:9e3081533f70 | 83 | |
shintamainjp | 2:9e3081533f70 | 84 | while (1) { |
mbed_Cookbook_SE | 6:9bddde38e25e | 85 | uint8_t buf[32]; |
shintamainjp | 2:9e3081533f70 | 86 | int bitlength1; |
shintamainjp | 2:9e3081533f70 | 87 | RemoteIR::Format format; |
shintamainjp | 2:9e3081533f70 | 88 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 89 | memset(buf, 0x00, sizeof(buf)); |
shintamainjp | 2:9e3081533f70 | 90 | |
mbed_Cookbook_SE | 6:9bddde38e25e | 91 | bitlength1 = receive(&format, buf, sizeof(buf)); |
mbed_Cookbook_SE | 6:9bddde38e25e | 92 | if (bitlength1 < 0) { |
mbed_Cookbook_SE | 6:9bddde38e25e | 93 | continue; |
shintamainjp | 2:9e3081533f70 | 94 | } |
mbed_Cookbook_SE | 6:9bddde38e25e | 95 | switch(SearchCode(buf,ir_buf,sizeof(buf))) |
shintamainjp | 2:9e3081533f70 | 96 | { |
mbed_Cookbook_SE | 6:9bddde38e25e | 97 | case 0: // forward |
mbed_Cookbook_SE | 6:9bddde38e25e | 98 | MOTOR_A = 100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 99 | MOTOR_B = 100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 100 | break; |
mbed_Cookbook_SE | 6:9bddde38e25e | 101 | case 1: // back |
mbed_Cookbook_SE | 6:9bddde38e25e | 102 | MOTOR_A = -100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 103 | MOTOR_B = -100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 104 | break; |
mbed_Cookbook_SE | 6:9bddde38e25e | 105 | case 2: // left |
mbed_Cookbook_SE | 6:9bddde38e25e | 106 | MOTOR_A = 100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 107 | MOTOR_B = -100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 108 | break; |
mbed_Cookbook_SE | 6:9bddde38e25e | 109 | case 3: // right |
mbed_Cookbook_SE | 6:9bddde38e25e | 110 | MOTOR_A = -100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 111 | MOTOR_B = 100; |
mbed_Cookbook_SE | 6:9bddde38e25e | 112 | break; |
mbed_Cookbook_SE | 6:9bddde38e25e | 113 | case 4: // stop |
mbed_Cookbook_SE | 6:9bddde38e25e | 114 | MOTOR_A = 0; |
mbed_Cookbook_SE | 6:9bddde38e25e | 115 | MOTOR_B = 0; |
mbed_Cookbook_SE | 6:9bddde38e25e | 116 | break; |
mbed_Cookbook_SE | 6:9bddde38e25e | 117 | default: |
mbed_Cookbook_SE | 6:9bddde38e25e | 118 | break; |
shintamainjp | 2:9e3081533f70 | 119 | } |
shintamainjp | 2:9e3081533f70 | 120 | } |
shintamainjp | 0:d081f6c767da | 121 | } |