From 4599f7e080d1bdacb28d5c12712704860c4ea80d Mon Sep 17 00:00:00 2001 From: tolauwae Date: Sun, 18 Jun 2023 12:09:20 +0200 Subject: [PATCH 1/2] Add analogWrite Arduino primitive --- src/Primitives/arduino.cpp | 20 ++++++++++++++++++-- src/Primitives/emulated.cpp | 11 +++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Primitives/arduino.cpp b/src/Primitives/arduino.cpp index 163f7ac4..e75c692a 100644 --- a/src/Primitives/arduino.cpp +++ b/src/Primitives/arduino.cpp @@ -131,7 +131,7 @@ int resolve_isr(int pin) { // Primitives #define NUM_PRIMITIVES 0 -#define NUM_PRIMITIVES_ARDUINO 37 +#define NUM_PRIMITIVES_ARDUINO 38 #define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO) @@ -519,6 +519,14 @@ def_prim(chip_analog_read, oneToOneI32) { return true; } +def_prim(chip_analog_write, twoToNoneU32) { + uint8_t pin = arg1.uint32; + uint8_t brightness = arg0.uint32; + pop_args(2); + analogWrite(pin, brightness); + return true; +} + // warning: undefined symbol: write_spi_byte def_prim(write_spi_byte, oneToNoneU32) { write_spi_byte(arg0.uint32); @@ -568,7 +576,7 @@ def_prim(clear_pixels, NoneToNoneU32) { // LED Control primitives -def_prim(chip_analog_write, threeToNoneU32) { +def_prim(chip_ledc_set_duty, threeToNoneU32) { uint8_t channel = arg2.uint32; uint32_t value = arg1.uint32; uint32_t maxValue = arg0.uint32; @@ -608,6 +616,8 @@ def_prim(subscribe_interrupt, threeToNoneU32) { uint8_t tidx = arg1.uint32; // Table Idx pointing to Callback function uint8_t mode = arg0.uint32; + printf("subscribe_interrupt(%i, %i, %i)\n", pin, tidx, mode); + int index = resolve_isr(pin); if (index < 0) { dbg_info("subscribe_interrupt: no ISR found for pin %i\n", pin); @@ -619,6 +629,11 @@ def_prim(subscribe_interrupt, threeToNoneU32) { return false; } + if (tidx < 0 || m->table.size < tidx) { + dbg_info("subscribe_interrupt: out of range table index %i\n", tidx); + return false; + } + attachInterrupt(digitalPinToInterrupt(pin), ISRs[index].ISR_callback, mode); String callback_id = INTERRUPT_TOPIC_PREFIX; @@ -978,6 +993,7 @@ void install_primitives() { install_primitive(chip_analog_write); install_primitive(chip_ledc_setup); install_primitive(chip_ledc_attach_pin); + install_primitive(chip_ledc_set_duty); dbg_info("INSTALLING ISRs\n"); install_isrs(); diff --git a/src/Primitives/emulated.cpp b/src/Primitives/emulated.cpp index a579372a..9a95fd08 100644 --- a/src/Primitives/emulated.cpp +++ b/src/Primitives/emulated.cpp @@ -25,7 +25,7 @@ #include "primitives.h" #define NUM_PRIMITIVES 0 -#define NUM_PRIMITIVES_ARDUINO 28 +#define NUM_PRIMITIVES_ARDUINO 29 #define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO) @@ -393,6 +393,12 @@ def_prim(chip_analog_read, oneToOneI32) { return true; } +def_prim(chip_analog_write, twoToNoneU32) { + debug("EMU: chip_analog_write(%u,%u) \n", arg1.uint32, arg0.uint32); + pop_args(2); + return true; +} + def_prim(chip_delay, oneToNoneU32) { using namespace std::this_thread; // sleep_for, sleep_until using namespace std::chrono; // nanoseconds, system_clock, seconds @@ -454,7 +460,7 @@ def_prim(subscribe_interrupt, threeToNoneU32) { } // Temporary Primitives needed for analogWrite in ESP32 -def_prim(chip_analog_write, threeToNoneU32) { +def_prim(chip_ledc_set_duty, threeToNoneU32) { uint8_t channel = arg2.uint32; uint32_t value = arg1.uint32; uint32_t maxValue = arg0.uint32; @@ -523,6 +529,7 @@ void install_primitives() { install_primitive(chip_analog_write); install_primitive(chip_ledc_setup); install_primitive(chip_ledc_attach_pin); + install_primitive(chip_ledc_set_duty); } //------------------------------------------------------ From 38cd4407ba73ef55bc2d5d1b3d1105e698522058 Mon Sep 17 00:00:00 2001 From: tolauwae Date: Wed, 5 Jul 2023 10:42:02 +0200 Subject: [PATCH 2/2] Apply review comments (#190) --- src/Primitives/arduino.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Primitives/arduino.cpp b/src/Primitives/arduino.cpp index e75c692a..a049d904 100644 --- a/src/Primitives/arduino.cpp +++ b/src/Primitives/arduino.cpp @@ -616,7 +616,7 @@ def_prim(subscribe_interrupt, threeToNoneU32) { uint8_t tidx = arg1.uint32; // Table Idx pointing to Callback function uint8_t mode = arg0.uint32; - printf("subscribe_interrupt(%i, %i, %i)\n", pin, tidx, mode); + dbg_info("subscribe_interrupt(%i, %i, %i)\n", pin, tidx, mode); int index = resolve_isr(pin); if (index < 0) {