dd
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
commandt.cpp@0:9bfc4aea91e2, 2016-02-19 (annotated)
- Committer:
- sbh9428
- Date:
- Fri Feb 19 07:07:26 2016 +0000
- Revision:
- 0:9bfc4aea91e2
- Child:
- 1:5c42ec7f1aeb
temp;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sbh9428 | 0:9bfc4aea91e2 | 1 | /* |
sbh9428 | 0:9bfc4aea91e2 | 2 | * commandt.cpp |
sbh9428 | 0:9bfc4aea91e2 | 3 | * |
sbh9428 | 0:9bfc4aea91e2 | 4 | * Created on: 2016. 2. 19. |
sbh9428 | 0:9bfc4aea91e2 | 5 | * Author: sbh9428 |
sbh9428 | 0:9bfc4aea91e2 | 6 | */ |
sbh9428 | 0:9bfc4aea91e2 | 7 | |
sbh9428 | 0:9bfc4aea91e2 | 8 | #include "commandt.h" |
sbh9428 | 0:9bfc4aea91e2 | 9 | |
sbh9428 | 0:9bfc4aea91e2 | 10 | command_t::command_t() { |
sbh9428 | 0:9bfc4aea91e2 | 11 | // TODO Auto-generated constructor stub |
sbh9428 | 0:9bfc4aea91e2 | 12 | |
sbh9428 | 0:9bfc4aea91e2 | 13 | } |
sbh9428 | 0:9bfc4aea91e2 | 14 | |
sbh9428 | 0:9bfc4aea91e2 | 15 | command_t::command_t(BufferedSerial* _pc, control_t* _control) |
sbh9428 | 0:9bfc4aea91e2 | 16 | { |
sbh9428 | 0:9bfc4aea91e2 | 17 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 18 | control=_control; |
sbh9428 | 0:9bfc4aea91e2 | 19 | pc=_pc; |
sbh9428 | 0:9bfc4aea91e2 | 20 | pc->baud(115200); |
sbh9428 | 0:9bfc4aea91e2 | 21 | } |
sbh9428 | 0:9bfc4aea91e2 | 22 | |
sbh9428 | 0:9bfc4aea91e2 | 23 | command_t::~command_t() { |
sbh9428 | 0:9bfc4aea91e2 | 24 | // TODO Auto-generated destructor stub |
sbh9428 | 0:9bfc4aea91e2 | 25 | } |
sbh9428 | 0:9bfc4aea91e2 | 26 | |
sbh9428 | 0:9bfc4aea91e2 | 27 | void command_t::clear_data() |
sbh9428 | 0:9bfc4aea91e2 | 28 | { |
sbh9428 | 0:9bfc4aea91e2 | 29 | for(int i=0;i<10;i++) |
sbh9428 | 0:9bfc4aea91e2 | 30 | { |
sbh9428 | 0:9bfc4aea91e2 | 31 | data[i]=0; |
sbh9428 | 0:9bfc4aea91e2 | 32 | } |
sbh9428 | 0:9bfc4aea91e2 | 33 | } |
sbh9428 | 0:9bfc4aea91e2 | 34 | void command_t::parse() |
sbh9428 | 0:9bfc4aea91e2 | 35 | { |
sbh9428 | 0:9bfc4aea91e2 | 36 | switch(data[0]) |
sbh9428 | 0:9bfc4aea91e2 | 37 | { |
sbh9428 | 0:9bfc4aea91e2 | 38 | case 's': |
sbh9428 | 0:9bfc4aea91e2 | 39 | switch(data[1]) |
sbh9428 | 0:9bfc4aea91e2 | 40 | { |
sbh9428 | 0:9bfc4aea91e2 | 41 | case 'm': |
sbh9428 | 0:9bfc4aea91e2 | 42 | control->set_mode(data[2]-'0'); |
sbh9428 | 0:9bfc4aea91e2 | 43 | break; |
sbh9428 | 0:9bfc4aea91e2 | 44 | case 'p': |
sbh9428 | 0:9bfc4aea91e2 | 45 | control->control_PWM(asci_to_bin(data+2)); |
sbh9428 | 0:9bfc4aea91e2 | 46 | printf("set WPM duty ratio %f\n", asci_to_bin(data+2)); |
sbh9428 | 0:9bfc4aea91e2 | 47 | break; |
sbh9428 | 0:9bfc4aea91e2 | 48 | case 't': |
sbh9428 | 0:9bfc4aea91e2 | 49 | |
sbh9428 | 0:9bfc4aea91e2 | 50 | break; |
sbh9428 | 0:9bfc4aea91e2 | 51 | } |
sbh9428 | 0:9bfc4aea91e2 | 52 | break; |
sbh9428 | 0:9bfc4aea91e2 | 53 | case 'g': |
sbh9428 | 0:9bfc4aea91e2 | 54 | |
sbh9428 | 0:9bfc4aea91e2 | 55 | break; |
sbh9428 | 0:9bfc4aea91e2 | 56 | dafault: |
sbh9428 | 0:9bfc4aea91e2 | 57 | pc->printf("command error"); |
sbh9428 | 0:9bfc4aea91e2 | 58 | } |
sbh9428 | 0:9bfc4aea91e2 | 59 | pc->printf("\n"); |
sbh9428 | 0:9bfc4aea91e2 | 60 | } |
sbh9428 | 0:9bfc4aea91e2 | 61 | |
sbh9428 | 0:9bfc4aea91e2 | 62 | void command_t::get_data() |
sbh9428 | 0:9bfc4aea91e2 | 63 | { |
sbh9428 | 0:9bfc4aea91e2 | 64 | data [count]=pc->getc(); |
sbh9428 | 0:9bfc4aea91e2 | 65 | |
sbh9428 | 0:9bfc4aea91e2 | 66 | if(data [count]=='f') |
sbh9428 | 0:9bfc4aea91e2 | 67 | parse(); |
sbh9428 | 0:9bfc4aea91e2 | 68 | else |
sbh9428 | 0:9bfc4aea91e2 | 69 | count++; |
sbh9428 | 0:9bfc4aea91e2 | 70 | |
sbh9428 | 0:9bfc4aea91e2 | 71 | if(count>9) |
sbh9428 | 0:9bfc4aea91e2 | 72 | { |
sbh9428 | 0:9bfc4aea91e2 | 73 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 74 | pc->printf("command error\n"); |
sbh9428 | 0:9bfc4aea91e2 | 75 | } |
sbh9428 | 0:9bfc4aea91e2 | 76 | } |
sbh9428 | 0:9bfc4aea91e2 | 77 | |
sbh9428 | 0:9bfc4aea91e2 | 78 | void command_t::refreshPWM() |
sbh9428 | 0:9bfc4aea91e2 | 79 | { |
sbh9428 | 0:9bfc4aea91e2 | 80 | printf("temperature is %f\n", control->get_temp()); |
sbh9428 | 0:9bfc4aea91e2 | 81 | |
sbh9428 | 0:9bfc4aea91e2 | 82 | } |
sbh9428 | 0:9bfc4aea91e2 | 83 | |
sbh9428 | 0:9bfc4aea91e2 | 84 | double command_t::asci_to_bin(int *start) |
sbh9428 | 0:9bfc4aea91e2 | 85 | { |
sbh9428 | 0:9bfc4aea91e2 | 86 | double _data=0; |
sbh9428 | 0:9bfc4aea91e2 | 87 | int current=0; |
sbh9428 | 0:9bfc4aea91e2 | 88 | |
sbh9428 | 0:9bfc4aea91e2 | 89 | double nth=1; //�ڸ��� |
sbh9428 | 0:9bfc4aea91e2 | 90 | int mode=0; |
sbh9428 | 0:9bfc4aea91e2 | 91 | int sign=1; |
sbh9428 | 0:9bfc4aea91e2 | 92 | |
sbh9428 | 0:9bfc4aea91e2 | 93 | if(*start=='-') |
sbh9428 | 0:9bfc4aea91e2 | 94 | { |
sbh9428 | 0:9bfc4aea91e2 | 95 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 96 | sign=-1; |
sbh9428 | 0:9bfc4aea91e2 | 97 | } |
sbh9428 | 0:9bfc4aea91e2 | 98 | while(*(start+current)!='f'&&*(start+current)!='.') |
sbh9428 | 0:9bfc4aea91e2 | 99 | { |
sbh9428 | 0:9bfc4aea91e2 | 100 | _data*=10; |
sbh9428 | 0:9bfc4aea91e2 | 101 | _data+=*(start+current)-'0'; |
sbh9428 | 0:9bfc4aea91e2 | 102 | |
sbh9428 | 0:9bfc4aea91e2 | 103 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 104 | } |
sbh9428 | 0:9bfc4aea91e2 | 105 | if(*(start+current)=='.') |
sbh9428 | 0:9bfc4aea91e2 | 106 | { |
sbh9428 | 0:9bfc4aea91e2 | 107 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 108 | while(*(start+current)!='f') |
sbh9428 | 0:9bfc4aea91e2 | 109 | { |
sbh9428 | 0:9bfc4aea91e2 | 110 | nth/=10; |
sbh9428 | 0:9bfc4aea91e2 | 111 | _data+=nth*(*(start+current)-'0'); |
sbh9428 | 0:9bfc4aea91e2 | 112 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 113 | } |
sbh9428 | 0:9bfc4aea91e2 | 114 | } |
sbh9428 | 0:9bfc4aea91e2 | 115 | return sign*_data; |
sbh9428 | 0:9bfc4aea91e2 | 116 | } |