comparison Small_CPU/Src/uartProtocol_Co2.c @ 809:9602a7338f28

DevBugfix CO2 sensor in streaming mode: In standalone mode (no multiplexer) the CO2 sensor is working in streaming mode => it could happen that the OSTC received data before the scaling factor was requested. As result the CO2 values were calculated with a zero value. This problem has been fixed. In addition some code cleanup has been done. Unifying usage of changeBaudrate function instead of old baud selection based on protocol request of Discovery firmware.
author Ideenmodellierer
date Sun, 27 Aug 2023 20:51:13 +0200
parents e9eba334b942
children c3dd461ca3f9
comparison
equal deleted inserted replaced
808:ea3267866120 809:9602a7338f28
37 37
38 38
39 39
40 void uartCo2_SendCmd(uint8_t CO2Cmd, uint8_t *cmdString, uint8_t *cmdLength) 40 void uartCo2_SendCmd(uint8_t CO2Cmd, uint8_t *cmdString, uint8_t *cmdLength)
41 { 41 {
42 *cmdLength = 0;
43
42 switch (CO2Cmd) 44 switch (CO2Cmd)
43 { 45 {
44 case CO2CMD_MODE_POLL: *cmdLength = snprintf((char*)cmdString, 10, "K 2\r\n"); 46 case CO2CMD_MODE_POLL: *cmdLength = snprintf((char*)cmdString, 10, "K 2\r\n");
45 break; 47 break;
46 case CO2CMD_MODE_STREAM: *cmdLength = snprintf((char*)cmdString, 10, "K 1\r\n"); 48 case CO2CMD_MODE_STREAM: *cmdLength = snprintf((char*)cmdString, 10, "K 1\r\n");
78 localComState = lastComState; 80 localComState = lastComState;
79 } 81 }
80 82
81 if(localComState == UART_CO2_INIT) 83 if(localComState == UART_CO2_INIT)
82 { 84 {
85 CO2Connected = 0;
83 externalInterface_SetCO2Scale(0.0); 86 externalInterface_SetCO2Scale(0.0);
84 UART_StartDMA_Receiption(); 87 UART_StartDMA_Receiption();
85 localComState = UART_CO2_SETUP; 88 localComState = UART_CO2_SETUP;
86 } 89 }
87 if(localComState == UART_CO2_SETUP) 90 if(localComState == UART_CO2_SETUP)
98 else 101 else
99 { 102 {
100 if(localComState == UART_CO2_CALIBRATE) 103 if(localComState == UART_CO2_CALIBRATE)
101 { 104 {
102 uartCo2_SendCmd(CO2CMD_CALIBRATE, cmdString, &cmdLength); 105 uartCo2_SendCmd(CO2CMD_CALIBRATE, cmdString, &cmdLength);
106 localComState = UART_CO2_IDLE;
103 } 107 }
104 else if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* sensor is working in polling mode if mux is connected to avoid interference with other sensors */ 108 else if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* sensor is working in polling mode if mux is connected to avoid interference with other sensors */
105 { 109 {
106 //if(cmdLength == 0) /* poll data */ 110 //if(cmdLength == 0) /* poll data */
107 if(localComState == UART_CO2_IDLE) 111 if(localComState == UART_CO2_IDLE)
116 } 120 }
117 } 121 }
118 else 122 else
119 { 123 {
120 localComState = UART_CO2_OPERATING; /* sensor in streaming mode if not connected to mux => operating */ 124 localComState = UART_CO2_OPERATING; /* sensor in streaming mode if not connected to mux => operating */
125 UART_StartDMA_Receiption();
121 } 126 }
122 } 127 }
123 lastComState = localComState; 128 lastComState = localComState;
124 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState); 129 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState);
125 } 130 }
157 dataValue = dataValue * 10 + (data - '0'); 162 dataValue = dataValue * 10 + (data - '0');
158 rxState++; 163 rxState++;
159 if(rxState == RX_Data5) 164 if(rxState == RX_Data5)
160 { 165 {
161 rxState = RX_DataComplete; 166 rxState = RX_DataComplete;
162 CO2Connected = 1;
163 } 167 }
164 } 168 }
165 else /* protocol error data has max 5 digits */ 169 else /* protocol error data has max 5 digits */
166 { 170 {
171 if(rxState != RX_DataComplete) /* commands will not answer with number values */
172 {
173 rxState = RX_Ready;
174 }
175 }
176 }
177 if((data == ' ') || (data == '\n')) /* Abort data detection */
178 {
179 if(rxState == RX_DataComplete)
180 {
181 CO2Connected = 1;
182 if(localComState == UART_CO2_SETUP)
183 {
184 if(dataType == '.')
185 {
186 localComState = UART_CO2_IDLE;
187 }
188 }
189 else
190 {
191 localComState = UART_CO2_IDLE;
192 }
193 if(externalInterface_GetCO2State() == 0)
194 {
195 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);
196 }
197 switch(dataType)
198 {
199 case 'D': externalInterface_SetCO2SignalStrength(dataValue);
200 break;
201 case 'l': LED_ZeroOffset = dataValue;
202 break;
203 case 'Z': externalInterface_SetCO2Value(dataValue);
204 break;
205 case '.': externalInterface_SetCO2Scale(dataValue);
206 break;
207 default: rxState = RX_Ready;
208 break;
209 }
210 }
211 if(rxState != RX_Data0) /* reset state machine because message in wrong format */
212 {
167 rxState = RX_Ready; 213 rxState = RX_Ready;
168 } 214 }
169 } 215 }
170 if((data == ' ') || (data == '\n')) /* Abort data detection */
171 {
172 if(rxState == RX_DataComplete)
173 {
174 localComState = UART_CO2_IDLE;
175 if(externalInterface_GetCO2State() == 0)
176 {
177 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);
178 }
179 switch(dataType)
180 {
181 case 'D': externalInterface_SetCO2SignalStrength(dataValue);
182 break;
183 case 'l': LED_ZeroOffset = dataValue;
184 break;
185 case 'Z': externalInterface_SetCO2Value(dataValue);
186 break;
187 case '.': externalInterface_SetCO2Scale(dataValue);
188 break;
189 default: rxState = RX_Ready;
190 break;
191 }
192 }
193 if(rxState != RX_Data0) /* reset state machine because message in wrong format */
194 {
195 rxState = RX_Ready;
196 }
197 }
198 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState); 216 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState);
199 } 217 }
200 218
201 uint8_t uartCo2_isSensorConnected() 219 uint8_t uartCo2_isSensorConnected()
202 { 220 {