2020-07-27 16:54:51 +02:00
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2020-08-16 16:40:05 +02:00
# include <string.h>
2020-07-27 16:54:51 +02:00
# include <time.h>
# include <sys/time.h>
# include "esp_log.h"
2020-08-16 16:40:05 +02:00
# include "driver/gpio.h"
2020-09-29 19:58:01 +02:00
# include "rtc.h"
# include "display.h"
# include "display-gfx.h"
2020-08-16 16:40:05 +02:00
2020-07-27 16:54:51 +02:00
# include "interface.h"
2020-08-16 16:40:05 +02:00
typedef enum
{
INTERFACE_DATETIME_STATE_HOUR = 0 ,
INTERFACE_DATETIME_STATE_MINUTE ,
INTERFACE_DATETIME_STATE_SECONDS ,
INTERFACE_DATETIME_STATE_DAY ,
INTERFACE_DATETIME_STATE_MONTH ,
INTERFACE_DATETIME_STATE_YEAR ,
} interface_datetime_state_t ;
static int current_interface_datetime_state ;
const uint32_t interface_datetime_steps [ 6 ] = { 3600 , 60 , 1 , 86400 , 2629800 , 31557600 } ;
int interface_datetime_state ( void )
{
return current_interface_datetime_state ;
}
2020-07-27 16:54:51 +02:00
2020-08-16 16:40:05 +02:00
void interface_datetime_set ( void )
{
interface_main_start ( ) ;
}
2020-07-27 16:54:51 +02:00
2020-08-16 16:40:05 +02:00
void interface_datetime_lft ( void )
{
interface_settings_start ( ) ;
}
2020-07-27 16:54:51 +02:00
2020-08-16 16:40:05 +02:00
void interface_datetime_rht ( void )
2020-07-27 16:54:51 +02:00
{
2020-08-16 16:40:05 +02:00
interface_wifi_start ( ) ;
2020-07-27 16:54:51 +02:00
}
2020-08-16 16:40:05 +02:00
void interface_datetime_mid ( void )
2020-07-27 16:54:51 +02:00
{
current_interface_datetime_state + + ;
2020-08-16 16:40:05 +02:00
if ( current_interface_datetime_state > INTERFACE_DATETIME_STATE_YEAR )
2020-07-27 16:54:51 +02:00
{
2020-08-16 16:40:05 +02:00
current_interface_datetime_state = INTERFACE_DATETIME_STATE_HOUR ;
2020-07-27 16:54:51 +02:00
}
ESP_LOGD ( INTERFACE_LOG , " datetime to %d " , current_interface_datetime_state ) ;
}
void interface_datetime_up ( void )
{
time_t curtime = time ( NULL ) ;
curtime + = interface_datetime_steps [ current_interface_datetime_state ] ;
struct timeval tv = { 0 } ;
tv . tv_sec = curtime ;
settimeofday ( & tv , NULL ) ;
2020-09-29 19:58:01 +02:00
rtc_set_time ( gmtime ( & curtime ) ) ;
2020-07-27 16:54:51 +02:00
ESP_LOGD ( INTERFACE_LOG , " increment %d about %u %s " , current_interface_datetime_state , interface_datetime_steps [ current_interface_datetime_state ] , ctime ( & curtime ) ) ;
}
2020-08-16 16:40:05 +02:00
void interface_datetime_dwn ( void )
2020-07-27 16:54:51 +02:00
{
time_t curtime = time ( NULL ) ;
curtime - = interface_datetime_steps [ current_interface_datetime_state ] ;
struct timeval tv = { 0 } ;
tv . tv_sec = curtime ;
settimeofday ( & tv , NULL ) ;
2020-09-29 19:58:01 +02:00
rtc_set_time ( gmtime ( & curtime ) ) ;
2020-07-27 16:54:51 +02:00
ESP_LOGD ( INTERFACE_LOG , " decrement %d about %u %s " , current_interface_datetime_state , interface_datetime_steps [ current_interface_datetime_state ] , ctime ( & curtime ) ) ;
}
2020-08-16 16:40:05 +02:00
void interface_datetime_display ( void )
2020-07-27 16:54:51 +02:00
{
2020-08-16 16:40:05 +02:00
static time_t current_timstamp ;
static struct tm * current_tm ;
static char time_buffer [ 9 ] ;
static char date_buffer [ 32 ] ;
2020-09-29 19:58:01 +02:00
display_menu_headline ( interface_get_label_text ( & interface_text_headline_time ) , true , 0 ) ;
2020-08-16 16:40:05 +02:00
static char edit_char [ 3 ] ;
static int edit_line = 3 ;
int edit_length = 2 ;
int edit_offset = 0 ;
2020-09-29 19:58:01 +02:00
display_clear_line ( edit_line - 1 , false ) ;
display_clear_line ( edit_line + 1 , false ) ;
2020-08-16 16:40:05 +02:00
time ( & current_timstamp ) ;
current_tm = localtime ( & current_timstamp ) ;
strftime ( time_buffer , 16 , INTERFACE_FORMAT_TIME , current_tm ) ;
2020-09-29 19:58:01 +02:00
display_text_line_column ( time_buffer , 3 , 4 , false ) ;
2020-08-16 16:40:05 +02:00
sprintf ( date_buffer , " %02d %s %02d " ,
current_tm - > tm_mday ,
interface_get_label_text ( & interface_texts_month [ current_tm - > tm_mon ] ) ,
current_tm - > tm_year - 100 ) ;
2020-09-29 19:58:01 +02:00
display_text_line_column ( date_buffer , 6 , 4 , false ) ;
2020-08-16 16:40:05 +02:00
switch ( interface_datetime_state ( ) )
{
case INTERFACE_DATETIME_STATE_YEAR :
memcpy ( & edit_char , & date_buffer [ 7 ] , edit_length ) ;
edit_line = 6 ;
edit_offset = 11 ;
break ;
case INTERFACE_DATETIME_STATE_DAY :
memcpy ( & edit_char , & date_buffer [ 0 ] , edit_length ) ;
edit_line = 6 ;
edit_offset = 4 ;
break ;
case INTERFACE_DATETIME_STATE_MONTH :
edit_length = 3 ;
memcpy ( & edit_char , & date_buffer [ 3 ] , edit_length ) ;
edit_line = 6 ;
edit_offset = 7 ;
break ;
case INTERFACE_DATETIME_STATE_HOUR :
memcpy ( & edit_char , & time_buffer [ 0 ] , edit_length ) ;
edit_line = 3 ;
edit_offset = 4 ;
break ;
case INTERFACE_DATETIME_STATE_MINUTE :
memcpy ( & edit_char , & time_buffer [ 3 ] , edit_length ) ;
edit_line = 3 ;
edit_offset = 7 ;
break ;
case INTERFACE_DATETIME_STATE_SECONDS :
memcpy ( & edit_char , & time_buffer [ 6 ] , edit_length ) ;
edit_line = 3 ;
edit_offset = 10 ;
break ;
}
2020-09-29 19:58:01 +02:00
display_data ( display_gfx_arrow_up , 8 , edit_line - 1 , edit_offset * 8 + 4 , false ) ;
display_chars ( edit_char , edit_length , edit_line , edit_offset , true ) ;
display_data ( display_gfx_arrow_down , 8 , edit_line + 1 , edit_offset * 8 + 4 , false ) ;
2020-07-27 16:54:51 +02:00
}
2020-08-16 16:40:05 +02:00
void interface_datetime_start ( void )
2020-07-27 16:54:51 +02:00
{
2020-08-16 16:40:05 +02:00
current_interface_datetime_state = INTERFACE_DATETIME_STATE_HOUR ;
2020-09-29 19:58:01 +02:00
interface_register_command_callback ( INTERFACE_COMMAND_LFT , & interface_datetime_lft ) ;
interface_register_command_callback ( INTERFACE_COMMAND_RHT , & interface_datetime_rht ) ;
interface_register_command_callback ( INTERFACE_COMMAND_MID , & interface_datetime_mid ) ;
interface_register_command_callback ( INTERFACE_COMMAND_UP , & interface_datetime_up ) ;
interface_register_command_callback ( INTERFACE_COMMAND_DWN , & interface_datetime_dwn ) ;
interface_register_command_callback ( INTERFACE_COMMAND_SET , & interface_datetime_set ) ;
interface_register_command_callback ( INTERFACE_COMMAND_RST , NULL ) ;
2020-08-16 16:40:05 +02:00
interface_set_display_function ( & interface_datetime_display ) ;
ESP_LOGD ( INTERFACE_LOG , " start datetime interface " ) ;
2020-07-27 16:54:51 +02:00
}