xref: /aosp_15_r20/external/libwebsockets/minimal-examples/embedded/esp32/esp-heltec-wb32/main/devices.c (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1 /*
2  * devices for ESP32 Heltec WB32
3  *
4  * Written in 2010-2020 by Andy Green <[email protected]>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  */
9 
10 #define LWIP_PROVIDE_ERRNO 1
11 #define _ESP_PLATFORM_ERRNO_H_
12 
13 #include <stdio.h>
14 #include "sdkconfig.h"
15 #include "freertos/FreeRTOS.h"
16 #include "freertos/task.h"
17 
18 #include <driver/gpio.h>
19 
20 #include <libwebsockets.h>
21 
22 struct lws_led_state *lls;
23 lws_display_state_t lds;
24 struct lws_button_state *bcs;
25 lws_netdev_instance_wifi_t *wnd;
26 
27 /*
28  * Hook up bitbang i2c, display driver and display
29  */
30 
31 static void
esp32_i2c_delay(void)32 esp32_i2c_delay(void)
33 {
34 	ets_delay_us(1);
35 }
36 
37 static const lws_bb_i2c_t li2c = {
38 	.bb_ops				= lws_bb_i2c_ops,
39 	.scl				= GPIO_NUM_15,
40 	.sda				= GPIO_NUM_4,
41 	.gpio				= &lws_gpio_plat,
42 	.delay				= esp32_i2c_delay
43 };
44 
45 /*
46  * Button controller
47  */
48 
49 static const lws_button_map_t bcm[] = {
50 	{
51 		.gpio			= GPIO_NUM_0,
52 		.smd_interaction_name	= "user"
53 	},
54 };
55 
56 static const lws_button_controller_t bc = {
57 	.smd_bc_name			= "bc",
58 	.gpio_ops			= &lws_gpio_plat,
59 	.button_map			= &bcm[0],
60 	.active_state_bitmap		= 0,
61 	.count_buttons			= LWS_ARRAY_SIZE(bcm),
62 };
63 
64 /*
65  * pwm controller
66  */
67 
68 static const lws_pwm_map_t pwm_map[] = {
69 	{ .gpio = GPIO_NUM_25, .index = 0, .active_level = 1 }
70 };
71 
72 static const lws_pwm_ops_t pwm_ops = {
73 	lws_pwm_plat_ops,
74 	.pwm_map			= &pwm_map[0],
75 	.count_pwm_map			= LWS_ARRAY_SIZE(pwm_map)
76 };
77 
78 static const lws_display_ssd1306_t disp = {
79 	.disp = {
80 		lws_display_ssd1306_ops,
81 		.w			= 128,
82 		.h			= 64
83 	},
84 	.i2c				= (lws_i2c_ops_t *)&li2c,
85 	.gpio				= &lws_gpio_plat,
86 	.reset_gpio			= GPIO_NUM_16,
87 	.i2c7_address			= SSD1306_I2C7_ADS1
88 };
89 
90 /*
91  * led controller
92  */
93 
94 static const lws_led_gpio_map_t lgm[] = {
95 	{
96 		.name			= "alert",
97 		.gpio			= GPIO_NUM_25,
98 		.pwm_ops		= &pwm_ops, /* managed by pwm */
99 		.active_level		= 1,
100 	},
101 };
102 
103 static const lws_led_gpio_controller_t lgc = {
104 	.led_ops			= lws_led_gpio_ops,
105 	.gpio_ops			= &lws_gpio_plat,
106 	.led_map			= &lgm[0],
107 	.count_leds			= LWS_ARRAY_SIZE(lgm)
108 };
109 
110 /*
111  * Settings stored in platform nv
112  */
113 
114 static const lws_settings_ops_t sett = {
115 	lws_settings_ops_plat
116 };
117 
118 /*
119  * Wifi
120  */
121 
122 static const lws_netdev_ops_t wifi_ops = {
123 	lws_netdev_wifi_plat_ops
124 };
125 
126 int
init_plat_devices(struct lws_context * ctx)127 init_plat_devices(struct lws_context *ctx)
128 {
129 	lws_settings_instance_t *si;
130 	lws_netdevs_t *netdevs = lws_netdevs_from_ctx(ctx);
131 
132 	si = lws_settings_init(&sett, (void *)"nvs");
133 	if (!si) {
134 		lwsl_err("%s: failed to create settings instance\n", __func__);
135 		return 1;
136 	}
137 	netdevs->si = si;
138 
139 #if 0
140 	/*
141 	 * This is a temp hack to bootstrap the settings to contain the test
142 	 * AP ssid and passphrase for one time, so the settings can be stored
143 	 * while there's no UI atm
144 	 */
145 	{
146 		lws_wifi_creds_t creds;
147 
148 		memset(&creds, 0, sizeof(creds));
149 
150 		lws_strncpy(creds.ssid, "xxx", sizeof(creds.ssid));
151 		lws_strncpy(creds.passphrase, "yyy", sizeof(creds.passphrase));
152 		lws_dll2_add_tail(&creds.list, &netdevs->owner_creds);
153 
154 		if (lws_netdev_credentials_settings_set(netdevs)) {
155 			lwsl_err("%s: failed to write bootstrap creds\n",
156 					__func__);
157 			return 1;
158 		}
159 	}
160 #endif
161 
162 	/* create the wifi network device and configure it */
163 
164 	wnd = (lws_netdev_instance_wifi_t *)
165 			wifi_ops.create(ctx, &wifi_ops, "wl0", NULL);
166 	if (!wnd) {
167 		lwsl_err("%s: failed to create wifi object\n", __func__);
168 		return 1;
169 	}
170 
171 	wnd->flags |= LNDIW_MODE_STA;
172 
173 	if (wifi_ops.configure(&wnd->inst, NULL)) {
174 		lwsl_err("%s: failed to configure wifi object\n", __func__);
175 		return 1;
176 	}
177 
178 	wifi_ops.up(&wnd->inst);
179 
180 	lls = lgc.led_ops.create(&lgc.led_ops);
181 	if (!lls) {
182 		lwsl_err("%s: could not create led\n", __func__);
183 		return 1;
184 	}
185 
186 	/* pwm init must go after the led controller init */
187 
188 	pwm_ops.init(&pwm_ops);
189 
190 	bcs = lws_button_controller_create(ctx, &bc);
191 	if (!bcs) {
192 		lwsl_err("%s: could not create buttons\n", __func__);
193 		return 1;
194 	}
195 
196 	/*
197 	 * Show the lws logo on the display
198 	 */
199 
200 	lws_display_state_init(&lds, ctx, 10000, 20000, lls, &disp.disp);
201 
202 	lws_button_enable(bcs, 0, lws_button_get_bit(bcs, "user"));
203 	lws_led_transition(lls, "alert", &lws_pwmseq_static_off,
204 					 &lws_pwmseq_static_on);
205 
206 	return 0;
207 }
208