dd
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
commandt.cpp@10:c751a0e8b7f9, 2016-07-11 (annotated)
- Committer:
- sbh9428
- Date:
- Mon Jul 11 01:05:52 2016 +0000
- Revision:
- 10:c751a0e8b7f9
- Parent:
- 4:7ca449fca19b
fff;
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 | 1:5c42ec7f1aeb | 17 | time=0; |
sbh9428 | 0:9bfc4aea91e2 | 18 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 19 | control=_control; |
sbh9428 | 0:9bfc4aea91e2 | 20 | pc=_pc; |
sbh9428 | 0:9bfc4aea91e2 | 21 | pc->baud(115200); |
sbh9428 | 0:9bfc4aea91e2 | 22 | } |
sbh9428 | 0:9bfc4aea91e2 | 23 | |
sbh9428 | 0:9bfc4aea91e2 | 24 | command_t::~command_t() { |
sbh9428 | 0:9bfc4aea91e2 | 25 | // TODO Auto-generated destructor stub |
sbh9428 | 0:9bfc4aea91e2 | 26 | } |
sbh9428 | 0:9bfc4aea91e2 | 27 | |
sbh9428 | 0:9bfc4aea91e2 | 28 | void command_t::clear_data() |
sbh9428 | 0:9bfc4aea91e2 | 29 | { |
sbh9428 | 0:9bfc4aea91e2 | 30 | for(int i=0;i<10;i++) |
sbh9428 | 0:9bfc4aea91e2 | 31 | { |
sbh9428 | 0:9bfc4aea91e2 | 32 | data[i]=0; |
sbh9428 | 0:9bfc4aea91e2 | 33 | } |
sbh9428 | 0:9bfc4aea91e2 | 34 | } |
sbh9428 | 0:9bfc4aea91e2 | 35 | void command_t::parse() |
sbh9428 | 0:9bfc4aea91e2 | 36 | { |
sbh9428 | 0:9bfc4aea91e2 | 37 | switch(data[0]) |
sbh9428 | 0:9bfc4aea91e2 | 38 | { |
sbh9428 | 0:9bfc4aea91e2 | 39 | case 's': |
sbh9428 | 0:9bfc4aea91e2 | 40 | switch(data[1]) |
sbh9428 | 0:9bfc4aea91e2 | 41 | { |
sbh9428 | 0:9bfc4aea91e2 | 42 | case 'm': |
sbh9428 | 0:9bfc4aea91e2 | 43 | control->set_mode(data[2]-'0'); |
sbh9428 | 1:5c42ec7f1aeb | 44 | printf("set mode %d\n", data[2]-'0'); |
sbh9428 | 0:9bfc4aea91e2 | 45 | break; |
sbh9428 | 1:5c42ec7f1aeb | 46 | case 'r': |
sbh9428 | 0:9bfc4aea91e2 | 47 | control->control_PWM(asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 48 | control->set_PWM_value(asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 49 | printf("set PWM duty ratio %f1.5\n", asci_to_bin(data+2)); |
sbh9428 | 0:9bfc4aea91e2 | 50 | break; |
sbh9428 | 0:9bfc4aea91e2 | 51 | case 't': |
sbh9428 | 1:5c42ec7f1aeb | 52 | control->set_target_temp(asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 53 | printf("set target temp %f\n", asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 54 | break; |
sbh9428 | 1:5c42ec7f1aeb | 55 | case 'p': |
sbh9428 | 1:5c42ec7f1aeb | 56 | control->set_P_value(asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 57 | printf("set P value %f\n", asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 58 | break; |
sbh9428 | 1:5c42ec7f1aeb | 59 | case 'i': |
sbh9428 | 1:5c42ec7f1aeb | 60 | control->set_I_value(asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 61 | printf("set I value %f\n", asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 62 | break; |
sbh9428 | 1:5c42ec7f1aeb | 63 | case 'd': |
sbh9428 | 1:5c42ec7f1aeb | 64 | control->set_D_value(asci_to_bin(data+2)); |
sbh9428 | 1:5c42ec7f1aeb | 65 | printf("set D value %f\n", asci_to_bin(data+2)); |
sbh9428 | 0:9bfc4aea91e2 | 66 | break; |
sbh9428 | 3:72644690e2e6 | 67 | case 'e': |
sbh9428 | 3:72644690e2e6 | 68 | control->set_period((int)asci_to_bin(data+2)); |
sbh9428 | 3:72644690e2e6 | 69 | printf("set period %d\n", (int)asci_to_bin(data+2)); |
sbh9428 | 10:c751a0e8b7f9 | 70 | break; |
sbh9428 | 10:c751a0e8b7f9 | 71 | case 'h': |
sbh9428 | 10:c751a0e8b7f9 | 72 | control->highTemp=asci_to_bin(data+2); |
sbh9428 | 10:c751a0e8b7f9 | 73 | printf("set high temp %2.2f\n", asci_to_bin(data+2)); |
sbh9428 | 10:c751a0e8b7f9 | 74 | break; |
sbh9428 | 10:c751a0e8b7f9 | 75 | case 'l': |
sbh9428 | 10:c751a0e8b7f9 | 76 | control->lowTemp=asci_to_bin(data+2); |
sbh9428 | 10:c751a0e8b7f9 | 77 | printf("set low temp %2.2f\n", asci_to_bin(data+2)); |
sbh9428 | 10:c751a0e8b7f9 | 78 | break; |
sbh9428 | 10:c751a0e8b7f9 | 79 | case 'a': |
sbh9428 | 10:c751a0e8b7f9 | 80 | control->highTempTime=(int)asci_to_bin(data+2); |
sbh9428 | 10:c751a0e8b7f9 | 81 | printf("set high temp time%d\n", (int)asci_to_bin(data+2)); |
sbh9428 | 10:c751a0e8b7f9 | 82 | break; |
sbh9428 | 10:c751a0e8b7f9 | 83 | case 'b': |
sbh9428 | 10:c751a0e8b7f9 | 84 | control->lowTempTime=(int)asci_to_bin(data+2); |
sbh9428 | 10:c751a0e8b7f9 | 85 | printf("set low temp %d\n", (int)asci_to_bin(data+2)); |
sbh9428 | 10:c751a0e8b7f9 | 86 | break; |
sbh9428 | 10:c751a0e8b7f9 | 87 | case 'o': |
sbh9428 | 10:c751a0e8b7f9 | 88 | control->periodNumber=(int)asci_to_bin(data+2); |
sbh9428 | 10:c751a0e8b7f9 | 89 | printf("set period repeat number %d \n", (int)asci_to_bin(data+2)); |
sbh9428 | 3:72644690e2e6 | 90 | break; |
sbh9428 | 3:72644690e2e6 | 91 | default: |
sbh9428 | 3:72644690e2e6 | 92 | pc->printf("command error"); |
sbh9428 | 0:9bfc4aea91e2 | 93 | } |
sbh9428 | 0:9bfc4aea91e2 | 94 | break; |
sbh9428 | 0:9bfc4aea91e2 | 95 | case 'g': |
sbh9428 | 1:5c42ec7f1aeb | 96 | switch(data[1]) |
sbh9428 | 1:5c42ec7f1aeb | 97 | { |
sbh9428 | 1:5c42ec7f1aeb | 98 | case 'm': |
sbh9428 | 1:5c42ec7f1aeb | 99 | printf("mode is %d\n", control->get_mode()); |
sbh9428 | 1:5c42ec7f1aeb | 100 | break; |
sbh9428 | 1:5c42ec7f1aeb | 101 | case 'r': |
sbh9428 | 1:5c42ec7f1aeb | 102 | printf("duty ratio is %f\n", control->get_PWM_value()); |
sbh9428 | 1:5c42ec7f1aeb | 103 | break; |
sbh9428 | 1:5c42ec7f1aeb | 104 | case 't': |
sbh9428 | 1:5c42ec7f1aeb | 105 | printf("target temp is %f\n", control->get_target_temp()); |
sbh9428 | 1:5c42ec7f1aeb | 106 | break; |
sbh9428 | 1:5c42ec7f1aeb | 107 | case 'p': |
sbh9428 | 1:5c42ec7f1aeb | 108 | printf("P value is %f\n", control->get_P_value()); |
sbh9428 | 1:5c42ec7f1aeb | 109 | break; |
sbh9428 | 1:5c42ec7f1aeb | 110 | case 'i': |
sbh9428 | 1:5c42ec7f1aeb | 111 | printf("I value is %f\n", control->get_I_value()); |
sbh9428 | 1:5c42ec7f1aeb | 112 | break; |
sbh9428 | 1:5c42ec7f1aeb | 113 | case 'd': |
sbh9428 | 1:5c42ec7f1aeb | 114 | printf("D value is %f\n", control->get_P_value()); |
sbh9428 | 1:5c42ec7f1aeb | 115 | break; |
sbh9428 | 3:72644690e2e6 | 116 | case 'e': |
sbh9428 | 3:72644690e2e6 | 117 | printf("Period is %d\n", control->get_period()); |
sbh9428 | 3:72644690e2e6 | 118 | break; |
sbh9428 | 3:72644690e2e6 | 119 | case 'a': |
sbh9428 | 3:72644690e2e6 | 120 | control->print_table(); |
sbh9428 | 3:72644690e2e6 | 121 | break; |
sbh9428 | 3:72644690e2e6 | 122 | default: |
sbh9428 | 3:72644690e2e6 | 123 | pc->printf("command error"); |
sbh9428 | 1:5c42ec7f1aeb | 124 | } |
sbh9428 | 0:9bfc4aea91e2 | 125 | |
sbh9428 | 0:9bfc4aea91e2 | 126 | break; |
sbh9428 | 0:9bfc4aea91e2 | 127 | dafault: |
sbh9428 | 0:9bfc4aea91e2 | 128 | pc->printf("command error"); |
sbh9428 | 0:9bfc4aea91e2 | 129 | } |
sbh9428 | 0:9bfc4aea91e2 | 130 | pc->printf("\n"); |
sbh9428 | 1:5c42ec7f1aeb | 131 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 132 | } |
sbh9428 | 0:9bfc4aea91e2 | 133 | |
sbh9428 | 0:9bfc4aea91e2 | 134 | void command_t::get_data() |
sbh9428 | 0:9bfc4aea91e2 | 135 | { |
sbh9428 | 0:9bfc4aea91e2 | 136 | data [count]=pc->getc(); |
sbh9428 | 0:9bfc4aea91e2 | 137 | |
sbh9428 | 0:9bfc4aea91e2 | 138 | if(data [count]=='f') |
sbh9428 | 0:9bfc4aea91e2 | 139 | parse(); |
sbh9428 | 0:9bfc4aea91e2 | 140 | else |
sbh9428 | 0:9bfc4aea91e2 | 141 | count++; |
sbh9428 | 0:9bfc4aea91e2 | 142 | |
sbh9428 | 0:9bfc4aea91e2 | 143 | if(count>9) |
sbh9428 | 0:9bfc4aea91e2 | 144 | { |
sbh9428 | 0:9bfc4aea91e2 | 145 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 146 | pc->printf("command error\n"); |
sbh9428 | 0:9bfc4aea91e2 | 147 | } |
sbh9428 | 0:9bfc4aea91e2 | 148 | } |
sbh9428 | 0:9bfc4aea91e2 | 149 | |
sbh9428 | 0:9bfc4aea91e2 | 150 | void command_t::refreshPWM() |
sbh9428 | 0:9bfc4aea91e2 | 151 | { |
sbh9428 | 4:7ca449fca19b | 152 | time=time+3; |
sbh9428 | 1:5c42ec7f1aeb | 153 | control->refresh_PWM(); |
sbh9428 | 0:9bfc4aea91e2 | 154 | } |
sbh9428 | 0:9bfc4aea91e2 | 155 | |
sbh9428 | 0:9bfc4aea91e2 | 156 | double command_t::asci_to_bin(int *start) |
sbh9428 | 0:9bfc4aea91e2 | 157 | { |
sbh9428 | 0:9bfc4aea91e2 | 158 | double _data=0; |
sbh9428 | 0:9bfc4aea91e2 | 159 | int current=0; |
sbh9428 | 0:9bfc4aea91e2 | 160 | |
sbh9428 | 0:9bfc4aea91e2 | 161 | double nth=1; //�ڸ��� |
sbh9428 | 0:9bfc4aea91e2 | 162 | int mode=0; |
sbh9428 | 0:9bfc4aea91e2 | 163 | int sign=1; |
sbh9428 | 0:9bfc4aea91e2 | 164 | |
sbh9428 | 0:9bfc4aea91e2 | 165 | if(*start=='-') |
sbh9428 | 0:9bfc4aea91e2 | 166 | { |
sbh9428 | 0:9bfc4aea91e2 | 167 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 168 | sign=-1; |
sbh9428 | 0:9bfc4aea91e2 | 169 | } |
sbh9428 | 0:9bfc4aea91e2 | 170 | while(*(start+current)!='f'&&*(start+current)!='.') |
sbh9428 | 0:9bfc4aea91e2 | 171 | { |
sbh9428 | 0:9bfc4aea91e2 | 172 | _data*=10; |
sbh9428 | 0:9bfc4aea91e2 | 173 | _data+=*(start+current)-'0'; |
sbh9428 | 0:9bfc4aea91e2 | 174 | |
sbh9428 | 0:9bfc4aea91e2 | 175 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 176 | } |
sbh9428 | 0:9bfc4aea91e2 | 177 | if(*(start+current)=='.') |
sbh9428 | 0:9bfc4aea91e2 | 178 | { |
sbh9428 | 0:9bfc4aea91e2 | 179 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 180 | while(*(start+current)!='f') |
sbh9428 | 0:9bfc4aea91e2 | 181 | { |
sbh9428 | 0:9bfc4aea91e2 | 182 | nth/=10; |
sbh9428 | 0:9bfc4aea91e2 | 183 | _data+=nth*(*(start+current)-'0'); |
sbh9428 | 0:9bfc4aea91e2 | 184 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 185 | } |
sbh9428 | 0:9bfc4aea91e2 | 186 | } |
sbh9428 | 0:9bfc4aea91e2 | 187 | return sign*_data; |
sbh9428 | 0:9bfc4aea91e2 | 188 | } |