Skip to content

Commit da74603

Browse files
committed
Added initial LED driver
1 parent bfe0258 commit da74603

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

source/application/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ C_FILES += \
4242
lua_libraries/display.c \
4343
lua_libraries/file.c \
4444
lua_libraries/imu.c \
45+
lua_libraries/led.c \
4546
lua_libraries/microphone.c \
4647
lua_libraries/system.c \
4748
lua_libraries/time.c \
@@ -85,6 +86,7 @@ C_FILES += \
8586
$(LIBRARIES)/lz4/lz4.c \
8687
$(LIBRARIES)/nrfx/drivers/src/nrfx_gpiote.c \
8788
$(LIBRARIES)/nrfx/drivers/src/nrfx_pdm.c \
89+
$(LIBRARIES)/nrfx/drivers/src/nrfx_pwm.c \
8890
$(LIBRARIES)/nrfx/drivers/src/nrfx_rtc.c \
8991
$(LIBRARIES)/nrfx/drivers/src/nrfx_saadc.c \
9092
$(LIBRARIES)/nrfx/drivers/src/nrfx_spim.c \

source/application/lua_libraries/frame_lua_libraries.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void lua_open_bluetooth_library(lua_State *L);
3535
void lua_open_camera_library(lua_State *L);
3636
void lua_open_display_library(lua_State *L);
3737
void lua_open_imu_library(lua_State *L);
38+
void lua_open_led_library(lua_State *L);
3839
void lua_open_microphone_library(lua_State *L);
3940
void lua_open_system_library(lua_State *L);
4041
void lua_open_time_library(lua_State *L);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This file is a part of: https://github.com/brilliantlabsAR/frame-codebase
3+
*
4+
* Authored by: Raj Nakarja / Brilliant Labs Ltd. (raj@brilliant.xyz)
5+
* Rohit Rathnam / Silicon Witchery AB (rohit@siliconwitchery.com)
6+
* Uma S. Gupta / Techno Exponent (umasankar@technoexponent.com)
7+
*
8+
* ISC Licence
9+
*
10+
* Copyright © 2024 Brilliant Labs Ltd.
11+
*
12+
* Permission to use, copy, modify, and/or distribute this software for any
13+
* purpose with or without fee is hereby granted, provided that the above
14+
* copyright notice and this permission notice appear in all copies.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22+
* PERFORMANCE OF THIS SOFTWARE.
23+
*/
24+
25+
#include "error_logging.h"
26+
#include "lauxlib.h"
27+
#include "lua.h"
28+
#include "nrfx_log.h"
29+
#include "nrfx_pwm.h"
30+
#include "pinout.h"
31+
32+
static const nrfx_pwm_t pwm = NRFX_PWM_INSTANCE(0);
33+
34+
static int lua_led_set_color(lua_State *L)
35+
{
36+
lua_Integer red = luaL_checkinteger(L, 1);
37+
lua_Integer green = luaL_checkinteger(L, 2);
38+
lua_Integer blue = luaL_checkinteger(L, 3);
39+
40+
if (red < 0 || red > 100 || green < 0 || green > 100 || blue < 0 || blue > 100)
41+
{
42+
luaL_error(L, "led color must be between 0 and 100");
43+
}
44+
45+
static nrf_pwm_values_individual_t seq_values;
46+
47+
seq_values.channel_0 = (uint16_t)red;
48+
seq_values.channel_1 = (uint16_t)green;
49+
seq_values.channel_2 = (uint16_t)blue;
50+
51+
nrf_pwm_sequence_t const seq = {
52+
.values.p_individual = &seq_values,
53+
.length = NRF_PWM_VALUES_LENGTH(seq_values),
54+
.repeats = 0,
55+
.end_delay = 0,
56+
};
57+
58+
nrfx_pwm_simple_playback(&pwm, &seq, 1, NRFX_PWM_FLAG_LOOP);
59+
60+
return 0;
61+
}
62+
63+
void lua_open_led_library(lua_State *L)
64+
{
65+
nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG(
66+
FRAME_LITE_LED_RED_PIN,
67+
FRAME_LITE_LED_GREEN_PIN,
68+
FRAME_LITE_LED_BLUE_PIN,
69+
NRF_PWM_PIN_NOT_CONNECTED);
70+
71+
config.pin_inverted[0] = true;
72+
config.pin_inverted[1] = true;
73+
config.pin_inverted[2] = true;
74+
75+
config.load_mode = NRF_PWM_LOAD_INDIVIDUAL;
76+
config.top_value = 100;
77+
78+
if (nrfx_pwm_init_check(&pwm) == false)
79+
{
80+
nrfx_pwm_init(&pwm, &config, NULL, NULL);
81+
}
82+
83+
lua_getglobal(L, "frame");
84+
85+
lua_newtable(L);
86+
87+
lua_pushcfunction(L, lua_led_set_color);
88+
lua_setfield(L, -2, "set_color");
89+
90+
lua_setfield(L, -2, "led");
91+
92+
lua_pop(L, 1);
93+
}

source/application/luaport.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void run_lua(bool is_paired)
9898
lua_open_microphone_library(L);
9999
lua_open_imu_library(L);
100100
lua_open_time_library(L);
101+
lua_open_led_library(L);
101102

102103
lua_open_file_library(L, !is_paired);
103104

source/application/nrfx_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
#define NRFX_PDM_ENABLED 1
3434
#define NRFX_PDM_DEFAULT_CONFIG_IRQ_PRIORITY 5
3535

36+
#define NRFX_PWM_ENABLED 1
37+
#define NRFX_PWM0_ENABLED 1
38+
#define NRFX_PWM1_ENABLED 1
39+
#define NRFX_PWM2_ENABLED 1
40+
3641
#define NRFX_RTC_ENABLED 1
3742
#define NRFX_RTC1_ENABLED 1
3843

tests/test_led.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Tests the Frame specific Lua libraries over Bluetooth.
3+
"""
4+
5+
import asyncio
6+
from frameutils import Bluetooth
7+
8+
9+
async def main():
10+
b = Bluetooth()
11+
12+
await b.connect(print_response_handler=lambda s: print(s))
13+
14+
# await b.send_lua("frame.led.set_color(100, 0, 0)")
15+
await asyncio.sleep(1)
16+
17+
# await b.send_lua("frame.led.set_color(0, 100, 0)")
18+
await asyncio.sleep(1)
19+
20+
await b.send_lua("frame.led.set_color(0, 0, 100)")
21+
await asyncio.sleep(1)
22+
23+
await b.disconnect()
24+
25+
26+
asyncio.run(main())

0 commit comments

Comments
 (0)