Skip to content

Commit

Permalink
Add analogWrite Arduino primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
tolauwae authored and carllocos committed Jul 5, 2023
1 parent adee735 commit 7c0065a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
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;

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);
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

0 comments on commit 7c0065a

Please sign in to comment.