int main()

in BalancingRobot/Software/HighLevelApp/main.c [241:358]


int main(int argc, char* argv[])
{
    Log_Debug("App Starting...\n");

    if (argc >= 2) {
        Log_Debug("Setting Azure Scope ID %s\n", argv[1]);
        strncpy(scopeId, argv[1], SCOPEID_LENGTH);
    }
    else {
        Log_Debug("ScopeId needs to be set in the app_manifest CmdArgs\n");
        return -1;
    }

    char deviceTwinString[20];
    // get last device twin version.
    if (GetProfileString("DeviceTwinVersion", deviceTwinString, 20) != -1)
    {
        // set last DeviceTwin version, so we don't duplicate that behavior.
        lastDeviceTwinVersion = atol(deviceTwinString);
    }


    // compare current/last ScopeId and reset IoTC Message value
    char lastScopeId[20];
    if (GetProfileString("LastScopeId", &lastScopeId[0], 20) != -1)
    {
        if (strncasecmp(lastScopeId, scopeId, 20) != 0)
        {
            Log_Debug("Resetting lastDeviceTwinVersion - this/last ScopeIDs don't matchz\n");
            lastDeviceTwinVersion = 0;
        }
    }
    // save current scope id to 'lastScopeID'
    WriteProfileString("LastScopeId", scopeId);

    if (argc == 3)
    {
        if (strncasecmp("appa", argv[2], 4) == 0)
        {
            isAppA = true;
            currentIcon = AppA;
        }

        if (strncasecmp("appb", argv[2], 4) == 0)
        {
            isAppA = false;
            currentIcon = AppB;
        }
    }

    SetupADC();
    InitUDPThread();

    // setup timers.
    int ret = CreateTimers();
    if (ret == -1)
    {
        Log_Debug("Failed to setup data refresh timers...\n");
    }

    InitInterCoreCommunications(eventLoop);

    // enable the motors on the M4 app.
    struct UPDATE_ACTIVE updateActive;
    updateActive.id = MSG_UPDATE_ACTIVE;
    updateActive.updateActive = false;
    EnqueueIntercoreMessage(&updateActive, sizeof(updateActive));

	curl_global_init(CURL_GLOBAL_DEFAULT);

	// initialize the SSD1306 display
	SSD1306_Init(true);
	delay(10);

	SSD1306_RotateImage(App_A_Icon, AppA_Rot180, 32, 32, 90);
	SSD1306_RotateImage(App_B_Icon, AppB_Rot180, 32, 32, 90);

	SSD1306_RotateImage(Wifi_Icon, WiFiIcon_Rot180, 32, 32, 180);
	SSD1306_RotateImage(Wifi_Icon, WiFiIcon_Rot180, 32, 32, 180);
	SSD1306_RotateImage(IoTC_Icon, IotcIcon_Rot180, 32, 32, 180);
	SSD1306_RotateImage(Update_Icon, UpdateIcon_Rot180, 32, 32, 180);
    SSD1306_RotateImage(Update_Icon_Defer_Rejected, Update_Icon_Defer_Rejected_Rot180, 32, 32, 180);

    BatteryLevel = GetBatteryLevel();

    // clear wait icon once we have imuStable from intercore comms.
    ShowWaitIcon();
    // UpdateDisplay(BatteryLevel, HaveNetwork, HaveIoTC, currentIcon);

	// Register a SIGTERM handler for termination requests
	struct sigaction action;
	memset(&action, 0, sizeof(struct sigaction));
	action.sa_handler = TerminationHandler;
	sigaction(SIGTERM, &action, NULL);

	// Main loop
	while (!terminationRequired) {
		EventLoop_Run_Result result = EventLoop_Run(eventLoop, -1, true);
		// Continue if interrupted by signal, e.g. due to breakpoint being set.
		if (result == EventLoop_Run_Failed && errno != EINTR) {
			terminationRequired = true;
		}
	}

    // show 'updating' icon.
    if (updateApplied)
    {
        ShowUpdatingIcon();
    }
    else
    {
        // show 'white' display
        SSD1306_Clear();
        SSD1306_FillRegion(fillBuffer, 32, 128, 0, 0, 32, 128, true);
        SSD1306_DrawImage(fillBuffer, 32, 128, 0, 0);
        SSD1306_Display();
    }
}