bool CExpressivePixelsApp::AppInitialize()

in Firmware/ExpressivePixelsCore/EPXApp.cpp [91:212]


bool CExpressivePixelsApp::AppInitialize(CLEDDriverBase *pLEDDriver, uint16_t *pArrayMatrix, uint16_t arrayWidth, uint16_t arrayHeight)
{
	// Configure hardware if pins have been set
	EPXPlatform_GPIO_Initialize(this, SystemGPIOEventHandler);	
	EPXPlatform_GPIO_PinConfigure(GPIO_PIN_FEATURE, EPXGPIO_INPUT);	
	EPXPlatform_GPIO_ButtonClickConfigure(GPIO_PIN_FEATURE);
	if (GPIO_PIN_3V3_ACCEN > 0)
		pinMode(GPIO_PIN_3V3_ACCEN, OUTPUT);
	if (GPIO_PIN_BOOSTER_ENABLE > 0)
		pinMode(GPIO_PIN_BOOSTER_ENABLE, OUTPUT);
	
	// Do a quick LED flash upon boot
	pinMode(GPIO_PIN_STATUSLED, OUTPUT);
	digitalWrite(GPIO_PIN_STATUSLED, HIGH);
	delay(250);
	digitalWrite(GPIO_PIN_STATUSLED, LOW);

	// Check state of Feature button at boot to do a 'default' system restore
	bool bFeatureButtonPressedOnBoot = !EPXPlatform_GPIO_PinRead(GPIO_PIN_FEATURE);
		
	// Configure display and array
	m_CDisplayTopology.SetMatrix(pArrayMatrix, arrayWidth, arrayHeight);
	m_CAnimator.SetTopology(&m_CDisplayTopology);
	m_CDisplayArray.Initialize(pLEDDriver, arrayWidth, arrayHeight, DISPLAYARRAY_POWERPIN);
	
	// Get remaining free memory for diagnostics
	m_bootUpRAM = freeRam();

	DataChannelPurge();
	DEBUGLOGLN("** BOOTUP RAM ** %s", EPXString(freeRam()).c_str());

#ifdef VARIANTCAPABILITY_BATTERY_MONITORING	
	// Initialize Battery Monitor
	m_BatteryMonitor.Initialize();
	SampleBattery();
#endif

	// Initialize storage layers, nd load settings
	m_CAppStorage.Initialize(bFeatureButtonPressedOnBoot);
	m_CAppStorage.SetReadDirectFromFile(true);   // On by default
	CSettings::Initialize();

	/**** Read persisted app settings ****/
	// Setting - load Bluetooth advertising name, and USB device descriptor name
	CSettings::ReadString((const char *) SETTINGSKEY_DEVICENAME, m_szDeviceName, sizeof(m_szDeviceName));
	m_CBLEChannel.SetDeviceName(m_szDeviceName);
	
	// Setting - load AES Key
	CSettings::Read((const char *) SETTINGSKEY_AESKEY, m_aesKey, sizeof(m_aesKey));
		
	// Setting - load autoplay
	m_bAutoPlayOnUSBPower = true;
	CSettings::Read((const char *) SETTINGSKEY_AUTOPLAYONUSBPOWER, &m_bAutoPlayOnUSBPower, sizeof(m_bAutoPlayOnUSBPower));
	DEBUGLOGLN("AUTOPLAYONUSBPOWER %s", m_bAutoPlayOnUSBPower ? "ON" : "OFF");
	
	// Setting - load last set brightness
	uint8_t bootBrightness = 0;
	CSettings::Read((const char *) SETTINGSKEY_BRIGHTNESS, &bootBrightness, sizeof(bootBrightness));
	if (bootBrightness > 0)
		m_CDisplayArray.SetBrightness(bootBrightness);
	DEBUGLOGLN("BOOT BRIGHTNESS %s", EPXString((int) bootBrightness).c_str());
	
	// Setting - load display rotation
	int rotateBy = 0;
	CSettings::Read((const char *) SETTINGSKEY_ROTATEBY, &rotateBy, sizeof(rotateBy));	
	m_CAnimator.SetRotation(rotateBy);
	
	// Setting - load BeaconActivation enabled
	CSettings::Read((const char *) SETTINGSKEY_BEACONACTIVATION, &m_bBeaconStartupActivation, sizeof(m_bBeaconStartupActivation));
	DEBUGLOGLN("BEACONSTARTUPACTIVATION %s", m_bBeaconStartupActivation ? "ENABLED" : "DISABLED");

	// Setting - load BeaconActivation AlwaysOn
	CSettings::Read((const char *) SETTINGSKEY_BEACONACTIVATIONALWAYSON, &m_bBeaconActivationAlwaysOn, sizeof(m_bBeaconActivationAlwaysOn));	
	DEBUGLOGLN("BEACONSTARTUPACTIVATION ALWAYSON %s", m_bBeaconActivationAlwaysOn ? "ON" : "OFF");
	
	// Initialize and start USB Channel **BEFORE** the Bluetooth Channel
	m_CUSBChannel.Initialize();

	// Initialize and start Bluetooth Channel
	m_CBLEChannel.Initialize(g_szDEFAULT_BLE_NAME);
	m_CBLEChannel.SetBeaconActivationEntries(m_beaconActivation.EntriesReference());
	m_beaconActivation.Load();

	// Initialize crypto for auhentication
	EPXPlatform_Crypto_Initialize();

	// USB device name is based on realized BLE name (that may autogenerate from MAC address)
	m_CUSBChannel.SetDeviceName(m_CBLEChannel.GetRealizedDeviceName());
		
	// Initialize trigger sources
	RegisterTriggerSource(&m_CSwitchActivation);

	// Finally start Bluetooth advertising
	m_CBLEChannel.Start();
	
#ifdef EPXMIDI
	// Initialize MIDI
	digitalWrite(GPIO_PIN_3V3_ACCEN, HIGH);
	m_CMIDIActivation.Initialize();
	m_CMIDIActivation.SetAnimationActivationHandler(this, ActivateSequenceByNameEvent);
	m_CMIDIActivation.SetTraceEventHandler(this, TraceEventHandler);
	m_bMIDIActivation = true;
#endif	
	
	DEBUGLOGLN("** WAITING RAM ** %s", EPXString(freeRam()).c_str());
	m_BootMillis = millis();

	// Do a pending power state so the system will go to sleep
	m_pendingPowerStateChange = EPXAPP_PENDINGPOWERSTATE_BLE_OFF;

	// Register all configured buttons
	EPXPlatform_GPIO_ButtonClickFinalize();
	
	// Do initial power up state activations for MIDI, BLE advertising etc
	ChannelsInitialized();
	
	// Activate USB
	m_CUSBChannel.Activate();	
	
	DEBUGLOGLN("** INITIALIZED **");
	return true;
}