Skip to content

Commit 4764643

Browse files
andy31415andreilitvincliffamzn
authored andcommitted
Add M5Stack specific builds for esp32 light (#25550)
* Enable builds for m5stack for more apps as m5stack and devkitc are the same MCU except screen support -i.e. defaults should work * start adding some compile options for ESP32 display ... still made a TODO for actual integration * Enable m5stack in Kconfig * Things compile now * Restyle * Remove demo items * Add more config variables to get displays working * Cleanup unused widgets, add callbacks for the wifi one * Make wifi green * Cleanup device with display: no more virtual devices * Restyle * Remove unnedded log * Remove unused variable * Remove useless commment in QRCodeScreen.h * Remove one more useless coment * Update examples/lighting-app/esp32/main/Button.cpp Co-authored-by: Cliff Chung <[email protected]> * Update examples/lighting-app/esp32/main/DeviceWithDisplay.cpp Co-authored-by: Cliff Chung <[email protected]> * Update examples/lighting-app/esp32/main/Globals.cpp Co-authored-by: Cliff Chung <[email protected]> * Update examples/lighting-app/esp32/main/StatusScreen.cpp Co-authored-by: Cliff Chung <[email protected]> * Update copyright dates and remove a bunch of @file comments * Move ifdefs around * Remove useless button.h comments * Move ifdefs a bit - HAVE_DISPLAY is a result of a header --------- Co-authored-by: Andrei Litvin <[email protected]> Co-authored-by: Cliff Chung <[email protected]>
1 parent 9885b68 commit 4764643

26 files changed

+1099
-81
lines changed

examples/common/screen-framework/Display.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2020 Project CHIP Authors
3+
* Copyright (c) 2020-2023 Project CHIP Authors
44
* Copyright (c) 2018 Nest Labs, Inc.
55
* All rights reserved.
66
*
@@ -16,14 +16,6 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
20-
/**
21-
* @file Display.cpp
22-
*
23-
* This file implements helper APIs for the M5Stack's display
24-
*
25-
*/
26-
2719
#include <string.h>
2820

2921
#include "driver/ledc.h"
@@ -54,7 +46,7 @@
5446
// The M5Stack's backlight is on Channel 7
5547
#define BACKLIGHT_CHANNEL LEDC_CHANNEL_7
5648

57-
extern const char * TAG;
49+
static const char * TAG = "Display";
5850

5951
uint16_t DisplayHeight = 0;
6052
uint16_t DisplayWidth = 0;

examples/lighting-app/esp32/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2021 Project CHIP Authors
2+
# Copyright (c) 2021-2023 Project CHIP Authors
33
# All rights reserved.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,6 +29,12 @@ set(EXTRA_COMPONENT_DIRS
2929
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/QRCode"
3030
)
3131

32+
if(${IDF_TARGET} STREQUAL "esp32")
33+
list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/tft"
34+
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/m5stack-tft/repo/components/spidriver"
35+
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/examples/common/screen-framework")
36+
endif()
37+
3238
project(chip-lighting-app)
3339

3440
# C++17 is required for RPC build.

examples/lighting-app/esp32/main/AppTask.cpp

+50-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2022 Project CHIP Authors
3+
* Copyright (c) 2022-2023 Project CHIP Authors
44
* All rights reserved.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +20,8 @@
2020
#include "esp_log.h"
2121
#include "freertos/FreeRTOS.h"
2222

23+
#include "DeviceWithDisplay.h"
24+
2325
#include <app-common/zap-generated/attributes/Accessors.h>
2426

2527
#define APP_TASK_NAME "APP"
@@ -36,7 +38,6 @@ using namespace ::chip::DeviceLayer;
3638
static const char * TAG = "app-task";
3739

3840
LEDWidget AppLED;
39-
Button AppButton;
4041

4142
namespace {
4243
constexpr EndpointId kLightEndpointId = 1;
@@ -61,14 +62,59 @@ CHIP_ERROR AppTask::StartAppTask()
6162
return (xReturned == pdPASS) ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED;
6263
}
6364

65+
void AppTask::ButtonEventHandler(const uint8_t buttonHandle, uint8_t btnAction)
66+
{
67+
if (btnAction != APP_BUTTON_PRESSED)
68+
{
69+
return;
70+
}
71+
72+
AppEvent button_event = {};
73+
button_event.Type = AppEvent::kEventType_Button;
74+
75+
#if CONFIG_HAVE_DISPLAY
76+
button_event.ButtonEvent.PinNo = buttonHandle;
77+
button_event.ButtonEvent.Action = btnAction;
78+
button_event.mHandler = ButtonPressedAction;
79+
#else
80+
button_event.mHandler = AppTask::LightingActionEventHandler;
81+
#endif
82+
83+
sAppTask.PostEvent(&button_event);
84+
}
85+
86+
#if CONFIG_DEVICE_TYPE_M5STACK
87+
void AppTask::ButtonPressedAction(AppEvent * aEvent)
88+
{
89+
uint32_t io_num = aEvent->ButtonEvent.PinNo;
90+
int level = gpio_get_level((gpio_num_t) io_num);
91+
if (level == 0)
92+
{
93+
bool woken = WakeDisplay();
94+
if (woken)
95+
{
96+
return;
97+
}
98+
// Button 1 is connected to the pin 39
99+
// Button 2 is connected to the pin 38
100+
// Button 3 is connected to the pin 37
101+
// So we use 40 - io_num to map the pin number to button number
102+
ScreenManager::ButtonPressed(40 - io_num);
103+
}
104+
}
105+
#endif
106+
64107
CHIP_ERROR AppTask::Init()
65108
{
66109
CHIP_ERROR err = CHIP_NO_ERROR;
67110

68111
AppLED.Init();
69-
AppButton.Init();
70112

71-
AppButton.SetButtonPressCallback(ButtonPressCallback);
113+
#if CONFIG_HAVE_DISPLAY
114+
InitDeviceDisplay();
115+
116+
AppLED.SetVLED(ScreenManager::AddVLED(TFT_YELLOW));
117+
#endif
72118

73119
return err;
74120
}
@@ -139,14 +185,6 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent)
139185
chip::DeviceLayer::PlatformMgr().UnlockChipStack();
140186
}
141187

142-
void AppTask::ButtonPressCallback()
143-
{
144-
AppEvent button_event;
145-
button_event.Type = AppEvent::kEventType_Button;
146-
button_event.mHandler = AppTask::LightingActionEventHandler;
147-
sAppTask.PostEvent(&button_event);
148-
}
149-
150188
void AppTask::UpdateClusterState()
151189
{
152190
ESP_LOGI(TAG, "Writing to OnOff cluster");
+68-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
3-
* Copyright (c) 2022 Project CHIP Authors
3+
* Copyright (c) 2022-2023 Project CHIP Authors
4+
* All rights reserved.
45
*
56
* Licensed under the Apache License, Version 2.0 (the "License");
67
* you may not use this file except in compliance with the License.
@@ -14,53 +15,95 @@
1415
* See the License for the specific language governing permissions and
1516
* limitations under the License.
1617
*/
18+
#include "driver/gpio.h"
19+
#include "esp_check.h"
20+
#include "esp_log.h"
21+
#include "esp_system.h"
1722

23+
#include "AppTask.h"
1824
#include "Button.h"
19-
#include "esp_attr.h"
25+
#include "Globals.h"
26+
#include "ScreenManager.h"
27+
#include <lib/support/CodeUtils.h>
28+
#include <platform/CHIPDeviceLayer.h>
29+
#include <vector>
2030

21-
#define GPIO_INPUT_IO_0 9
22-
#define GPIO_INPUT_PIN_SEL (1ULL << GPIO_INPUT_IO_0)
23-
#define ESP_INTR_FLAG_DEFAULT 0
31+
static const char * TAG = "Button.cpp";
2432

25-
static const char * TAG = "Button";
33+
extern Button gButtons[BUTTON_NUMBER];
2634

27-
static Button::ButtonPressCallback button_press_handler = nullptr;
35+
Button::Button() {}
2836

29-
static void IRAM_ATTR gpio_isr_handler(void * arg)
37+
Button::Button(gpio_num_t gpioNum)
3038
{
31-
if (button_press_handler != nullptr)
39+
mGPIONum = gpioNum;
40+
}
41+
42+
int32_t Find_Button_Via_Pin(gpio_num_t gpioNum)
43+
{
44+
for (int i = 0; i < BUTTON_NUMBER; i++)
3245
{
33-
button_press_handler();
46+
if (gButtons[i].GetGPIONum() == gpioNum)
47+
{
48+
return i;
49+
}
3450
}
51+
return -1;
3552
}
3653

37-
void Button::Init()
54+
void IRAM_ATTR button_isr_handler(void * arg)
3855
{
39-
/* Initialize button interrupt*/
40-
// zero-initialize the config structure.
56+
uint32_t gpio_num = (uint32_t) arg;
57+
int32_t idx = Find_Button_Via_Pin((gpio_num_t) gpio_num);
58+
if (idx == -1)
59+
{
60+
return;
61+
}
62+
BaseType_t taskWoken = pdFALSE;
63+
xTimerStartFromISR(gButtons[idx].mbuttonTimer,
64+
&taskWoken); // If the timer had already been started ,restart it will reset its expiry time
65+
}
66+
67+
esp_err_t Button::Init()
68+
{
69+
return Init(mGPIONum);
70+
}
71+
72+
esp_err_t Button::Init(gpio_num_t gpioNum)
73+
{
74+
esp_err_t ret = ESP_OK;
75+
76+
mGPIONum = gpioNum;
77+
// zero-initialize the config structure.
4178
gpio_config_t io_conf = {};
42-
// interrupt of rising edge
79+
// interrupt of falling edge
4380
io_conf.intr_type = GPIO_INTR_NEGEDGE;
4481
// bit mask of the pins, use GPIO4/5 here
45-
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
82+
io_conf.pin_bit_mask = 1ULL << gpioNum;
4683
// set as input mode
4784
io_conf.mode = GPIO_MODE_INPUT;
4885
// enable pull-up mode
4986
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
87+
5088
gpio_config(&io_conf);
5189

52-
// install gpio isr service
53-
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
5490
// hook isr handler for specific gpio pin
55-
gpio_isr_handler_add(static_cast<gpio_num_t>(GPIO_INPUT_IO_0), gpio_isr_handler, (void *) GPIO_INPUT_IO_0);
91+
ret = gpio_isr_handler_add(gpioNum, button_isr_handler, (void *) gpioNum);
92+
ESP_RETURN_ON_ERROR(ret, TAG, "gpio_isr_handler_add failed: %s", esp_err_to_name(ret));
5693

57-
ESP_LOGI(TAG, "Button initialized..");
58-
}
94+
mbuttonTimer = xTimerCreate("BtnTmr", // Just a text name, not used by the RTOS kernel
95+
pdMS_TO_TICKS(50), // timer period
96+
false, // no timer reload (==one-shot)
97+
(void *) (int) gpioNum, // init timer id = gpioNum index
98+
TimerCallback // timer callback handler (all buttons use
99+
// the same timer cn function)
100+
);
59101

60-
void Button::SetButtonPressCallback(ButtonPressCallback button_callback)
102+
return ESP_OK;
103+
}
104+
void Button::TimerCallback(TimerHandle_t xTimer)
61105
{
62-
if (button_callback != nullptr)
63-
{
64-
button_press_handler = button_callback;
65-
}
106+
// Get the button index of the expired timer and call button event Handler.
107+
uint32_t gpio_num = (uint32_t) pvTimerGetTimerID(xTimer);
108+
GetAppTask().ButtonEventHandler(gpio_num, APP_BUTTON_PRESSED);
66109
}

examples/lighting-app/esp32/main/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2021 Project CHIP Authors
2+
# Copyright (c) 2021-2023 Project CHIP Authors
33
# All rights reserved.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -85,6 +85,10 @@ set(SRC_DIRS_LIST "${SRC_DIRS_LIST}"
8585
)
8686
endif (CONFIG_ENABLE_PW_RPC)
8787

88+
if ("${CONFIG_DEVICE_TYPE_M5STACK}" STREQUAL "y")
89+
list(APPEND PRIV_REQUIRES_LIST tft screen-framework)
90+
endif()
91+
8892
idf_component_register(PRIV_INCLUDE_DIRS ${PRIV_INCLUDE_DIRS_LIST}
8993
SRC_DIRS ${SRC_DIRS_LIST}
9094
PRIV_REQUIRES ${PRIV_REQUIRES_LIST})

examples/lighting-app/esp32/main/DeviceCallbacks.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2021 Project CHIP Authors
3+
* Copyright (c) 2021-2023 Project CHIP Authors
44
* All rights reserved.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,17 +15,10 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
19-
/**
20-
* @file DeviceCallbacks.cpp
21-
*
22-
* Implements all the callbacks to the application from the CHIP Stack
23-
*
24-
**/
25-
2618
#include "AppTask.h"
2719

2820
#include "DeviceCallbacks.h"
21+
#include "Globals.h"
2922
#include "LEDWidget.h"
3023

3124
#include <app/util/util.h>
@@ -147,3 +140,13 @@ void emberAfOnOffClusterInitCallback(EndpointId endpoint)
147140
ESP_LOGI(TAG, "emberAfOnOffClusterInitCallback");
148141
GetAppTask().UpdateClusterState();
149142
}
143+
144+
void AppDeviceCallbacksDelegate::OnIPv4ConnectivityEstablished()
145+
{
146+
wifiLED.Set(true);
147+
}
148+
149+
void AppDeviceCallbacksDelegate::OnIPv4ConnectivityLost()
150+
{
151+
wifiLED.Set(false);
152+
}

0 commit comments

Comments
 (0)