VERSION of : currently running good with weekday changing

This commit is contained in:
Charlez Kwan 2020-01-21 15:31:13 +01:00
parent 284a503ded
commit e4e0066412

View file

@ -73,12 +73,15 @@ SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
PAINT_TIME sPaint_time;
RTC_TimeTypeDef sTime;
RTC_TimeTypeDef sSetStart[7] = {{1,2,3},{13,2,3},{21,2,3},{25,2,3},{1,2,3},{1,2,3},{1,2,3}};
RTC_TimeTypeDef sSetEnd[7] = {{22,22,23},{23,2,23},{2,22,3},{25,22,3},{21,2,3},{21,2,3},{21,2,3}};
RTC_DateTypeDef sDate;
RTC_TimeTypeDef sTimeSet = {0,0,0};
RTC_TimeTypeDef sSetStart[7] = {{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
RTC_TimeTypeDef sSetEnd[7] = {{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
RTC_DateTypeDef sDate = {0,1,1,20};
const uint8_t WeekLetter[7][2]={
"Mo",
"Di",
@ -88,11 +91,15 @@ const uint8_t WeekLetter[7][2]={
"Sa",
"So"
};
enum Mode{Running, Choose, Set, Offscreen};
uint8_t system_mode = Running;
uint8_t setWeekday,weekday,hour,minsec;
uint8_t selector_pos;
//Create a new image cache
UBYTE *BlackImage;
UWORD Imagesize = ((EPD_WIDTH % 8 == 0) ? (EPD_WIDTH / 8) : (EPD_WIDTH / 8 + 1)) * EPD_HEIGHT;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@ -103,14 +110,20 @@ static void MX_USART1_UART_Init(void);
static void MX_RTC_Init(void);
/* USER CODE BEGIN PFP */
void Paint_DrawArray(void);
void Paint_DrawTimeArray(void);
void Paint_DrawWeekday(void);
void Paint_DrawCurrent(uint8_t weekday);
void Paint_DrawLoad(bool draw_yes);
void Paint_DrawCurrent(void);
void Paint_Drawonoff(bool);
void Paint_Selector(bool, uint8_t,uint8_t);
bool System_fire(void);
bool System_SecondHeartbeat(void);
void ButtonLeft_Pressed(void);
void ButtonPress_Pressed(void);
void ButtonRight_Pressed(void);
void Change_systemsetmode(void);
void Change_systemmode(void);
void Change_selectorpos(bool);
void Change_time(uint8_t,bool);
void Read_intosettime(void);
void Write_intostime(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
@ -152,20 +165,20 @@ int main(void)
/* USER CODE BEGIN 2 */
if (EPD_Init(lut_full_update) != 0){
printf("e-Paper init failed\r\n");
}
EPD_Clear();
DEV_Delay_ms(500);
}
EPD_Clear();
DEV_Delay_ms(500);
if ((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL){
printf("Failed to apply for black memory...\r\n");
return 1;
}
Paint_NewImage(BlackImage, EPD_WIDTH, EPD_HEIGHT, 270, WHITE);
Paint_SelectImage(BlackImage);
Paint_Clear(WHITE);
sPaint_time.Hour = 0;
sPaint_time.Min = 0;
sPaint_time.Sec = 0;
if ((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL){
printf("Failed to apply for black memory...\r\n");
return 1;
}
Paint_NewImage(BlackImage, EPD_WIDTH, EPD_HEIGHT, 270, WHITE);
Paint_SelectImage(BlackImage);
Paint_Clear(WHITE);
sPaint_time.Hour = 0;
sPaint_time.Min = 0;
sPaint_time.Sec = 0;
//Partial refresh, example shows time
@ -174,10 +187,7 @@ int main(void)
}
Paint_SelectImage(BlackImage);
sPaint_time.Hour = 12;
sPaint_time.Min = 34;
sPaint_time.Sec = 56;
selector_pos = 0;
for (;;){
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); // Get Time
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); // Get Date
@ -186,15 +196,17 @@ int main(void)
sPaint_time.Sec = sTime.Seconds;
// printf("hour %i :",sPaint_time.Hour);
// printf("min %i :",sPaint_time.Min);
// printf("sec %i :",sPaint_time.Sec);
Paint_DrawArray();
Paint_DrawCurrent();
Paint_DrawTimeArray();
Paint_DrawWeekday();
Paint_DrawCurrent(0);
Paint_DrawLoad(System_SecondHeartbeat());
Paint_Selector(System_SecondHeartbeat(),selector_pos, system_mode);
Paint_Drawonoff(System_fire());
// printf("sTime hour:", sTime.Hours);
// printf(" : sTime min :", sTime.Minutes);
// printf("\r\n");
EPD_Display(BlackImage);
DEV_Delay_ms(1000); //Analog clock 1s
DEV_Delay_ms(50); //Analog clock 1s
// printf("500ms..\r\n");
if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)){
ButtonLeft_Pressed();
@ -212,52 +224,13 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void Paint_DrawTimeArray(){
for(int i=0; i<7; i++){
sPaint_time.Hour = sSetStart[i].Hours;
sPaint_time.Min = sSetStart[i].Minutes;
sPaint_time.Sec = sSetStart[i].Seconds;
Paint_ClearWindows(5, i*20 + 5, 5 + Font20.Width * 7, i*20 + 5 + Font20.Height, WHITE);
Paint_DrawTime(5, i*20 + 5, &sPaint_time, &Font20, WHITE, BLACK);
sPaint_time.Hour = sSetEnd[i].Hours;
sPaint_time.Min = sSetEnd[i].Minutes;
sPaint_time.Sec = sSetEnd[i].Seconds;
Paint_ClearWindows(100, i*20 + 5, 100 + Font20.Width * 7, i*20 + 5 + Font20.Height, WHITE);
Paint_DrawTime(100, i*20 + 5, &sPaint_time, &Font20, WHITE, BLACK);
}
}
void Paint_DrawWeekday(){
for(uint8_t i=0; i<7 ; i++){
Paint_DrawChar(176,20*i+5,WeekLetter[i][0],&Font20,WHITE,BLACK);
Paint_DrawChar(187,20*i+5,WeekLetter[i][1],&Font20,WHITE,BLACK);
}
}
void Paint_DrawCurrent(uint8_t weekday){
sPaint_time.Hour = sTime.Hours;
sPaint_time.Min = sTime.Minutes;
sPaint_time.Sec = sTime.Seconds;
Paint_ClearWindows(50, 150, 50 + Font24.Width * 7, 150 + Font24.Height, WHITE);
Paint_DrawTime(50,150, &sPaint_time, &Font24, WHITE, BLACK);
Paint_DrawChar(140,150,WeekLetter[weekday][0],&Font20,WHITE,BLACK);
Paint_DrawChar(152,150,WeekLetter[weekday][1],&Font20,WHITE,BLACK);
}
void Paint_DrawLoad(bool draw_yes){
Paint_ClearWindows(50, 180, 70, 200, WHITE);
Paint_DrawRectangle(50, 180, 70, 200, BLACK, draw_yes, DOT_PIXEL_1X1);
}
/**
* @brief System Clock Configuration
@ -315,14 +288,8 @@ static void MX_RTC_Init(void)
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
// RTC_TimeTypeDef sSet[7] = {{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3},{1,2,3}};
// sSet[0].Hours=1;
// sSet[0].Minutes=2;
// sSet[0].Seconds=3;
RTC_DateTypeDef DateToUpdate = {0};
RTC_TimeTypeDef sTime = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
@ -352,7 +319,7 @@ static void MX_RTC_Init(void)
}
DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
DateToUpdate.Month = RTC_MONTH_JANUARY;
DateToUpdate.Date = 0x1;
DateToUpdate.Date = 0x2;
DateToUpdate.Year = 0x0;
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
@ -464,6 +431,12 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : ButtonLeft_Pin ButtonPress_Pin ButtonRight_Pin */
GPIO_InitStruct.Pin = ButtonLeft_Pin|ButtonPress_Pin|ButtonRight_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
@ -494,6 +467,129 @@ void Paint_DrawArray(){
}
}
void Paint_DrawCurrent(){
weekday = sDate.WeekDay;
sPaint_time.Hour = sTime.Hours;
sPaint_time.Min = sTime.Minutes;
sPaint_time.Sec = sTime.Seconds;
if(system_mode == Set && (selector_pos == 28 || selector_pos == 29 || selector_pos == 30)){
sPaint_time.Hour = sTimeSet.Hours;
sPaint_time.Min = sTimeSet.Minutes;
sPaint_time.Sec = sTimeSet.Seconds;
weekday = setWeekday;
}
Paint_ClearWindows(50, 175, 50 + Font24.Width * 7, 175 + Font24.Height, WHITE);
Paint_DrawTime(50,175, &sPaint_time, &Font24, WHITE, BLACK);
Paint_DrawChar(140,175,WeekLetter[weekday][0],&Font20,WHITE,BLACK);
Paint_DrawChar(152,175,WeekLetter[weekday][1],&Font20,WHITE,BLACK);
}
void Paint_Drawonoff(bool draw_yes){
Paint_ClearWindows(180, 180, 190, 190, WHITE);
Paint_DrawRectangle(180, 180, 190, 190, BLACK, draw_yes, DOT_PIXEL_1X1);
}
void Paint_Selector(bool heartbeat,uint8_t pos, uint8_t runningmode){
if(runningmode == Choose || runningmode == Set){
if(runningmode == Choose){
heartbeat = true;
}
if(pos< MAX_SSET_POS){
const uint8_t sset_selectorpos[4]={10,45,105,140};
const uint8_t sset_selectorlength=20;
uint8_t sset_selectorrow= pos/4 + 1;
Paint_DrawLine(sset_selectorpos[pos%4],25*sset_selectorrow,sset_selectorpos[pos%4]+sset_selectorlength,25*sset_selectorrow,BLACK,heartbeat,DOT_PIXEL_2X2);
}
if(pos == 28){
Paint_DrawLine(55,195,80,195,BLACK,heartbeat,DOT_PIXEL_2X2);
}
if(pos == 29){
Paint_DrawLine(95,195,120,195,BLACK,heartbeat,DOT_PIXEL_2X2);
}
if(pos == 30){
Paint_DrawLine(140,195,165,195,BLACK,heartbeat,DOT_PIXEL_2X2);
}
for(uint8_t i=1 ; i<8 ; i++){
Paint_ClearWindows(0, 25*i, 200, 25*i + 2, WHITE);
}
}
}
bool System_fire(){ // TODO the comparison is still wrong( same start hour but different end hour doesnt work)
uint16_t Activetime_length_min=0, Currenttime_length_min=0;
if(sSetEnd[weekday].Hours > sSetStart[weekday].Hours){
Activetime_length_min = (sSetEnd[weekday].Hours * 60 + sSetEnd[weekday].Minutes) - (sSetStart[weekday].Hours * 60 + sSetStart[weekday].Minutes);
}
if(sSetEnd[weekday].Hours == sSetStart[weekday].Hours){
if(sSetEnd[weekday].Minutes > sSetStart[weekday].Minutes){
Activetime_length_min = sSetEnd[weekday].Minutes - sSetStart[weekday].Minutes;
}
if(sSetEnd[weekday].Minutes < sSetStart[weekday].Minutes){
Activetime_length_min = 60 * 24 - (sSetStart[weekday].Minutes - sSetEnd[weekday].Minutes);
}
}
if(sSetEnd[weekday].Hours < sSetStart[weekday].Hours){
Activetime_length_min = ( (sSetEnd[weekday].Hours + 24) * 60 + sSetEnd[weekday].Minutes ) - (sSetStart[weekday].Hours*60 + sSetStart[weekday].Minutes);
}
if(sTime.Hours > sSetStart[weekday].Hours){
Currenttime_length_min = (sTime.Hours * 60 + sTime.Minutes) - (sSetStart[weekday].Hours * 60 + sSetStart[weekday].Minutes);
}
if(sTime.Hours == sSetStart[weekday].Hours){
if(sTime.Minutes > sSetStart[weekday].Minutes){
Currenttime_length_min = sTime.Minutes - sSetStart[weekday].Minutes;
}
if(sTime.Minutes < sSetStart[weekday].Minutes){
Currenttime_length_min = 60 * 24 - (sSetStart[weekday].Minutes - sTime.Minutes);
}
}
if(sTime.Hours < sSetStart[weekday].Hours){
Currenttime_length_min = ( (sTime.Hours + 24) * 60 + sTime.Minutes ) - (sSetStart[weekday].Hours*60 + sSetStart[weekday].Minutes);
}
if(Currenttime_length_min < Activetime_length_min){
return true;
}
return false;
// if(sTime.Hours > sSetStart[weekday].Hours && sTime.Hours < sSetEnd[weekday].Hours){
// return true;
// }else{
// if(sTime.Hours == sSetStart[weekday].Hours && sTime.Minutes >= sSetStart[weekday].Minutes){
// if(sTime.Hours <= sSetEnd[weekday].Hours){
// return true;
// }
// if(sTime.Hours == sSetEnd[weekday].Hours && sTime.Minutes <= sSetEnd[weekday].Minutes){
// return true;
// }
// }
// if(sTime.Hours == sSetEnd[weekday].Hours && sTime.Minutes <= sSetEnd[weekday].Minutes){
// if(sTime.Hours >= sSetStart[weekday].Hours){
// return true;
// }
// if(sTime.Hours == sSetStart[weekday].Hours && sTime.Minutes >= sSetStart[weekday].Minutes){
// return true;
// }
// }
// }
//
// return false;
}
bool System_SecondHeartbeat(){
if(sTime.Seconds%2 == 0){
return true;
}else{
return false;
}
}
//keep it short, executed in ISR
void ButtonLeft_Pressed(void){
if(system_mode==Choose){
@ -534,6 +630,106 @@ void ButtonRight_Pressed(void){
}
};
void Change_systemsetmode(){
if(system_mode == Choose){
system_mode = Set;
}
if(system_mode == Set){
system_mode = Choose;
}
}
void Change_systemmode(){
system_mode == 2 ? system_mode = 0 : system_mode++;
}
void Change_time(uint8_t selectorpos, bool increase){
if(selectorpos < 28){
uint8_t setrow = selectorpos/4;
uint8_t setpos = selectorpos%4;
if(increase){
//ssetstart
if(setpos<2){
if(setpos==0){
sSetStart[setrow].Hours == 23 ? sSetStart[setrow].Hours = 0 : sSetStart[setrow].Hours ++;
}
if(setpos==1){
sSetStart[setrow].Minutes == 59 ? sSetStart[setrow].Minutes = 0 : sSetStart[setrow].Minutes ++;
}
}else{ //ssetend
if(setpos==2){
sSetEnd[setrow].Hours == 23 ? sSetEnd[setrow].Hours = 0 : sSetEnd[setrow].Hours ++;
}
if(setpos==3){
sSetEnd[setrow].Minutes == 59 ? sSetEnd[setrow].Minutes = 0 : sSetEnd[setrow].Minutes ++;
}
}
}else{
//ssetstart
if(setpos<2){
if(setpos==0){
sSetStart[setrow].Hours == 0 ? sSetStart[setrow].Hours = 23 : sSetStart[setrow].Hours --;
}
if(setpos==1){
sSetStart[setrow].Minutes == 0 ? sSetStart[setrow].Minutes = 59 : sSetStart[setrow].Minutes --;
}
}else{ //ssetend
if(setpos==2){
sSetEnd[setrow].Hours == 0 ? sSetEnd[setrow].Hours = 23 : sSetEnd[setrow].Hours --;
}
if(setpos==3){
sSetEnd[setrow].Minutes == 0 ? sSetEnd[setrow].Minutes = 59 : sSetEnd[setrow].Minutes --;
}
}
}
}
if(selectorpos >= 28 && selectorpos < 31){
if(selectorpos == 28){
if(increase){
sTimeSet.Hours == 23 ? sTimeSet.Hours = 0 : sTimeSet.Hours++;
}else{
sTimeSet.Hours == 0 ? sTimeSet.Hours = 23 : sTimeSet.Hours--;
}
}
if(selectorpos == 29){
if(increase){
sTimeSet.Minutes == 59 ? sTimeSet.Minutes = 0 : sTimeSet.Minutes++;
}else{
sTimeSet.Minutes == 0 ? sTimeSet.Minutes = 59 : sTimeSet.Minutes--;
}
}
if(selectorpos == 30){
if(increase){
setWeekday == 6 ? setWeekday = 0 : setWeekday++;
}else{
setWeekday == 0 ? setWeekday = 6 : setWeekday--;
}
}
}
}
void Change_selectorpos(bool increase){
if(increase){
selector_pos == 30 ? selector_pos = 0 : selector_pos++;
}else{
selector_pos == 0 ? selector_pos = 30 : selector_pos--;
}
};
void Read_intosettime(){
sTimeSet = sTime;
setWeekday = sDate.WeekDay;
};
void Write_intostime(){
sTime = sTimeSet;
sDate.Date = setWeekday + 2; //date hack because the RTC auto detect the weekday from the date
}
int __io_putchar(int ch){
uint8_t c[1];
c[0] = ch & 0x00FF;
@ -548,6 +744,7 @@ int _write(int file, char *ptr, int len){
}
return len;
}
/* USER CODE END 4 */
/**