/* * * Copyright (c) 2024 Project CHIP Authors\ * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. */ /********************************************************** * Includes *********************************************************/ #include "AppTask.h" #include "AppConfig.h" #include "AppEvent.h" #include "LEDWidget.h" #ifdef DISPLAY_ENABLED #include "AirQualitySensorUI.h" #include "lcd.h" #ifdef QR_CODE_ENABLED #include "qrcodegen.h" #endif // QR_CODE_ENABLED #endif // DISPLAY_ENABLED #include #include #include #include #include #include #include #include #include #include #include #include #include #include /********************************************************** * Defines and Constants *********************************************************/ #define APP_FUNCTION_BUTTON 0 using namespace chip; using namespace chip::app; using namespace chip::TLV; using namespace chip::DeviceLayer; using namespace chip::app::Clusters::AirQuality; using namespace chip::app::Clusters; /********************************************************** * AppTask Definitions *********************************************************/ AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init() { CHIP_ERROR err = CHIP_NO_ERROR; chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler); #ifdef DISPLAY_ENABLED GetLCD().Init((uint8_t *) "Air-Quality-Sensor"); GetLCD().SetCustomUI(AirQualitySensorUI::DrawUI); #endif err = BaseApplication::Init(); if (err != CHIP_NO_ERROR) { ChipLogDetail(AppServer, "BaseApplication::Init() failed"); appError(err); } err = SensorManager::SensorMgr().Init(); if (err != CHIP_NO_ERROR) { ChipLogDetail(AppServer, "SensorMgr::Init() failed"); appError(err); } return err; } CHIP_ERROR AppTask::StartAppTask() { return BaseApplication::StartAppTask(AppTaskMain); } void AppTask::AppTaskMain(void * pvParameter) { AppEvent event; osMessageQueueId_t sAppEventQueue = *(static_cast(pvParameter)); CHIP_ERROR err = sAppTask.Init(); if (err != CHIP_NO_ERROR) { ChipLogDetail(AppServer, "AppTask.Init() failed"); appError(err); } #if !(defined(CHIP_CONFIG_ENABLE_ICD_SERVER) && CHIP_CONFIG_ENABLE_ICD_SERVER) sAppTask.StartStatusLEDTimer(); #endif ChipLogDetail(AppServer, "App Task started"); while (true) { osStatus_t eventReceived = osMessageQueueGet(sAppEventQueue, &event, NULL, osWaitForever); while (eventReceived == osOK) { sAppTask.DispatchEvent(&event); eventReceived = osMessageQueueGet(sAppEventQueue, &event, NULL, 0); } } } void AppTask::UpdateAirQualitySensorUI() { // Update the LCD with the Stored value. Show QR Code if not provisioned #ifdef DISPLAY_ENABLED GetLCD().WriteDemoUI(false); #ifdef QR_CODE_ENABLED if (BaseApplication::GetProvisionStatus()) { GetLCD().ShowQRCode(true); } #endif // QR_CODE_ENABLED #endif } void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction) { AppEvent aEvent = {}; aEvent.Type = AppEvent::kEventType_Button; aEvent.ButtonEvent.Action = btnAction; if (button == APP_FUNCTION_BUTTON) { aEvent.Handler = BaseApplication::ButtonHandler; sAppTask.PostEvent(&aEvent); } }