LibDriver AOX4000
Loading...
Searching...
No Matches
driver_aox4000.c
Go to the documentation of this file.
1
36
37#include "driver_aox4000.h"
38
42#define CHIP_NAME "ASAIR AOX4000"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 4.75f
45#define SUPPLY_VOLTAGE_MAX 5.25f
46#define MAX_CURRENT 32.0f
47#define TEMPERATURE_MIN -30.0f
48#define TEMPERATURE_MAX 60.0f
49#define DRIVER_VERSION 1000
50
54#define AOX4000_COMMAND_MODE "M"
55#define AOX4000_COMMAND_OXYGEN_PPO2 "O"
56#define AOX4000_COMMAND_OXYGEN_DENSITY "%"
57#define AOX4000_COMMAND_TEMPERATURE "T"
58#define AOX4000_COMMAND_PRESSURE "P"
59#define AOX4000_COMMAND_ALL "A"
60#define AOX4000_COMMAND_ERROR "E"
61
76static uint8_t a_aox4000_write_read(aox4000_handle_t *handle, char *input, uint16_t input_len, char *output, uint16_t *output_len)
77{
78 uint8_t res;
79
80 res = handle->uart_flush(); /* flush */
81 if (res != 0) /* check result */
82 {
83 return 1; /* return error */
84 }
85 res = handle->uart_write((uint8_t *)input, input_len); /* write data */
86 if (res != 0) /* check result */
87 {
88 return 1; /* return error */
89 }
90 handle->delay_ms(AOX4000_UART_DELAY_MS); /* delay */
91 *output_len = handle->uart_read((uint8_t *)output, *output_len); /* read data */
92
93 return 0; /* success return 0 */
94}
95
108static uint8_t a_aox4000_read(aox4000_handle_t *handle, char *output, uint16_t *output_len)
109{
110 uint8_t res;
111
112 res = handle->uart_flush(); /* flush */
113 if (res != 0) /* check result */
114 {
115 return 1; /* return error */
116 }
117 handle->delay_ms(AOX4000_UART_POLL_DELAY_MS); /* delay */
118 *output_len = handle->uart_read((uint8_t *)output, *output_len); /* read data */
119
120 return 0; /* success return 0 */
121}
122
135static uint8_t aox4000_parse_mode(aox4000_handle_t *handle, char *input, uint8_t *mode)
136{
137 if (strstr(input, AOX4000_COMMAND_MODE) != NULL) /* check m */
138 {
139 char buf[8];
140
141 memset(buf, 0, sizeof(char) * 8); /* clear buffer */
142 buf[0] = input[2]; /* set buf[0] */
143 buf[1] = input[3]; /* set buf[1] */
144 handle->last_error = (uint8_t)AOX4000_STATUS_OK; /* set last error */
145 *mode = (uint8_t)atoi(buf); /* convert mode */
146
147 return 0; /* success return 0 */
148 }
149 if (strstr(input, AOX4000_COMMAND_ERROR) != NULL) /* check e */
150 {
151 char buf[8];
152
153 memset(buf, 0, sizeof(char) * 8); /* clear buffer */
154 buf[0] = input[2]; /* set buf[0] */
155 buf[1] = input[3]; /* set buf[1] */
156 handle->last_error = (uint8_t)atoi(buf); /* set last error */
157
158 return 1; /* return error */
159 }
160
161 handle->last_error = (uint8_t)AOX4000_STATUS_UNKNOWN; /* set unknown */
162
163 return 1; /* return error */
164}
165
178static uint8_t aox4000_parse_number(aox4000_handle_t *handle, const char *command, char *input, float *number)
179{
180 if (strstr(input, command) != NULL) /* check command */
181 {
182 uint8_t i;
183 char buf[16];
184
185 memset(buf, 0, sizeof(char) * 16); /* clear buffer */
186 for (i = 0; i < 15; i++) /* copy all */
187 {
188 if (((input[2 + i] >= '0') &&
189 (input[2 + i] <= '9')) ||
190 (input[2 + i] == '.') ||
191 (input[2 + i] == '+') ||
192 (input[2 + i] == '-')) /* check end */
193 {
194 buf[i] = input[2 + i]; /* save to buffer */
195 }
196 else
197 {
198 break; /* break */
199 }
200 }
201 handle->last_error = (uint8_t)AOX4000_STATUS_OK; /* set last error */
202 *number = (float)atof(buf); /* convert mode */
203
204 return 0; /* success return 0 */
205 }
206 if (strstr(input, AOX4000_COMMAND_ERROR) != NULL) /* check e */
207 {
208 char buf[8];
209
210 memset(buf, 0, sizeof(char) * 8); /* clear buffer */
211 buf[0] = input[2]; /* set buf[0] */
212 buf[1] = input[3]; /* set buf[1] */
213 handle->last_error = (uint8_t)atoi(buf); /* set last error */
214
215 return 1; /* return error */
216 }
217
218 handle->last_error = (uint8_t)AOX4000_STATUS_UNKNOWN; /* set unknown */
219
220 return 1; /* return error */
221}
222
238static uint8_t aox4000_parse_data(aox4000_handle_t *handle, char *input, float *oxygen_ppo2_mbar,
239 float *oxygen_density_percentage, float *temperature_degree, float *pressure_mbar)
240{
241 uint8_t res;
242 char *p;
243
244 if (strstr(input, AOX4000_COMMAND_ERROR) != NULL) /* check e */
245 {
246 char buf[8];
247
248 memset(buf, 0, sizeof(char) * 8); /* clear buffer */
249 buf[0] = input[2]; /* set buf[0] */
250 buf[1] = input[3]; /* set buf[1] */
251 handle->last_error = (uint8_t)atoi(buf); /* set last error */
252
253 return 1; /* return error */
254 }
255
256 p = strstr(input, AOX4000_COMMAND_OXYGEN_PPO2); /* find oxygen ppo2 */
257 if (p != NULL) /* check oxygen ppo2 */
258 {
259 res = aox4000_parse_number(handle, AOX4000_COMMAND_OXYGEN_PPO2,
260 p, oxygen_ppo2_mbar); /* parse number*/
261 if (res != 0) /* check result */
262 {
263 return 1; /* return error */
264 }
265 }
266 else
267 {
268 return 1; /* return error */
269 }
270
271 p = strstr(input, AOX4000_COMMAND_OXYGEN_DENSITY); /* find oxygen density */
272 if (p != NULL) /* check oxygen density */
273 {
274 res = aox4000_parse_number(handle, AOX4000_COMMAND_OXYGEN_DENSITY,
275 p, oxygen_density_percentage); /* parse number*/
276 if (res != 0) /* check result */
277 {
278 return 1; /* return error */
279 }
280 }
281 else
282 {
283 return 1; /* return error */
284 }
285
286 p = strstr(input, AOX4000_COMMAND_TEMPERATURE); /* find temperature */
287 if (p != NULL) /* check temperature */
288 {
289 res = aox4000_parse_number(handle, AOX4000_COMMAND_TEMPERATURE,
290 p, temperature_degree); /* parse number*/
291 if (res != 0) /* check result */
292 {
293 return 1; /* return error */
294 }
295 }
296 else
297 {
298 return 1; /* return error */
299 }
300
301 p = strstr(input, AOX4000_COMMAND_PRESSURE); /* find pressure */
302 if (p != NULL) /* check pressure */
303 {
304 res = aox4000_parse_number(handle, AOX4000_COMMAND_PRESSURE,
305 p, pressure_mbar); /* parse number*/
306 if (res != 0) /* check result */
307 {
308 return 1; /* return error */
309 }
310 }
311 else
312 {
313 return 1; /* return error */
314 }
315
316 handle->last_error = (uint8_t)AOX4000_STATUS_UNKNOWN; /* set unknown */
317
318 return 0; /* success return 0 */
319}
320
332{
333 if (handle == NULL) /* check handle */
334 {
335 return 2; /* return error */
336 }
337 if (handle->debug_print == NULL) /* check debug_print */
338 {
339 return 3; /* return error */
340 }
341 if (handle->uart_init == NULL) /* check uart_init */
342 {
343 handle->debug_print("aox4000: uart_init is null.\n"); /* uart_init is null */
344
345 return 3; /* return error */
346 }
347 if (handle->uart_deinit == NULL) /* check uart_deinit */
348 {
349 handle->debug_print("aox4000: uart_deinit is null.\n"); /* uart_deinit is null */
350
351 return 3; /* return error */
352 }
353 if (handle->uart_read == NULL) /* check uart_read */
354 {
355 handle->debug_print("aox4000: uart_read is null.\n"); /* uart_read is null */
356
357 return 3; /* return error */
358 }
359 if (handle->uart_write == NULL) /* check uart_write */
360 {
361 handle->debug_print("aox4000: uart_write is null.\n"); /* uart_write is null */
362
363 return 3; /* return error */
364 }
365 if (handle->uart_flush == NULL) /* check uart_flush */
366 {
367 handle->debug_print("aox4000: uart_flush is null.\n"); /* uart_flush is null */
368
369 return 3; /* return error */
370 }
371 if (handle->delay_ms == NULL) /* check delay_ms */
372 {
373 handle->debug_print("aox4000: delay_ms is null.\n"); /* delay_ms is null */
374
375 return 3; /* return error */
376 }
377
378 if (handle->uart_init() != 0) /* uart init */
379 {
380 handle->debug_print("aox4000: uart init failed.\n"); /* uart init failed */
381
382 return 1; /* return error */
383 }
384 handle->mode = (uint8_t)(AOX4000_MODE_REQUEST); /* set request mode */
385 handle->inited = 1; /* flag finish initialization */
386
387 return 0; /* success return 0 */
388}
389
401{
402 if (handle == NULL) /* check handle */
403 {
404 return 2; /* return error */
405 }
406 if (handle->inited != 1) /* check handle initialization */
407 {
408 return 3; /* return error */
409 }
410
411 if (handle->uart_deinit() != 0) /* uart deinit */
412 {
413 handle->debug_print("aox4000: uart deinit failed.\n"); /* uart deinit failed */
414
415 return 1; /* return error */
416 }
417 handle->inited = 0; /* flag close */
418
419 return 0; /* success return 0 */
420}
421
433{
434 if (handle == NULL) /* check handle */
435 {
436 return 2; /* return error */
437 }
438 if (handle->inited != 1) /* check handle initialization */
439 {
440 return 3; /* return error */
441 }
442
443 *status = (aox4000_status_t)(handle->last_error); /* get last error */
444
445 return 0; /* success return 0 */
446}
447
460{
461 uint8_t res;
462 uint16_t input_len;
463 uint16_t output_len;
464 uint8_t buf[1];
465 char input[16];
466 char output[16];
467
468 if (handle == NULL) /* check handle */
469 {
470 return 2; /* return error */
471 }
472 if (handle->inited != 1) /* check handle initialization */
473 {
474 return 3; /* return error */
475 }
476
477 if (mode == AOX4000_MODE_AUTO) /* auto mode */
478 {
479 memset(input, 0, sizeof(char) * 16); /* init input buffer */
480 memset(output, 0, sizeof(char) * 16); /* init output buffer */
481 input_len = (uint16_t)snprintf(input, 15, "%s 0\r\n", AOX4000_COMMAND_MODE); /* get input length */
482 }
483 else /* request mode */
484 {
485 memset(input, 0, sizeof(char) * 16); /* init input buffer */
486 memset(output, 0, sizeof(char) * 16); /* init output buffer */
487 input_len = (uint16_t)snprintf(input, 15, "%s 1\r\n", AOX4000_COMMAND_MODE); /* get input length */
488 }
489
490 output_len = 15; /* set 15 */
491 res = a_aox4000_write_read(handle, input, input_len, output, &output_len); /* write read */
492 if (res != 0) /* check result */
493 {
494 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
495
496 return 1; /* return error */
497 }
498 if (output_len < 5) /* check length */
499 {
500 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
501
502 return 1; /* return error */
503 }
504 res = aox4000_parse_mode(handle, output, buf); /* parse the mode */
505 if (res != 0) /* check result */
506 {
507 handle->debug_print("aox4000: response error failed.\n"); /* response error failed */
508
509 return 1; /* return error */
510 }
511 if (buf[0] != (uint8_t)(mode)) /* check response */
512 {
513 handle->debug_print("aox4000: response error failed.\n"); /* response error failed */
514
515 return 1; /* return error */
516 }
517 handle->mode = (uint8_t)(mode); /* save mode */
518
519 return 0; /* success return 0 */
520}
521
534{
535 uint8_t res;
536 uint16_t input_len;
537 uint16_t output_len;
538 uint8_t buf[1];
539 char input[16];
540 char output[16];
541
542 if (handle == NULL) /* check handle */
543 {
544 return 2; /* return error */
545 }
546 if (handle->inited != 1) /* check handle initialization */
547 {
548 return 3; /* return error */
549 }
550
551 memset(input, 0, sizeof(char) * 16); /* init input buffer */
552 memset(output, 0, sizeof(char) * 16); /* init output buffer */
553 input_len = (uint16_t)snprintf(input, 15, "%s\r\n", AOX4000_COMMAND_MODE); /* get input length */
554
555 output_len = 15; /* set 15 */
556 res = a_aox4000_write_read(handle, input, input_len, output, &output_len); /* write read */
557 if (res != 0) /* check result */
558 {
559 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
560
561 return 1; /* return error */
562 }
563 res = aox4000_parse_mode(handle, output, buf); /* parse the mode */
564 if (res != 0) /* check result */
565 {
566 handle->debug_print("aox4000: response error failed.\n"); /* response error failed */
567
568 return 1; /* return error */
569 }
570 if (output_len < 5) /* check length */
571 {
572 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
573
574 return 1; /* return error */
575 }
576 if ((buf[0] == 0) || (buf[0] == 1)) /* check response */
577 {
578 *mode = (aox4000_mode_t)(buf[0]); /* save mode */
579
580 return 0; /* success return 0 */
581 }
582 else
583 {
584 handle->debug_print("aox4000: response error failed.\n"); /* response error failed */
585
586 return 1; /* return error */
587 }
588}
589
604uint8_t aox4000_read(aox4000_handle_t *handle, float *oxygen_ppo2_mbar,
605 float *oxygen_density_percentage,
606 float *temperature_degree, float *pressure_mbar)
607{
608 uint8_t res;
609
610 if (handle == NULL) /* check handle */
611 {
612 return 2; /* return error */
613 }
614 if (handle->inited != 1) /* check handle initialization */
615 {
616 return 3; /* return error */
617 }
618
619 if (handle->mode == (uint8_t)(AOX4000_MODE_AUTO)) /* auto mode */
620 {
621 char output[48];
622 uint16_t output_len;
623
624 output_len = 47; /* set 47 */
625 res = a_aox4000_read(handle, output, &output_len); /* read */
626 if (res != 0) /* check result */
627 {
628 handle->debug_print("aox4000: uart read failed.\n"); /* uart read failed */
629
630 return 1; /* return error */
631 }
632 if (output_len < 5) /* check length */
633 {
634 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
635
636 return 1; /* return error */
637 }
638 res = aox4000_parse_data(handle, output,
639 oxygen_ppo2_mbar, oxygen_density_percentage,
640 temperature_degree, pressure_mbar); /* parse data */
641 if (res != 0) /* check result */
642 {
643 handle->debug_print("aox4000: response error.\n"); /* response error */
644
645 return 1; /* return error */
646 }
647
648 return 0; /* success return 0 */
649 }
650 else /* request mode */
651 {
652 char input[16];
653 char output[48];
654 uint16_t input_len;
655 uint16_t output_len;
656
657 memset(input, 0, sizeof(char) * 16); /* init input buffer */
658 memset(output, 0, sizeof(char) * 48); /* init buffer */
659 input_len = (uint16_t)snprintf(input, 15, "%s\r\n", AOX4000_COMMAND_ALL); /* get input length */
660 output_len = 47; /* set 47 */
661 res = a_aox4000_write_read(handle, input, input_len, output, &output_len); /* write read */
662 if (res != 0) /* check result */
663 {
664 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
665
666 return 1; /* return error */
667 }
668 if (output_len < 5) /* check length */
669 {
670 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
671
672 return 1; /* return error */
673 }
674 res = aox4000_parse_data(handle, output,
675 oxygen_ppo2_mbar, oxygen_density_percentage,
676 temperature_degree, pressure_mbar); /* parse data */
677 if (res != 0) /* check result */
678 {
679 handle->debug_print("aox4000: response error.\n"); /* response error */
680
681 return 1; /* return error */
682 }
683
684 return 0; /* success return 0 */
685 }
686}
687
699uint8_t aox4000_read_oxygen_ppo2(aox4000_handle_t *handle, float *mbar)
700{
701 uint8_t res;
702
703 if (handle == NULL) /* check handle */
704 {
705 return 2; /* return error */
706 }
707 if (handle->inited != 1) /* check handle initialization */
708 {
709 return 3; /* return error */
710 }
711
712 if (handle->mode == (uint8_t)(AOX4000_MODE_AUTO)) /* auto mode */
713 {
714 char output[48];
715 uint16_t output_len;
716 float oxygen_density_percentage;
717 float temperature_degree;
718 float pressure_mbar;
719
720 output_len = 47; /* set 47 */
721 res = a_aox4000_read(handle, output, &output_len); /* read */
722 if (res != 0) /* check result */
723 {
724 handle->debug_print("aox4000: uart read failed.\n"); /* uart read failed */
725
726 return 1; /* return error */
727 }
728 if (output_len < 5) /* check length */
729 {
730 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
731
732 return 1; /* return error */
733 }
734 res = aox4000_parse_data(handle, output,
735 mbar, &oxygen_density_percentage,
736 &temperature_degree, &pressure_mbar); /* parse data */
737 if (res != 0) /* check result */
738 {
739 handle->debug_print("aox4000: response error.\n"); /* response error */
740
741 return 1; /* return error */
742 }
743
744 return 0; /* success return 0 */
745 }
746 else /* request mode */
747 {
748 char input[16];
749 char output[48];
750 uint16_t input_len;
751 uint16_t output_len;
752
753 memset(input, 0, sizeof(char) * 16); /* init input buffer */
754 memset(output, 0, sizeof(char) * 48); /* init buffer */
755 input_len = (uint16_t)snprintf(input, 15, "%s\r\n",
756 AOX4000_COMMAND_OXYGEN_PPO2); /* get input length */
757 output_len = 47; /* set 47 */
758 res = a_aox4000_write_read(handle, input, input_len,
759 output, &output_len); /* write read */
760 if (res != 0) /* check result */
761 {
762 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
763
764 return 1; /* return error */
765 }
766 if (output_len < 5) /* check length */
767 {
768 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
769
770 return 1; /* return error */
771 }
772 res = aox4000_parse_number(handle, AOX4000_COMMAND_OXYGEN_PPO2,
773 output, mbar); /* parse data */
774 if (res != 0) /* check result */
775 {
776 handle->debug_print("aox4000: response error.\n"); /* response error */
777
778 return 1; /* return error */
779 }
780
781 return 0; /* success return 0 */
782 }
783}
784
796uint8_t aox4000_read_oxygen_density(aox4000_handle_t *handle, float *percentage)
797{
798 uint8_t res;
799
800 if (handle == NULL) /* check handle */
801 {
802 return 2; /* return error */
803 }
804 if (handle->inited != 1) /* check handle initialization */
805 {
806 return 3; /* return error */
807 }
808
809 if (handle->mode == (uint8_t)(AOX4000_MODE_AUTO)) /* auto mode */
810 {
811 char output[48];
812 uint16_t output_len;
813 float oxygen_ppo2_mbar;
814 float temperature_degree;
815 float pressure_mbar;
816
817 output_len = 47; /* set 47 */
818 res = a_aox4000_read(handle, output, &output_len); /* read */
819 if (res != 0) /* check result */
820 {
821 handle->debug_print("aox4000: uart read failed.\n"); /* uart read failed */
822
823 return 1; /* return error */
824 }
825 if (output_len < 5) /* check length */
826 {
827 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
828
829 return 1; /* return error */
830 }
831 res = aox4000_parse_data(handle, output,
832 &oxygen_ppo2_mbar, percentage,
833 &temperature_degree, &pressure_mbar); /* parse data */
834 if (res != 0) /* check result */
835 {
836 handle->debug_print("aox4000: response error.\n"); /* response error */
837
838 return 1; /* return error */
839 }
840
841 return 0; /* success return 0 */
842 }
843 else /* request mode */
844 {
845 char input[16];
846 char output[48];
847 uint16_t input_len;
848 uint16_t output_len;
849
850 memset(input, 0, sizeof(char) * 16); /* init input buffer */
851 memset(output, 0, sizeof(char) * 48); /* init buffer */
852 input_len = (uint16_t)snprintf(input, 15, "%s\r\n",
853 AOX4000_COMMAND_OXYGEN_DENSITY); /* get input length */
854 output_len = 47; /* set 47 */
855 res = a_aox4000_write_read(handle, input, input_len,
856 output, &output_len); /* write read */
857 if (res != 0) /* check result */
858 {
859 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
860
861 return 1; /* return error */
862 }
863 if (output_len < 5) /* check length */
864 {
865 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
866
867 return 1; /* return error */
868 }
869 res = aox4000_parse_number(handle, AOX4000_COMMAND_OXYGEN_DENSITY,
870 output, percentage); /* parse data */
871 if (res != 0) /* check result */
872 {
873 handle->debug_print("aox4000: response error.\n"); /* response error */
874
875 return 1; /* return error */
876 }
877
878 return 0; /* success return 0 */
879 }
880}
881
893uint8_t aox4000_read_pressure(aox4000_handle_t *handle, float *mbar)
894{
895 uint8_t res;
896
897 if (handle == NULL) /* check handle */
898 {
899 return 2; /* return error */
900 }
901 if (handle->inited != 1) /* check handle initialization */
902 {
903 return 3; /* return error */
904 }
905
906 if (handle->mode == (uint8_t)(AOX4000_MODE_AUTO)) /* auto mode */
907 {
908 char output[48];
909 uint16_t output_len;
910 float oxygen_ppo2_mbar;
911 float oxygen_density_percentage;
912 float temperature_degree;
913
914 output_len = 47; /* set 47 */
915 res = a_aox4000_read(handle, output, &output_len); /* read */
916 if (res != 0) /* check result */
917 {
918 handle->debug_print("aox4000: uart read failed.\n"); /* uart read failed */
919
920 return 1; /* return error */
921 }
922 if (output_len < 5) /* check length */
923 {
924 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
925
926 return 1; /* return error */
927 }
928 res = aox4000_parse_data(handle, output,
929 &oxygen_ppo2_mbar, &oxygen_density_percentage,
930 &temperature_degree, mbar); /* parse data */
931 if (res != 0) /* check result */
932 {
933 handle->debug_print("aox4000: response error.\n"); /* response error */
934
935 return 1; /* return error */
936 }
937
938 return 0; /* success return 0 */
939 }
940 else /* request mode */
941 {
942 char input[16];
943 char output[48];
944 uint16_t input_len;
945 uint16_t output_len;
946
947 memset(input, 0, sizeof(char) * 16); /* init input buffer */
948 memset(output, 0, sizeof(char) * 48); /* init buffer */
949 input_len = (uint16_t)snprintf(input, 15, "%s\r\n",
950 AOX4000_COMMAND_PRESSURE); /* get input length */
951 output_len = 47; /* set 47 */
952 res = a_aox4000_write_read(handle, input, input_len,
953 output, &output_len); /* write read */
954 if (res != 0) /* check result */
955 {
956 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
957
958 return 1; /* return error */
959 }
960 if (output_len < 5) /* check length */
961 {
962 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
963
964 return 1; /* return error */
965 }
966 res = aox4000_parse_number(handle, AOX4000_COMMAND_PRESSURE, output, mbar); /* parse data */
967 if (res != 0) /* check result */
968 {
969 handle->debug_print("aox4000: response error.\n"); /* response error */
970
971 return 1; /* return error */
972 }
973
974 return 0; /* success return 0 */
975 }
976}
977
989uint8_t aox4000_read_temperature(aox4000_handle_t *handle, float *degree)
990{
991 uint8_t res;
992
993 if (handle == NULL) /* check handle */
994 {
995 return 2; /* return error */
996 }
997 if (handle->inited != 1) /* check handle initialization */
998 {
999 return 3; /* return error */
1000 }
1001
1002 if (handle->mode == (uint8_t)(AOX4000_MODE_AUTO)) /* auto mode */
1003 {
1004 char output[48];
1005 uint16_t output_len;
1006 float oxygen_ppo2_mbar;
1007 float oxygen_density_percentage;
1008 float pressure_mbar;
1009
1010 output_len = 47; /* set 47 */
1011 res = a_aox4000_read(handle, output, &output_len); /* read */
1012 if (res != 0) /* check result */
1013 {
1014 handle->debug_print("aox4000: uart read failed.\n"); /* uart read failed */
1015
1016 return 1; /* return error */
1017 }
1018 if (output_len < 5) /* check length */
1019 {
1020 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
1021
1022 return 1; /* return error */
1023 }
1024 res = aox4000_parse_data(handle, output,
1025 &oxygen_ppo2_mbar, &oxygen_density_percentage,
1026 degree, &pressure_mbar); /* parse data */
1027 if (res != 0) /* check result */
1028 {
1029 handle->debug_print("aox4000: response error.\n"); /* response error */
1030
1031 return 1; /* return error */
1032 }
1033
1034 return 0; /* success return 0 */
1035 }
1036 else /* request mode */
1037 {
1038 char input[16];
1039 char output[48];
1040 uint16_t input_len;
1041 uint16_t output_len;
1042
1043 memset(input, 0, sizeof(char) * 16); /* init input buffer */
1044 memset(output, 0, sizeof(char) * 48); /* init buffer */
1045 input_len = (uint16_t)snprintf(input, 15, "%s\r\n",
1046 AOX4000_COMMAND_TEMPERATURE); /* get input length */
1047 output_len = 47; /* set 47 */
1048 res = a_aox4000_write_read(handle, input, input_len,
1049 output, &output_len); /* write read */
1050 if (res != 0) /* check result */
1051 {
1052 handle->debug_print("aox4000: uart write read failed.\n"); /* uart write read failed */
1053
1054 return 1; /* return error */
1055 }
1056 if (output_len < 5) /* check length */
1057 {
1058 handle->debug_print("aox4000: response error failed..\n"); /* response error failed. */
1059
1060 return 1; /* return error */
1061 }
1062 res = aox4000_parse_number(handle, AOX4000_COMMAND_TEMPERATURE,
1063 output, degree); /* parse data */
1064 if (res != 0) /* check result */
1065 {
1066 handle->debug_print("aox4000: response error.\n"); /* response error */
1067
1068 return 1; /* return error */
1069 }
1070
1071 return 0; /* success return 0 */
1072 }
1073}
1074
1087uint8_t aox4000_command_read(aox4000_handle_t *handle, char *buf, uint16_t len)
1088{
1089 uint16_t l;
1090
1091 if (handle == NULL) /* check handle */
1092 {
1093 return 2; /* return error */
1094 }
1095 if (handle->inited != 1) /* check handle initialization */
1096 {
1097 return 3; /* return error */
1098 }
1099
1100 l = handle->uart_read((uint8_t *)buf, len); /* uart read */
1101 if (l != len) /* check result */
1102 {
1103 handle->debug_print("aox4000: uart read failed.\n"); /* uart read failed */
1104
1105 return 1; /* return error */
1106 }
1107
1108 return 0; /* success return 0 */
1109}
1110
1123uint8_t aox4000_command_write(aox4000_handle_t *handle, char *buf, uint16_t len)
1124{
1125 uint8_t res;
1126
1127 if (handle == NULL) /* check handle */
1128 {
1129 return 2; /* return error */
1130 }
1131 if (handle->inited != 1) /* check handle initialization */
1132 {
1133 return 3; /* return error */
1134 }
1135
1136 res = handle->uart_flush(); /* uart flush */
1137 if (res != 0) /* check result */
1138 {
1139 handle->debug_print("aox4000: uart flush failed.\n"); /* uart flush failed */
1140
1141 return 1; /* return error */
1142 }
1143 res = handle->uart_write((uint8_t *)buf, len);
1144 if (res != 0) /* check result */
1145 {
1146 handle->debug_print("aox4000: uart write failed.\n"); /* uart write failed */
1147
1148 return 1; /* return error */
1149 }
1150
1151 return 0; /* success return 0 */
1152}
1153
1163{
1164 if (info == NULL) /* check handle */
1165 {
1166 return 2; /* return error */
1167 }
1168
1169 memset(info, 0, sizeof(aox4000_info_t)); /* initialize aox4000 info structure */
1170 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1171 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1172 strncpy(info->interface, "UART", 8); /* copy interface name */
1173 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1174 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1175 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1176 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1177 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1178 info->driver_version = DRIVER_VERSION; /* set driver version */
1179
1180 return 0; /* success return 0 */
1181}
#define AOX4000_COMMAND_MODE
chip command definition
#define AOX4000_COMMAND_TEMPERATURE
#define AOX4000_COMMAND_ALL
#define MAX_CURRENT
#define AOX4000_COMMAND_PRESSURE
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define AOX4000_COMMAND_OXYGEN_DENSITY
#define AOX4000_COMMAND_OXYGEN_PPO2
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define AOX4000_COMMAND_ERROR
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver aox4000 header file
uint8_t aox4000_read(aox4000_handle_t *handle, float *oxygen_ppo2_mbar, float *oxygen_density_percentage, float *temperature_degree, float *pressure_mbar)
read data
uint8_t aox4000_read_oxygen_density(aox4000_handle_t *handle, float *percentage)
read oxygen density
uint8_t aox4000_info(aox4000_info_t *info)
get chip's information
struct aox4000_handle_s aox4000_handle_t
aox4000 handle structure definition
aox4000_status_t
aox4000 status enumeration definition
uint8_t aox4000_read_temperature(aox4000_handle_t *handle, float *degree)
read temperature
uint8_t aox4000_get_last_error(aox4000_handle_t *handle, aox4000_status_t *status)
get last error
#define AOX4000_UART_DELAY_MS
aox4000 uart delay definition
#define AOX4000_UART_POLL_DELAY_MS
aox4000 uart poll delay definition
uint8_t aox4000_deinit(aox4000_handle_t *handle)
close the chip
aox4000_mode_t
aox4000 mode enumeration definition
uint8_t aox4000_init(aox4000_handle_t *handle)
initialize the chip
struct aox4000_info_s aox4000_info_t
aox4000 information structure definition
uint8_t aox4000_read_oxygen_ppo2(aox4000_handle_t *handle, float *mbar)
read oxygen ppo2
uint8_t aox4000_read_pressure(aox4000_handle_t *handle, float *mbar)
read pressure
uint8_t aox4000_get_mode(aox4000_handle_t *handle, aox4000_mode_t *mode)
get mode
uint8_t aox4000_set_mode(aox4000_handle_t *handle, aox4000_mode_t mode)
set mode
@ AOX4000_STATUS_UNKNOWN
@ AOX4000_STATUS_OK
@ AOX4000_MODE_AUTO
@ AOX4000_MODE_REQUEST
uint8_t aox4000_command_read(aox4000_handle_t *handle, char *buf, uint16_t len)
command read
uint8_t aox4000_command_write(aox4000_handle_t *handle, char *buf, uint16_t len)
command write
uint8_t(* uart_flush)(void)
uint8_t(* uart_write)(uint8_t *buf, uint16_t len)
void(* delay_ms)(uint32_t ms)
uint8_t(* uart_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint16_t(* uart_read)(uint8_t *buf, uint16_t len)
uint8_t(* uart_init)(void)
uint32_t driver_version
char manufacturer_name[32]