Skip to content

Commit 7c0065a

Browse files
tolauwaecarllocos
authored andcommitted
Add analogWrite Arduino primitive
1 parent adee735 commit 7c0065a

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/Primitives/arduino.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int resolve_isr(int pin) {
131131
// Primitives
132132

133133
#define NUM_PRIMITIVES 0
134-
#define NUM_PRIMITIVES_ARDUINO 37
134+
#define NUM_PRIMITIVES_ARDUINO 38
135135

136136
#define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO)
137137

@@ -519,6 +519,14 @@ def_prim(chip_analog_read, oneToOneI32) {
519519
return true;
520520
}
521521

522+
def_prim(chip_analog_write, twoToNoneU32) {
523+
uint8_t pin = arg1.uint32;
524+
uint8_t brightness = arg0.uint32;
525+
pop_args(2);
526+
analogWrite(pin, brightness);
527+
return true;
528+
}
529+
522530
// warning: undefined symbol: write_spi_byte
523531
def_prim(write_spi_byte, oneToNoneU32) {
524532
write_spi_byte(arg0.uint32);
@@ -568,7 +576,7 @@ def_prim(clear_pixels, NoneToNoneU32) {
568576

569577
// LED Control primitives
570578

571-
def_prim(chip_analog_write, threeToNoneU32) {
579+
def_prim(chip_ledc_set_duty, threeToNoneU32) {
572580
uint8_t channel = arg2.uint32;
573581
uint32_t value = arg1.uint32;
574582
uint32_t maxValue = arg0.uint32;
@@ -608,6 +616,8 @@ def_prim(subscribe_interrupt, threeToNoneU32) {
608616
uint8_t tidx = arg1.uint32; // Table Idx pointing to Callback function
609617
uint8_t mode = arg0.uint32;
610618

619+
printf("subscribe_interrupt(%i, %i, %i)\n", pin, tidx, mode);
620+
611621
int index = resolve_isr(pin);
612622
if (index < 0) {
613623
dbg_info("subscribe_interrupt: no ISR found for pin %i\n", pin);
@@ -619,6 +629,11 @@ def_prim(subscribe_interrupt, threeToNoneU32) {
619629
return false;
620630
}
621631

632+
if (tidx < 0 || m->table.size < tidx) {
633+
dbg_info("subscribe_interrupt: out of range table index %i\n", tidx);
634+
return false;
635+
}
636+
622637
attachInterrupt(digitalPinToInterrupt(pin), ISRs[index].ISR_callback, mode);
623638

624639
String callback_id = INTERRUPT_TOPIC_PREFIX;
@@ -978,6 +993,7 @@ void install_primitives() {
978993
install_primitive(chip_analog_write);
979994
install_primitive(chip_ledc_setup);
980995
install_primitive(chip_ledc_attach_pin);
996+
install_primitive(chip_ledc_set_duty);
981997

982998
dbg_info("INSTALLING ISRs\n");
983999
install_isrs();

src/Primitives/emulated.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "primitives.h"
2626

2727
#define NUM_PRIMITIVES 0
28-
#define NUM_PRIMITIVES_ARDUINO 28
28+
#define NUM_PRIMITIVES_ARDUINO 29
2929

3030
#define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO)
3131

@@ -393,6 +393,12 @@ def_prim(chip_analog_read, oneToOneI32) {
393393
return true;
394394
}
395395

396+
def_prim(chip_analog_write, twoToNoneU32) {
397+
debug("EMU: chip_analog_write(%u,%u) \n", arg1.uint32, arg0.uint32);
398+
pop_args(2);
399+
return true;
400+
}
401+
396402
def_prim(chip_delay, oneToNoneU32) {
397403
using namespace std::this_thread; // sleep_for, sleep_until
398404
using namespace std::chrono; // nanoseconds, system_clock, seconds
@@ -454,7 +460,7 @@ def_prim(subscribe_interrupt, threeToNoneU32) {
454460
}
455461

456462
// Temporary Primitives needed for analogWrite in ESP32
457-
def_prim(chip_analog_write, threeToNoneU32) {
463+
def_prim(chip_ledc_set_duty, threeToNoneU32) {
458464
uint8_t channel = arg2.uint32;
459465
uint32_t value = arg1.uint32;
460466
uint32_t maxValue = arg0.uint32;
@@ -523,6 +529,7 @@ void install_primitives() {
523529
install_primitive(chip_analog_write);
524530
install_primitive(chip_ledc_setup);
525531
install_primitive(chip_ledc_attach_pin);
532+
install_primitive(chip_ledc_set_duty);
526533
}
527534

528535
//------------------------------------------------------

0 commit comments

Comments
 (0)