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+ }
0 commit comments