Meteo Station

Dependencies:   ds3231 mbed-rtos mbed

Fork of meteo_station by Valentín Salinas

Committer:
ValenSalLop
Date:
Wed Apr 26 16:03:56 2017 +0000
Revision:
0:de99871168ac
Child:
1:678088dfd174
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ValenSalLop 0:de99871168ac 1 #include "mbed.h"
ValenSalLop 0:de99871168ac 2 #include "rtos.h"
ValenSalLop 0:de99871168ac 3 #include "ds3231.h"
ValenSalLop 0:de99871168ac 4
ValenSalLop 0:de99871168ac 5 #define ESC 0x1B
ValenSalLop 0:de99871168ac 6
ValenSalLop 0:de99871168ac 7 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
ValenSalLop 0:de99871168ac 8 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
ValenSalLop 0:de99871168ac 9
ValenSalLop 0:de99871168ac 10 int main(void)
ValenSalLop 0:de99871168ac 11 {
ValenSalLop 0:de99871168ac 12 //rtc object
ValenSalLop 0:de99871168ac 13 Ds3231 rtc(D14, D15);
ValenSalLop 0:de99871168ac 14
ValenSalLop 0:de99871168ac 15 time_t epoch_time;
ValenSalLop 0:de99871168ac 16
ValenSalLop 0:de99871168ac 17 //DS3231 rtc variables
ValenSalLop 0:de99871168ac 18
ValenSalLop 0:de99871168ac 19 //default, use bit masks in ds3231.h for desired operation
ValenSalLop 0:de99871168ac 20 ds3231_cntl_stat_t rtc_control_status = {0,0};
ValenSalLop 0:de99871168ac 21 ds3231_time_t rtc_time;
ValenSalLop 0:de99871168ac 22 ds3231_calendar_t rtc_calendar;
ValenSalLop 0:de99871168ac 23
ValenSalLop 0:de99871168ac 24 rtc.set_cntl_stat_reg(rtc_control_status);
ValenSalLop 0:de99871168ac 25
ValenSalLop 0:de99871168ac 26 //get day from user
ValenSalLop 0:de99871168ac 27 get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
ValenSalLop 0:de99871168ac 28 7, &rtc_calendar.day);
ValenSalLop 0:de99871168ac 29
ValenSalLop 0:de99871168ac 30 //get day of month from user
ValenSalLop 0:de99871168ac 31 get_user_input("\nPlease enter day of month (1-31): ", 1, 31,
ValenSalLop 0:de99871168ac 32 &rtc_calendar.date);
ValenSalLop 0:de99871168ac 33
ValenSalLop 0:de99871168ac 34 //get month from user
ValenSalLop 0:de99871168ac 35 get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1,
ValenSalLop 0:de99871168ac 36 12, &rtc_calendar.month);
ValenSalLop 0:de99871168ac 37
ValenSalLop 0:de99871168ac 38 //get year from user
ValenSalLop 0:de99871168ac 39 get_user_input("\nPlease enter the year (0-99): ",0, 99,
ValenSalLop 0:de99871168ac 40 &rtc_calendar.year);
ValenSalLop 0:de99871168ac 41
ValenSalLop 0:de99871168ac 42 //Get time mode
ValenSalLop 0:de99871168ac 43 get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1,
ValenSalLop 0:de99871168ac 44 &rtc_time.mode);
ValenSalLop 0:de99871168ac 45
ValenSalLop 0:de99871168ac 46 if(rtc_time.mode)
ValenSalLop 0:de99871168ac 47 {
ValenSalLop 0:de99871168ac 48 //Get AM/PM status
ValenSalLop 0:de99871168ac 49 get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1,
ValenSalLop 0:de99871168ac 50 &rtc_time.am_pm);
ValenSalLop 0:de99871168ac 51 //Get hour from user
ValenSalLop 0:de99871168ac 52 get_user_input("\nPlease enter the hour (1-12): ", 1, 12,
ValenSalLop 0:de99871168ac 53 &rtc_time.hours);
ValenSalLop 0:de99871168ac 54 }
ValenSalLop 0:de99871168ac 55 else
ValenSalLop 0:de99871168ac 56 {
ValenSalLop 0:de99871168ac 57 //Get hour from user
ValenSalLop 0:de99871168ac 58 get_user_input("\nPlease enter the hour (0-23): ", 0, 23,
ValenSalLop 0:de99871168ac 59 &rtc_time.hours);
ValenSalLop 0:de99871168ac 60 }
ValenSalLop 0:de99871168ac 61
ValenSalLop 0:de99871168ac 62 //Get minutes from user
ValenSalLop 0:de99871168ac 63 get_user_input("\nPlease enter the minute (0-59): ", 0, 59,
ValenSalLop 0:de99871168ac 64 &rtc_time.minutes);
ValenSalLop 0:de99871168ac 65
ValenSalLop 0:de99871168ac 66
ValenSalLop 0:de99871168ac 67 //Get seconds from user
ValenSalLop 0:de99871168ac 68 get_user_input("\nPlease enter the second (0-59): ", 0, 59,
ValenSalLop 0:de99871168ac 69 &rtc_time.seconds);
ValenSalLop 0:de99871168ac 70
ValenSalLop 0:de99871168ac 71
ValenSalLop 0:de99871168ac 72
ValenSalLop 0:de99871168ac 73 //Set the time, uses inverted logic for return value
ValenSalLop 0:de99871168ac 74 if(rtc.set_time(rtc_time))
ValenSalLop 0:de99871168ac 75 {
ValenSalLop 0:de99871168ac 76 printf("\nrtc.set_time failed!!\n");
ValenSalLop 0:de99871168ac 77 exit(0);
ValenSalLop 0:de99871168ac 78 }
ValenSalLop 0:de99871168ac 79
ValenSalLop 0:de99871168ac 80 //Set the calendar, uses inverted logic for return value
ValenSalLop 0:de99871168ac 81 if(rtc.set_calendar(rtc_calendar))
ValenSalLop 0:de99871168ac 82 {
ValenSalLop 0:de99871168ac 83 printf("\nrtc.set_calendar failed!!\n");
ValenSalLop 0:de99871168ac 84 exit(0);
ValenSalLop 0:de99871168ac 85 }
ValenSalLop 0:de99871168ac 86
ValenSalLop 0:de99871168ac 87 char buffer[32];
ValenSalLop 0:de99871168ac 88
ValenSalLop 0:de99871168ac 89 for(;;)
ValenSalLop 0:de99871168ac 90 {
ValenSalLop 0:de99871168ac 91 printf("%c[2J", ESC); //clear screen
ValenSalLop 0:de99871168ac 92 printf("%c[H", ESC); //move cursor to Home
ValenSalLop 0:de99871168ac 93
ValenSalLop 0:de99871168ac 94 //new epoch time fx
ValenSalLop 0:de99871168ac 95 epoch_time = rtc.get_epoch();
ValenSalLop 0:de99871168ac 96
ValenSalLop 0:de99871168ac 97 printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
ValenSalLop 0:de99871168ac 98
ValenSalLop 0:de99871168ac 99 printf("\nTime as a basic string = %s", ctime(&epoch_time));
ValenSalLop 0:de99871168ac 100
ValenSalLop 0:de99871168ac 101 strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
ValenSalLop 0:de99871168ac 102 printf("\nTime as a custom formatted string = %s", buffer);
ValenSalLop 0:de99871168ac 103
ValenSalLop 0:de99871168ac 104 wait(1.0);
ValenSalLop 0:de99871168ac 105 }//loop
ValenSalLop 0:de99871168ac 106 }
ValenSalLop 0:de99871168ac 107
ValenSalLop 0:de99871168ac 108
ValenSalLop 0:de99871168ac 109 /**********************************************************************
ValenSalLop 0:de99871168ac 110 * Function: get_user_input()
ValenSalLop 0:de99871168ac 111 * Parameters: message - user prompt
ValenSalLop 0:de99871168ac 112 * min - minimum value of input
ValenSalLop 0:de99871168ac 113 * max - maximum value of input
ValenSalLop 0:de99871168ac 114 * member - pointer to struct member
ValenSalLop 0:de99871168ac 115 * Returns: none
ValenSalLop 0:de99871168ac 116 *
ValenSalLop 0:de99871168ac 117 * Description: get time/date input from user
ValenSalLop 0:de99871168ac 118 *
ValenSalLop 0:de99871168ac 119 **********************************************************************/
ValenSalLop 0:de99871168ac 120 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
ValenSalLop 0:de99871168ac 121 {
ValenSalLop 0:de99871168ac 122 uint32_t temp;
ValenSalLop 0:de99871168ac 123
ValenSalLop 0:de99871168ac 124 do
ValenSalLop 0:de99871168ac 125 {
ValenSalLop 0:de99871168ac 126 printf("\n%s", message);
ValenSalLop 0:de99871168ac 127
ValenSalLop 0:de99871168ac 128 //for some reason mbed doesn't like a pointer to a member in scanf
ValenSalLop 0:de99871168ac 129 //term.scanf("%d", member); works with gcc on RPi
ValenSalLop 0:de99871168ac 130 scanf("%d", &temp);
ValenSalLop 0:de99871168ac 131
ValenSalLop 0:de99871168ac 132 *member = temp;
ValenSalLop 0:de99871168ac 133
ValenSalLop 0:de99871168ac 134 if((*(member)< min) || (*(member) > max))
ValenSalLop 0:de99871168ac 135 {
ValenSalLop 0:de99871168ac 136 printf("\nERROR-RTI");
ValenSalLop 0:de99871168ac 137 }
ValenSalLop 0:de99871168ac 138 }
ValenSalLop 0:de99871168ac 139 while((*(member) < min) || (*(member) > max));
ValenSalLop 0:de99871168ac 140 }
ValenSalLop 0:de99871168ac 141
ValenSalLop 0:de99871168ac 142
ValenSalLop 0:de99871168ac 143 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
ValenSalLop 0:de99871168ac 144 {
ValenSalLop 0:de99871168ac 145 uint32_t temp;
ValenSalLop 0:de99871168ac 146
ValenSalLop 0:de99871168ac 147 do
ValenSalLop 0:de99871168ac 148 {
ValenSalLop 0:de99871168ac 149 printf("\n%s", message);
ValenSalLop 0:de99871168ac 150
ValenSalLop 0:de99871168ac 151 //for some reason mbed doesn't like a pointer to a member in scanf
ValenSalLop 0:de99871168ac 152 //term.scanf("%d", member); works with gcc on RPi
ValenSalLop 0:de99871168ac 153 scanf("%d", &temp);
ValenSalLop 0:de99871168ac 154
ValenSalLop 0:de99871168ac 155 *member = temp;
ValenSalLop 0:de99871168ac 156
ValenSalLop 0:de99871168ac 157 if((*(member)< min) || (*(member) > max))
ValenSalLop 0:de99871168ac 158 {
ValenSalLop 0:de99871168ac 159 printf("\nERROR-RTI");
ValenSalLop 0:de99871168ac 160 }
ValenSalLop 0:de99871168ac 161 }
ValenSalLop 0:de99871168ac 162 while((*(member) < min) || (*(member) > max));
ValenSalLop 0:de99871168ac 163 }
ValenSalLop 0:de99871168ac 164
ValenSalLop 0:de99871168ac 165