void Get_AlarmTimeString(char *TimeString) { char timeBuffer[9]; volatile uint32 scroll, sec, min, hour; sec = 0u; min = 0u; hour = 0u; if(AlarmTime.status == 1) { RTC_GetAlarmDateAndTime(&AlarmTime); TimeStruct.Time = RTC_GetTime(); min = RTC_GetMinutes(AlarmTime.time); hour = RTC_GetHours(AlarmTime.time); if(RTC_GetSecond(AlarmTime.time) < RTC_GetSecond(TimeStruct.Time)) { if(min) { min--; sec = 59 - (RTC_GetSecond(TimeStruct.Time) - RTC_GetSecond(AlarmTime.time)); } else { hour--; min = 59; sec = 59 - (RTC_GetSecond(TimeStruct.Time) - RTC_GetSecond(AlarmTime.time)); } } else { sec = (RTC_GetSecond(AlarmTime.time) - RTC_GetSecond(TimeStruct.Time)); } if(min < RTC_GetMinutes(TimeStruct.Time)) { hour--; min = 59 - (RTC_GetMinutes(TimeStruct.Time) - min); } else { min = min - RTC_GetMinutes(TimeStruct.Time); } hour = hour - RTC_GetHours(TimeStruct.Time); } sprintf(timeBuffer, "%02lu:%02lu:%02lu", hour, min, sec); for(scroll = 0; scroll < 9; scroll++) { TimeString[scroll] = timeBuffer[scroll]; } }
/*! ******************************************************************************* * \brief Print debug line * * \note ******************************************************************************/ void COM_print_debug(uint8_t type) { bool sync = (type==2); if (!sync) { wireless_async = true; wireless_putchar('D'); } wireless_putchar(RTC_GetMinute() | (CTL_test_auto()?0x40:0) | ((CTL_mode_auto)?0x80:0)); wireless_putchar(RTC_GetSecond() | ((mode_window())?0x40:0) | ((menu_locked)?0x80:0)); wireless_putchar(CTL_error); wireless_putchar(temp_average >> 8); // current temp wireless_putchar(temp_average & 0xff); wireless_putchar(bat_average >> 8); // current temp wireless_putchar(bat_average & 0xff); wireless_putchar(CTL_temp_wanted); // wanted temp wireless_putchar(valve_wanted); // valve pos wireless_async = false; rfm_start_tx(); }
/******************************************************************************* * Function Name: WriteSkierResult ******************************************************************************** * * Summary: * Start log system(mount fat) * * Parametrs: * *data - pointer from result skiers * Return: * resultF - result mount FAT system (from enum FRESULT) * *******************************************************************************/ uint32_t WriteSkierResult(skierDB_El *data) { uint8 resultF; uint count; uint32_t nameYear; uint32_t nameMonth; uint32_t nameDay; uint32_t date; /*get date from RTC*/ date = RTC_GetDate(); nameYear = RTC_GetYear(date)-2000; nameMonth = RTC_GetMonth(date); nameDay = RTC_GetDay(date); /*Construct name of file in format dd_mm_yyyy*/ sprintf(nameFile,"%02d_%02d_%d.txt",nameDay,nameMonth,nameYear); createFlag = 0; resultF = f_open(&fileO, nameFile, FA_WRITE | FA_OPEN_APPEND, &createFlag); if(resultF == RES_OK) { /*Construct save data*/ char start[LEN_DATA]; char finish[LEN_DATA]; char result[LEN_DATA]; char writeData[LEN_DATA_ALL]; RTC_DATE_TIME time; /*read unix time*/ RTC_UnixToDateTime(&time, data->unixStartSkier, RTC_24_HOURS_FORMAT); sprintf(start, "\t\t%02lu:%02lu:%02lu:%03u",RTC_GetHours(time.time),RTC_GetMinutes(time.time),RTC_GetSecond(time.time),data->millsStartSkier); RTC_UnixToDateTime(&time, data->unixFinishSkier, RTC_24_HOURS_FORMAT); sprintf(finish, "\t\t%02lu:%02lu:%02lu:%03u",RTC_GetHours(time.time),RTC_GetMinutes(time.time),RTC_GetSecond(time.time),data->millsFinishSkier); sprintf(result, "\t\t%02lu:%03u",(uint32)data->secondsWay,data->millsWay); sprintf(writeData,"\n\r%d%s%s%s\n\r",position, start, finish, result); if((position == 1) || (createFlag == 1)) { /*write new "cap"*/ uint32_t tmpTime ; tmpTime = RTC_GetTime(); f_printf(&fileO,"\r\nSystem started %02d:%02d:%02d\n\r", RTC_GetHours(tmpTime),RTC_GetMinutes(tmpTime),RTC_GetSecond(tmpTime)); f_printf(&fileO,"--------------------------------------------------\n\r"); f_printf(&fileO,"NUM\t\tSTART\t\t\tFINISH\t\t\tRESULT\n\r"); f_printf(&fileO,"--------------------------------------------------\n\r"); } /*write data*/ resultF = f_write(&fileO, writeData, strlen(writeData),&count); resultF = f_close(&fileO); position++; } return resultF; }
UINT32 RTC_GetSecondsSinceEpoch() { UINT32 nowDays = RTC_GetTotalDays((RTC_GetCentury() * 100) + RTC_GetYear(), RTC_GetMonth(), RTC_GetDayOfTheMonth()); UINT32 epochDays = RTC_GetTotalDays(1970, 1, 1); return ((nowDays - epochDays) * 24 * 60 * 60) + (RTC_GetHour() * 60 * 60) + (RTC_GetMinute() * 60) + RTC_GetSecond(); }
/******************************************************************************* * Function name: DisplayPrintfRealTime ******************************************************************************** * * Summary: * print real time from position row = 1, column = 0 * *******************************************************************************/ void DisplayRealTime(void) { uint32_t time; time = RTC_GetTime(); sprintf(buff, "%02lu:%02lu:%02lu ", RTC_GetHours(time),RTC_GetMinutes(time), RTC_GetSecond(time)); LCD_Position(1,0); LCD_PrintString(buff); }
/*! ******************************************************************************* * main program ******************************************************************************/ int main(void) { bool last_state_mnt; //!< motor mounted bool state_mnt; //!< motor mounted bool err; //!< error bool ref_pos_changed; //!< ref Position changed uint8_t last_statekey; //!< state of keys on last loop uint8_t last_second; //!< RTC-second of last main cycle uint8_t ref_position; //!< desired position in percent motor_speed_t speed; //!< motor speed (fast or quiet) uint8_t display_mode; //!< desired display output uint16_t value16; //!< 16 Bit value int16_t value16s; //!< signed 16 Bit value uint8_t value8; //!< 8 Bit value //! initalization init(); //! load/set default values load_defauls(); //! Enable interrupts sei(); //! show POST Screen LCD_AllSegments(LCD_MODE_ON); // all segments on delay(1000); LCD_AllSegments(LCD_MODE_OFF); LCD_PrintDec(REVHIGH, 1, LCD_MODE_ON); // print version LCD_PrintDec(REVLOW, 0, LCD_MODE_ON); LCD_Update(); delay(1000); LCD_AllSegments(LCD_MODE_OFF); // all off //! \todo Send Wakeup MSG state_mnt=false; ref_pos_changed=true; last_second=99; speed=full; last_statekey = 0; last_state_mnt = false; m_key_action = true; ref_position = 10; display_mode=3; ISR(PCINT1_vect); // get keystate // We should do the following once here to have valid data from the start ADC_Measure_Ub(); ADC_Measure_Temp(); /*! **************************************************************************** * main loop * * 1) process keypresses * - m_state_keys and m_wheel are set from IRQ, only process them * - you can set m_wheel to a new value * - controll content of LCD * 2) \todo calc new valveposition if new temp available * - temp is measured by IRQ * 3) calibrate motor * - start calibration is valve mounted changed to on * (during calibration the main loop stops at least for 10 seconds) * - reset calibration is valve mounted changed to off * 4) start motor if * - actual valveposition != desired valveposition && motor is off * 5) if motor is on call MOTOR_CheckBlocked at least once a second * - that switches motor of if it is blocked * 6) store keystate at end of loop before going to sleep * 7) Check for serial command to process * 8) \todo goto sleep if * - motor is of * - no key is pressed (AUTO, C, PROG) * - no serial communication active ***************************************************************************/ for (;;){ // change displaystate every 10 seconds 0 to 5 // Activate Auto Mode // setautomode(true); // 1) process keypresses if (m_key_action){ m_key_action = false; state_mnt = !(m_state_keys & KEYMASK_MOUNT); // State of keys AUTO, C and PROG and valve mounted if ((m_state_keys & KEYMASK_AUTO) != 0){ display_mode--; LCD_SetHourBarVal(display_mode, LCD_MODE_ON); ref_pos_changed = true; LCD_SetSeg(LCD_SEG_AUTO, LCD_MODE_ON); } else { LCD_SetSeg(LCD_SEG_AUTO, LCD_MODE_OFF); } if ((m_state_keys & KEYMASK_C) != 0){ if (display_mode==9){ m_reftemp = input_temp(m_reftemp); } //LCD_SetHourBarVal(display_mode, LCD_MODE_ON); //ref_pos_changed = true; //LCD_SetSeg(LCD_SEG_MANU, LCD_MODE_ON); } else { LCD_SetSeg(LCD_SEG_MANU, LCD_MODE_OFF); } if ((m_state_keys & KEYMASK_PROG)!= 0){ display_mode++; LCD_SetHourBarVal(display_mode+1, LCD_MODE_ON); ref_pos_changed = true; LCD_SetSeg(LCD_SEG_PROG, LCD_MODE_ON); } else { LCD_SetSeg(LCD_SEG_PROG, LCD_MODE_OFF); } } // 2) calc new valveposition if new temp available // - temp is measured by IRQ // 3) calibrate motor // - start calibration if valve is now mounted // TODO: // (during calibration the main loop stops for a long time // maybe add global var to cancel callibration, e.g.: if // HR20 removed from ther gear) // - reset calibration is valve mounted changed to off if (last_state_mnt != state_mnt) { MOTOR_SetMountStatus(state_mnt); if (state_mnt) { LCD_ClearNumbers(); LCD_PrintChar(LCD_CHAR_C, 3, LCD_MODE_ON); LCD_PrintChar(LCD_CHAR_A, 2, LCD_MODE_ON); LCD_PrintChar(LCD_CHAR_L, 1, LCD_MODE_ON); LCD_Update(); // DEBUG: if next line is disabled, not calibration and no // motor control is done // MOTOR_Calibrate(ref_position, speed); LCD_ClearNumbers(); } } // 4) start motor if // - actual valveposition != desired valveposition // - motor is off // - motor is calibrated if ((ref_pos_changed) && (MOTOR_IsCalibrated())){ err = MOTOR_Goto(ref_position, speed); ref_pos_changed = false; } // 5) if motor is on call MOTOR_CheckBlocked at least once a second // - that switches motor of if it is blocked if (MOTOR_On()){ if (last_second != RTC_GetSecond()){ MOTOR_CheckBlocked(); last_second = RTC_GetSecond(); } } // 6) store keystate at end of loop before going to sleep last_statekey = m_state_keys; last_state_mnt = state_mnt; // 7) Check if there is a serial command to process // Loop until all is processed e_meassure(); // test call to sample values and send them to serial port while( (COM_Process() == true) ){}; // 8) goto sleep if // - motor is of // - no key is pressed (AUTO, C, PROG) // - no serial communication active if (display_mode==1) { // Ub: ADC Value Hex ADC_Measure_Ub(); value16 = ADC_Get_Bat_Val(); LCD_PrintHexW(value16, LCD_MODE_ON); }else if (display_mode==2) { // Ub: ADC Value Decimal ADC_Measure_Ub(); value16 = ADC_Get_Bat_Val(); LCD_PrintDecW(value16, LCD_MODE_ON); }else if (display_mode==3) { // Ub: Voltage [mV] ADC_Measure_Ub(); value16 = ADC_Get_Bat_Voltage(); LCD_PrintDecW(value16, LCD_MODE_ON); }else if (display_mode==4) { // Temp: ADC Value Hex ADC_Measure_Temp(); value16 = ADC_Get_Temp_Val(); LCD_PrintHexW(value16, LCD_MODE_ON); }else if (display_mode==5) { // Temp: ADC Value Decimal ADC_Measure_Temp(); value16 = ADC_Get_Temp_Val(); LCD_PrintDecW(value16, LCD_MODE_ON); }else if (display_mode==6) { // Temp: Temperature (Degree) ADC_Measure_Temp(); value16s = ADC_Get_Temp_Degree(); LCD_PrintTempInt(value16s, LCD_MODE_ON); }else if (display_mode==7) { // - 9,87 °C value16s = -987; LCD_PrintTempInt(value16s, LCD_MODE_ON); }else if (display_mode==8) { // 98,76 °C value16s = 9876; LCD_PrintTempInt(value16s, LCD_MODE_ON); }else{ value16 = display_mode; LCD_PrintDecW(value16, LCD_MODE_ON); } /* // Bar 24 on if calibrated if (MOTOR_IsCalibrated()) { LCD_SetSeg(LCD_SEG_BAR24, LCD_MODE_ON); } else { LCD_SetSeg(LCD_SEG_BAR24, LCD_MODE_OFF); } // Hour 0 on if state_mnt if (state_mnt) { LCD_SetSeg(LCD_SEG_B0, LCD_MODE_ON); } else { LCD_SetSeg(LCD_SEG_B0, LCD_MODE_OFF); } // Hour 1 on if last_state_mnt if (last_state_mnt) { LCD_SetSeg(LCD_SEG_B1, LCD_MODE_ON); } else { LCD_SetSeg(LCD_SEG_B1, LCD_MODE_OFF); } if (!MOTOR_IsCalibrated()) { LCD_PrintChar(LCD_CHAR_E, 3, LCD_MODE_ON); LCD_PrintChar(LCD_CHAR_2, 2, LCD_MODE_ON); } else { LCD_PrintDec(ref_position, 1, LCD_MODE_ON); LCD_PrintDec(MOTOR_GetPosPercent(), 0, LCD_MODE_ON); } */ // impulses = MOTOR_GetImpulses(); // update Display each main loop LCD_Update(); } //End Main loop return 0; }
// *** LCD *** // *********** LCD_Update(); // print DEC and HEX values LCD_PrintDec(i, 0, LCD_MODE_ON); LCD_PrintHex(i, 1, LCD_MODE_ON); // m_reftemp LCD_PrintTemp(m_reftemp, LCD_MODE_ON); LCD_PrintTemp (i, LCD_MODE_ON); // Hour Bar shows displaystate LCD_SetHourBarVal(i, LCD_MODE_ON); LCD_SetHourBarBar(i, LCD_MODE_ON); LCD_SetHourBarSeg(i, LCD_MODE_ON); // mm:ss LCD_PrintDec(RTC_GetMinute(), 1, LCD_MODE_ON); LCD_PrintDec(RTC_GetSecond(), 0, LCD_MODE_ON); // hh:mm LCD_PrintDec(RTC_GetHour() , 1, LCD_MODE_ON); LCD_PrintDec(RTC_GetMinute(), 0, LCD_MODE_ON); // DD.MM LCD_PrintDec(RTC_GetDay() , 1, LCD_MODE_ON); LCD_PrintDec(RTC_GetMonth() , 0, LCD_MODE_ON); // YY YY tmp= RTC_GetYearYY(); LCD_PrintDec( (20+(tmp/100) ), 1, LCD_MODE_ON); LCD_PrintDec( (tmp%100) , 0, LCD_MODE_ON); // Weekday LCD_PrintDayOfWeek(RTC_GetDayOfWeek(), LCD_MODE_ON); // 24 hour bar LCD_SetSeg(LCD_SEG_BAR24, LCD_MODE_ON); LCD_SetHourBarSeg(i, LCD_MODE_OFF);
void Get_RealTimeString(char *TimeString) { char timeBuffer[9]; uint8 scroll = 0; TimeStruct.Time = RTC_GetTime(); sprintf(timeBuffer, "%02lu:%02lu:%02lu", RTC_GetHours(TimeStruct.Time), RTC_GetMinutes(TimeStruct.Time), RTC_GetSecond(TimeStruct.Time)); for(scroll = 0; scroll < 9; scroll++) { TimeString[scroll] = timeBuffer[scroll]; } }