|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2022 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "AppTask.h" |
| 20 | +#include "Button.h" |
| 21 | +#include "DeviceWithDisplay.h" |
| 22 | +#include "Globals.h" |
| 23 | +#include "LEDWidget.h" |
| 24 | +#include "ScreenManager.h" |
| 25 | +#include "driver/gpio.h" |
| 26 | +#include "esp_log.h" |
| 27 | +#include "esp_spi_flash.h" |
| 28 | +#include "freertos/FreeRTOS.h" |
| 29 | +#include <app/server/OnboardingCodesUtil.h> |
| 30 | + |
| 31 | +#define APP_TASK_NAME "APP" |
| 32 | +#define APP_EVENT_QUEUE_SIZE 10 |
| 33 | +#define APP_TASK_STACK_SIZE (3072) |
| 34 | + |
| 35 | +static const char * TAG = "app-task"; |
| 36 | + |
| 37 | +namespace { |
| 38 | + |
| 39 | +QueueHandle_t sAppEventQueue; |
| 40 | +TaskHandle_t sAppTaskHandle; |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +AppTask AppTask::sAppTask; |
| 45 | + |
| 46 | +CHIP_ERROR AppTask::StartAppTask() |
| 47 | +{ |
| 48 | + sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent)); |
| 49 | + if (sAppEventQueue == NULL) |
| 50 | + { |
| 51 | + ESP_LOGE(TAG, "Failed to allocate app event queue"); |
| 52 | + return APP_ERROR_EVENT_QUEUE_FAILED; |
| 53 | + } |
| 54 | + |
| 55 | + // Start App task. |
| 56 | + BaseType_t xReturned; |
| 57 | + xReturned = xTaskCreate(AppTaskMain, APP_TASK_NAME, APP_TASK_STACK_SIZE, NULL, 1, &sAppTaskHandle); |
| 58 | + return (xReturned == pdPASS) ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED; |
| 59 | +} |
| 60 | + |
| 61 | +CHIP_ERROR AppTask::Init() |
| 62 | +{ |
| 63 | + /* Print chip information */ |
| 64 | + esp_chip_info_t chip_info; |
| 65 | + esp_chip_info(&chip_info); |
| 66 | + ESP_LOGI(TAG, "This is ESP32 chip with %d CPU cores, WiFi%s%s, ", chip_info.cores, |
| 67 | + (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : ""); |
| 68 | + ESP_LOGI(TAG, "silicon revision %d, ", chip_info.revision); |
| 69 | + ESP_LOGI(TAG, "%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024), |
| 70 | + (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); |
| 71 | + |
| 72 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 73 | + statusLED1.Init(STATUS_LED_GPIO_NUM); |
| 74 | + // Our second LED doesn't map to any physical LEDs so far, just to virtual |
| 75 | + // "LED"s on devices with screens. |
| 76 | + statusLED2.Init(GPIO_NUM_MAX); |
| 77 | + bluetoothLED.Init(); |
| 78 | + wifiLED.Init(); |
| 79 | + pairingWindowLED.Init(); |
| 80 | + |
| 81 | + // Print QR Code URL |
| 82 | + PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE)); |
| 83 | + |
| 84 | +#if CONFIG_HAVE_DISPLAY |
| 85 | + InitDeviceDisplay(); |
| 86 | +#endif |
| 87 | + return err; |
| 88 | +} |
| 89 | + |
| 90 | +void AppTask::AppTaskMain(void * pvParameter) |
| 91 | +{ |
| 92 | + AppEvent event; |
| 93 | + CHIP_ERROR err = sAppTask.Init(); |
| 94 | + if (err != CHIP_NO_ERROR) |
| 95 | + { |
| 96 | + ESP_LOGI(TAG, "AppTask.Init() failed due to %" CHIP_ERROR_FORMAT, err.Format()); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + ESP_LOGI(TAG, "App Task started"); |
| 101 | + |
| 102 | + while (true) |
| 103 | + { |
| 104 | + BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10)); |
| 105 | + while (eventReceived == pdTRUE) |
| 106 | + { |
| 107 | + sAppTask.DispatchEvent(&event); |
| 108 | + eventReceived = xQueueReceive(sAppEventQueue, &event, 0); // return immediately if the queue is empty |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void AppTask::PostEvent(const AppEvent * aEvent) |
| 114 | +{ |
| 115 | + if (sAppEventQueue != NULL) |
| 116 | + { |
| 117 | + BaseType_t status; |
| 118 | + if (xPortInIsrContext()) |
| 119 | + { |
| 120 | + BaseType_t higherPrioTaskWoken = pdFALSE; |
| 121 | + status = xQueueSendFromISR(sAppEventQueue, aEvent, &higherPrioTaskWoken); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + status = xQueueSend(sAppEventQueue, aEvent, 1); |
| 126 | + } |
| 127 | + if (!status) |
| 128 | + ESP_LOGE(TAG, "Failed to post event to app task event queue"); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + ESP_LOGE(TAG, "Event Queue is NULL should never happen"); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void AppTask::DispatchEvent(AppEvent * aEvent) |
| 137 | +{ |
| 138 | + if (aEvent->mHandler) |
| 139 | + { |
| 140 | + aEvent->mHandler(aEvent); |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + ESP_LOGI(TAG, "Event received with no handler. Dropping event."); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void AppTask::ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction) |
| 149 | +{ |
| 150 | + AppEvent button_event = {}; |
| 151 | + button_event.mType = AppEvent::kEventType_Button; |
| 152 | + button_event.mButtonEvent.mPinNo = btnIdx; |
| 153 | + button_event.mButtonEvent.mAction = btnAction; |
| 154 | + |
| 155 | + if (btnAction == APP_BUTTON_PRESSED) |
| 156 | + { |
| 157 | + button_event.mHandler = ButtonPressedAction; |
| 158 | + sAppTask.PostEvent(&button_event); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +void AppTask::ButtonPressedAction(AppEvent * aEvent) |
| 163 | +{ |
| 164 | +#if CONFIG_DEVICE_TYPE_M5STACK |
| 165 | + uint32_t io_num = aEvent->mButtonEvent.mPinNo; |
| 166 | + int level = gpio_get_level((gpio_num_t) io_num); |
| 167 | + if (level == 0) |
| 168 | + { |
| 169 | + bool woken = WakeDisplay(); |
| 170 | + if (woken) |
| 171 | + { |
| 172 | + return; |
| 173 | + } |
| 174 | + // Button 1 is connected to the pin 39 |
| 175 | + // Button 2 is connected to the pin 38 |
| 176 | + // Button 3 is connected to the pin 37 |
| 177 | + // So we use 40 - io_num to map the pin number to button number |
| 178 | + ScreenManager::ButtonPressed(40 - io_num); |
| 179 | + } |
| 180 | +#endif |
| 181 | +} |
0 commit comments