2023-06-28 14:26:56 -04:00
// OMOTE firmware for ESP32
2024-03-10 14:27:46 -04:00
// 2023-2024 Maximilian Kern / Klaus Musch
// init hardware and hardware loop
# include "applicationInternal/hardware/hardwarePresenter.h"
// register devices and their commands
2024-04-16 08:21:16 -04:00
// special
2024-03-10 14:27:46 -04:00
# include "devices/misc/device_specialCommands.h"
2024-04-16 08:21:16 -04:00
# include "applicationInternal/commandHandler.h"
// keyboards
# include "devices/keyboard/device_keyboard_mqtt/device_keyboard_mqtt.h"
# include "devices/keyboard/device_keyboard_ble/device_keyboard_ble.h"
// TV
2024-03-10 14:27:46 -04:00
# include "devices/TV/device_samsungTV/device_samsungTV.h"
2024-04-16 08:21:16 -04:00
//#include "devices/TV/device_lgTV/device_lgTV.h"
// AV receiver
2024-03-10 14:27:46 -04:00
# include "devices/AVreceiver/device_yamahaAmp/device_yamahaAmp.h"
2024-04-16 08:21:16 -04:00
//#include "devices/AVreceiver/device_denonAvr/device_denonAvr.h"
//#include "devices/AVreceiver/device_lgsoundbar/device_lgsoundbar.h"
// media player
2024-03-10 14:27:46 -04:00
# include "devices/mediaPlayer/device_appleTV/device_appleTV.h"
2024-04-16 08:21:16 -04:00
//#include "devices/mediaPlayer/device_lgbluray/device_lgbluray.h"
//#include "devices/mediaPlayer/device_samsungbluray/device_samsungbluray.h"
//#include "devices/mediaPlayer/device_shield/device_shield.h"
// misc
2024-03-10 14:27:46 -04:00
# include "devices/misc/device_smarthome/device_smarthome.h"
// register gui and keys
# include "applicationInternal/gui/guiBase.h"
# include "applicationInternal/gui/guiRegistry.h"
2024-03-25 17:08:51 -04:00
# include "guis/gui_sceneSelection.h"
2024-03-10 14:27:46 -04:00
# include "guis/gui_irReceiver.h"
# include "guis/gui_settings.h"
# include "guis/gui_numpad.h"
# include "devices/mediaPlayer/device_appleTV/gui_appleTV.h"
# include "devices/misc/device_smarthome/gui_smarthome.h"
# include "applicationInternal/keys.h"
# include "applicationInternal/gui/guiStatusUpdate.h"
// register scenes
2024-03-25 17:11:53 -04:00
# include "scenes/scene__default.h"
2024-02-12 13:57:51 -05:00
# include "scenes/scene_allOff.h"
# include "scenes/scene_TV.h"
# include "scenes/scene_fireTV.h"
# include "scenes/scene_chromecast.h"
2024-03-25 17:08:51 -04:00
# include "scenes/scene_appleTV.h"
2024-03-10 14:27:46 -04:00
# include "applicationInternal/scenes/sceneHandler.h"
# if defined(ARDUINO)
// in case of Arduino we have a setup() and a loop()
void setup ( ) {
2023-06-28 14:26:56 -04:00
2024-03-10 14:27:46 -04:00
# elif defined(WIN32) || defined(__linux__)
// in case of Windows/Linux, we have only a main() function, no setup() and loop(), so we have to simulate them
// forward declaration of loop()
void loop ( unsigned long * pIMUTaskTimer , unsigned long * pUpdateStatusTimer ) ;
// main function as usual in C
int main ( int argc , char * argv [ ] ) {
# endif
2023-06-28 14:26:56 -04:00
// --- Startup ---
Serial . begin ( 115200 ) ;
2024-03-10 14:27:46 -04:00
// do some general hardware setup, like powering the TFT, I2C, ...
init_hardware_general ( ) ;
2024-03-15 16:35:13 -04:00
// get wakeup reason
init_sleep ( ) ;
2023-06-28 14:26:56 -04:00
// Restore settings from internal flash memory
2024-02-12 13:57:51 -05:00
init_preferences ( ) ;
2024-03-10 14:27:46 -04:00
// blinking led
init_userled ( ) ;
2024-02-12 13:57:51 -05:00
// Power Pin definition
init_battery ( ) ;
2024-03-10 14:27:46 -04:00
// button Pin definition for hardware keys
init_keys ( ) ;
2024-02-12 13:57:51 -05:00
// setup IR sender
init_infraredSender ( ) ;
2024-03-10 14:27:46 -04:00
# if (ENABLE_KEYBOARD_BLE == 1)
init_keyboardBLE ( ) ;
# endif
2024-02-12 13:57:51 -05:00
// register commands for the devices
2024-03-10 14:27:46 -04:00
register_specialCommands ( ) ;
2024-04-16 08:21:16 -04:00
// TV
2024-03-10 14:27:46 -04:00
register_device_samsungTV ( ) ;
2024-04-16 08:21:16 -04:00
// register_device_lgTV();
// AV receiver
2024-03-10 14:27:46 -04:00
register_device_yamahaAmp ( ) ;
2024-04-16 08:21:16 -04:00
//register_device_denonAvr();
//register_device_lgsoundbar();
// media player
2024-02-12 13:57:51 -05:00
register_device_appleTV ( ) ;
2024-04-16 08:21:16 -04:00
//register_device_lgbluray();
//register_device_samsungbluray();
//register_device_shield();
// misc
register_device_smarthome ( ) ;
2024-03-10 14:27:46 -04:00
# if (ENABLE_KEYBOARD_MQTT == 1)
2024-02-12 13:57:51 -05:00
register_device_keyboard_mqtt ( ) ;
# endif
2024-03-10 14:27:46 -04:00
# if (ENABLE_KEYBOARD_BLE == 1)
2024-02-12 13:57:51 -05:00
register_device_keyboard_ble ( ) ;
# endif
2024-03-10 14:27:46 -04:00
register_keyboardCommands ( ) ;
2024-02-12 13:57:51 -05:00
2024-04-22 15:52:48 -04:00
// Register the GUIs. They will be displayed in the order they have been registered.
// GUIs must be registered before the scenes, because only the scenes re-register key bindings to commands which have been defined after the key binding (see register_scene())
2024-03-25 17:08:51 -04:00
register_gui_sceneSelection ( ) ;
2024-02-12 13:57:51 -05:00
register_gui_irReceiver ( ) ;
register_gui_settings ( ) ;
register_gui_appleTV ( ) ;
2024-03-10 14:27:46 -04:00
register_gui_numpad ( ) ;
2024-02-12 13:57:51 -05:00
register_gui_smarthome ( ) ;
2024-03-25 17:08:51 -04:00
// Only show these GUIs in the main gui list. If you don't set this explicitely, by default all registered guis are shown.
# if (USE_SCENE_SPECIFIC_GUI_LIST != 0)
main_gui_list = { tabName_sceneSelection , tabName_smarthome , tabName_settings , tabName_irReceiver } ;
# endif
2024-04-22 11:12:47 -04:00
// register the scenes and their key_commands_*
register_scene_defaultKeys ( ) ;
register_scene_TV ( ) ;
register_scene_fireTV ( ) ;
register_scene_chromecast ( ) ;
register_scene_appleTV ( ) ;
register_scene_allOff ( ) ;
// Only show these scenes on the sceneSelection gui. If you don't set this explicitely, by default all registered scenes are shown.
set_scenes_on_sceneSelectionGUI ( { scene_name_TV , scene_name_fireTV , scene_name_chromecast , scene_name_appleTV } ) ;
2024-03-10 14:27:46 -04:00
// init GUI - will initialize tft, touch and lvgl
2024-02-12 13:57:51 -05:00
init_gui ( ) ;
2024-04-21 11:53:28 -04:00
setLabelActiveScene ( ) ;
2024-02-12 13:57:51 -05:00
gui_loop ( ) ; // Run the LVGL UI once before the loop takes over
2024-03-25 17:08:51 -04:00
2024-03-15 16:35:13 -04:00
// setup the Inertial Measurement Unit (IMU) for motion detection. Has to be after init_gui(), otherwise I2C will not work
init_IMU ( ) ;
2024-02-12 13:57:51 -05:00
2024-03-10 14:27:46 -04:00
// init WiFi - needs to be after init_gui() because WifiLabel must be available
# if (ENABLE_WIFI_AND_MQTT == 1)
2024-02-12 13:57:51 -05:00
init_mqtt ( ) ;
2023-06-28 14:26:56 -04:00
# endif
2024-03-10 09:41:50 -04:00
Serial . printf ( " Setup finished in %lu ms. \r \n " , millis ( ) ) ;
2024-03-10 14:27:46 -04:00
# if defined(WIN32) || defined(__linux__)
// In Windows/Linux there is no loop function that is automatically being called. So we have to do this on our own infinitely here in main()
unsigned long IMUTaskTimer = 0 ;
unsigned long updateStatusTimer = 0 ;
while ( 1 )
loop ( & IMUTaskTimer , & updateStatusTimer ) ;
# endif
2023-06-28 14:26:56 -04:00
}
// Loop ------------------------------------------------------------------------------------------------------------------------------------
2024-03-10 14:27:46 -04:00
# if defined(ARDUINO)
unsigned long IMUTaskTimer = 0 ;
unsigned long updateStatusTimer = 0 ;
unsigned long * pIMUTaskTimer = & IMUTaskTimer ;
unsigned long * pUpdateStatusTimer = & updateStatusTimer ;
2024-03-10 09:41:50 -04:00
void loop ( ) {
2024-03-10 14:27:46 -04:00
# elif defined(WIN32) || defined(__linux__)
void loop ( unsigned long * pIMUTaskTimer , unsigned long * pUpdateStatusTimer ) {
# endif
2023-06-28 14:26:56 -04:00
2024-03-10 14:27:46 -04:00
// --- do as often as possible --------------------------------------------------------
// update backlight brightness. Fade in on startup, dim before going to sleep
2024-02-12 13:57:51 -05:00
update_backligthBrighness ( ) ;
2024-03-10 14:27:46 -04:00
// keypad handling: get key states from hardware and process them
2024-03-10 09:41:50 -04:00
keypad_loop ( ) ;
2024-03-10 14:27:46 -04:00
// process IR receiver, if activated
if ( get_irReceiverEnabled ( ) ) {
2024-03-10 09:41:50 -04:00
infraredReceiver_loop ( ) ;
}
2024-03-10 14:27:46 -04:00
// update LVGL UI
gui_loop ( ) ;
2024-03-18 15:20:59 -04:00
// call mqtt loop to receive mqtt messages, if you are subscribed to some topics
# if (ENABLE_WIFI_AND_MQTT == 1)
mqtt_loop ( ) ;
# endif
2024-03-10 14:27:46 -04:00
// --- every 100 ms -------------------------------------------------------------------
// Refresh IMU data (motion detection) every 100 ms
// If no action (key, TFT or motion), then go to sleep
if ( millis ( ) - * pIMUTaskTimer > = 100 ) {
* pIMUTaskTimer = millis ( ) ;
2023-06-28 14:26:56 -04:00
2024-02-12 13:57:51 -05:00
check_activity ( ) ;
2024-03-18 15:20:59 -04:00
2023-06-28 14:26:56 -04:00
}
2024-03-10 14:27:46 -04:00
// --- every 1000 ms ------------------------------------------------------------------
if ( millis ( ) - * pUpdateStatusTimer > = 1000 ) {
* pUpdateStatusTimer = millis ( ) ;
2023-06-28 14:26:56 -04:00
2024-03-10 14:27:46 -04:00
// update user_led, battery, BLE, memoryUsage on GUI
updateHardwareStatusAndShowOnGUI ( ) ;
2024-02-12 13:57:51 -05:00
}
2023-06-28 14:26:56 -04:00
2024-02-12 13:57:51 -05:00
}