esp-ena/components/interface/interface-main.c

144 lines
4.9 KiB
C
Raw Permalink Normal View History

2020-08-16 16:40:05 +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.
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "esp_log.h"
#include "driver/gpio.h"
#include "ds3231.h"
#include "ssd1306.h"
#include "ssd1306-gfx.h"
#include "wifi-controller.h"
#include "ena-storage.h"
#include "ena-exposure.h"
#include "interface.h"
static time_t current_timstamp;
static struct tm *current_tm;
static char time_buffer[32];
static char text_buffer[32];
static bool wifi_connected;
void interface_main_set(void)
{
interface_datetime_start();
}
void interface_main_rst(void)
{
interface_report_start();
}
void interface_main_display(void)
{
if (wifi_connected)
{
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_wifi, 8, 0, 0, false);
}
else
{
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_cross, 8, 0, 0, false);
}
time(&current_timstamp);
current_tm = localtime(&current_timstamp);
// curent date
sprintf(text_buffer, "%s %s %d",
interface_get_label_text(&interface_texts_weekday[current_tm->tm_wday]),
interface_get_label_text(&interface_texts_month[current_tm->tm_mon]),
current_tm->tm_mday);
ssd1306_text_line_column(SSD1306_ADDRESS, text_buffer, 0, 16 - strlen(text_buffer), false);
// current time
strftime(time_buffer, 16, INTERFACE_FORMAT_TIME, current_tm);
ssd1306_text_line_column(SSD1306_ADDRESS, time_buffer, 1, 16 - strlen(time_buffer), false);
}
void interface_main_start(void)
{
interface_register_button_callback(INTERFACE_BUTTON_RST, &interface_main_rst);
interface_register_button_callback(INTERFACE_BUTTON_SET, &interface_main_set);
interface_register_button_callback(INTERFACE_BUTTON_LFT, NULL);
interface_register_button_callback(INTERFACE_BUTTON_RHT, NULL);
interface_register_button_callback(INTERFACE_BUTTON_MID, NULL);
interface_register_button_callback(INTERFACE_BUTTON_UP, NULL);
interface_register_button_callback(INTERFACE_BUTTON_DWN, NULL);
if (wifi_controller_connection() != NULL)
{
wifi_connected = true;
}
else
{
wifi_connected = false;
}
interface_set_display_function(&interface_main_display);
ena_exposure_summary_t *current_exposure_summary = ena_exposure_current_summary();
time(&current_timstamp);
uint32_t last_update = ena_storage_read_last_exposure_date();
// status unknown if no update or last update older than two days
if (last_update == 0 || ((current_timstamp - last_update) / (60 * 60 * 24)) > 2)
{
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_question[0], 24, 0, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_question[1], 24, 1, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_question[2], 24, 2, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_question[3], 24, 3, 12, false);
}
else if (current_exposure_summary->max_risk_score < 100)
{
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_smile[0], 24, 0, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_smile[1], 24, 1, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_smile[2], 24, 2, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_smile[3], 24, 3, 12, false);
}
else
{
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_sad[0], 24, 0, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_sad[1], 24, 1, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_sad[2], 24, 2, 12, false);
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_sad[3], 24, 3, 12, false);
}
// clock icon
ssd1306_data(SSD1306_ADDRESS, ssd1306_gfx_clock, 8, 4, 8, false);
// last update
struct tm *last_update_tm = localtime((time_t*) &last_update);
sprintf(time_buffer, "%02d %s %02d:%02d",
last_update_tm->tm_mday,
interface_get_label_text(&interface_texts_month[last_update_tm->tm_mon]),
last_update_tm->tm_hour,
last_update_tm->tm_min);
if (last_update != 0)
{
ssd1306_text_line_column(SSD1306_ADDRESS, time_buffer, 4, 3, false);
}
// buttons
ssd1306_set_button(SSD1306_ADDRESS, interface_get_label_text(&interface_text_button_menu), true, false);
ssd1306_set_button(SSD1306_ADDRESS, interface_get_label_text(&interface_text_button_report), false, true);
ESP_LOGD(INTERFACE_LOG, "start main interface");
}