Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Primitives/arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

dbg_info("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);
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 9 additions & 2 deletions src/Primitives/emulated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

//------------------------------------------------------
Expand Down