School project.
Dependencies: Timezone NTPClient BSP_DISCO_F746NG Grove_temperature
sc_functions.h@10:137cf2c92871, 2021-01-15 (annotated)
- Committer:
- tuxx0046
- Date:
- Fri Jan 15 09:27:26 2021 +0000
- Revision:
- 10:137cf2c92871
- Child:
- 13:41debc0b9063
Add serial communication service functions.; Major changes to comments.; Minor fixes to code to clear buffer in console when typing etc.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tuxx0046 | 10:137cf2c92871 | 1 | /** |
tuxx0046 | 10:137cf2c92871 | 2 | @file sc_functions.h |
tuxx0046 | 10:137cf2c92871 | 3 | @author Tu Tri Huynh |
tuxx0046 | 10:137cf2c92871 | 4 | @date January 15, 2021 |
tuxx0046 | 10:137cf2c92871 | 5 | @brief Functions used for Serial Communication during service. |
tuxx0046 | 10:137cf2c92871 | 6 | */ |
tuxx0046 | 10:137cf2c92871 | 7 | |
tuxx0046 | 10:137cf2c92871 | 8 | /** |
tuxx0046 | 10:137cf2c92871 | 9 | This function will prompt the user for which sensor data to fetch and print to console. |
tuxx0046 | 10:137cf2c92871 | 10 | @date 1/15/2021 |
tuxx0046 | 10:137cf2c92871 | 11 | */ |
tuxx0046 | 10:137cf2c92871 | 12 | void sc_read_input() |
tuxx0046 | 10:137cf2c92871 | 13 | { |
tuxx0046 | 10:137cf2c92871 | 14 | char choice; |
tuxx0046 | 10:137cf2c92871 | 15 | printf("Your input: "); |
tuxx0046 | 10:137cf2c92871 | 16 | scanf("%1s", &choice); |
tuxx0046 | 10:137cf2c92871 | 17 | if (choice == '1') |
tuxx0046 | 10:137cf2c92871 | 18 | { |
tuxx0046 | 10:137cf2c92871 | 19 | printf("Current light intensity: %1.2f\n", light_sensor.read()); |
tuxx0046 | 10:137cf2c92871 | 20 | } |
tuxx0046 | 10:137cf2c92871 | 21 | else if (choice == '2') |
tuxx0046 | 10:137cf2c92871 | 22 | { |
tuxx0046 | 10:137cf2c92871 | 23 | printf("Current rotary position: %1.2f\n", rotary.read()); |
tuxx0046 | 10:137cf2c92871 | 24 | } |
tuxx0046 | 10:137cf2c92871 | 25 | else |
tuxx0046 | 10:137cf2c92871 | 26 | { |
tuxx0046 | 10:137cf2c92871 | 27 | printf("Invalid choice.\n"); |
tuxx0046 | 10:137cf2c92871 | 28 | } |
tuxx0046 | 10:137cf2c92871 | 29 | /// This will empty the buffer and will avoid already inputted data to be used in scanf etc. |
tuxx0046 | 10:137cf2c92871 | 30 | int c; |
tuxx0046 | 10:137cf2c92871 | 31 | do |
tuxx0046 | 10:137cf2c92871 | 32 | { |
tuxx0046 | 10:137cf2c92871 | 33 | c = getchar(); |
tuxx0046 | 10:137cf2c92871 | 34 | } while (c != EOF && c != '\n'); |
tuxx0046 | 10:137cf2c92871 | 35 | } |
tuxx0046 | 10:137cf2c92871 | 36 | |
tuxx0046 | 10:137cf2c92871 | 37 | /** |
tuxx0046 | 10:137cf2c92871 | 38 | This function will show the menu for serial communication. |
tuxx0046 | 10:137cf2c92871 | 39 | @date 1/15/2021 |
tuxx0046 | 10:137cf2c92871 | 40 | */ |
tuxx0046 | 10:137cf2c92871 | 41 | void sc_show_menu() |
tuxx0046 | 10:137cf2c92871 | 42 | { |
tuxx0046 | 10:137cf2c92871 | 43 | printf("*********************************************************************\n"); |
tuxx0046 | 10:137cf2c92871 | 44 | printf("To get data from the sensors, please enter a number and press return.\n"); |
tuxx0046 | 10:137cf2c92871 | 45 | printf("Input 1 to get current light intensity.\n"); |
tuxx0046 | 10:137cf2c92871 | 46 | printf("Input 2 to get the current position of rotary control button.\n"); |
tuxx0046 | 10:137cf2c92871 | 47 | sc_read_input(); |
tuxx0046 | 10:137cf2c92871 | 48 | } |
tuxx0046 | 10:137cf2c92871 | 49 | |
tuxx0046 | 10:137cf2c92871 | 50 | |
tuxx0046 | 10:137cf2c92871 | 51 | /** |
tuxx0046 | 10:137cf2c92871 | 52 | This function will run the serial communication service. |
tuxx0046 | 10:137cf2c92871 | 53 | @date 1/15/2021 |
tuxx0046 | 10:137cf2c92871 | 54 | */ |
tuxx0046 | 10:137cf2c92871 | 55 | void sc_run_service() |
tuxx0046 | 10:137cf2c92871 | 56 | { |
tuxx0046 | 10:137cf2c92871 | 57 | while(1) |
tuxx0046 | 10:137cf2c92871 | 58 | { |
tuxx0046 | 10:137cf2c92871 | 59 | sc_show_menu(); |
tuxx0046 | 10:137cf2c92871 | 60 | } |
tuxx0046 | 10:137cf2c92871 | 61 | } |