Skip to content

Commit fd4c18e

Browse files
author
Carlos Rojas
committed
added primitives for ESP32 analogwrite
1 parent 84be87f commit fd4c18e

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/Primitives/arduino.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void write_spi_bytes_16_prim(int times, uint32_t color) {
5454
}
5555

5656
#define NUM_PRIMITIVES 0
57-
#define NUM_PRIMITIVES_ARDUINO 33
57+
#define NUM_PRIMITIVES_ARDUINO 36
5858

5959
#define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO)
6060

@@ -487,6 +487,40 @@ def_prim(clear_pixels, NoneToNoneU32) {
487487
return true;
488488
}
489489

490+
// Temporary Primitives needed for analogWrite in ESP32
491+
def_prim(chip_ledc_analog_write, threeToNoneU32) {
492+
uint8_t channel = arg2.uint32;
493+
uint32_t value = arg1.uint32;
494+
uint32_t maxValue = arg0.uint32;
495+
496+
// printf("chip_ledc_analog_write(%u, %u, %u)\n", channel, value, maxValue);
497+
// calculate duty, 4095 from 2 ^ 12 - 1
498+
uint32_t duty = (4095 / maxValue) * min(value, maxValue);
499+
500+
ledcWrite(channel, duty);
501+
pop_args(3);
502+
return true;
503+
}
504+
505+
def_prim(chip_ledc_setup, threeToNoneU32) {
506+
uint32_t channel = arg2.uint32;
507+
uint32_t freq = arg1.uint32;
508+
uint32_t ledc_timer = arg0.uint32;
509+
// printf("chip_ledc_setup(%u, %u, %u)\n", channel, freq, ledc_timer);
510+
ledcSetup(channel, freq, ledc_timer);
511+
pop_args(3);
512+
return true;
513+
}
514+
515+
def_prim(chip_ledc_attach_pin, twoToNoneU32) {
516+
uint32_t pin = arg1.uint32;
517+
uint32_t channel = arg0.uint32;
518+
// printf("chip_ledc_attach_pin(%u,%u)\n", pin, channel);
519+
ledcAttachPin(pin, channel);
520+
pop_args(2);
521+
return true;
522+
}
523+
490524
// INTERRUPTS
491525

492526
class Interrupt {
@@ -857,6 +891,11 @@ void install_primitives() {
857891
install_primitive(set_pixel_color);
858892
install_primitive(clear_pixels);
859893
install_primitive(show_pixels);
894+
895+
// temporary primitives needed for analogWrite in ESP32
896+
install_primitive(chip_ledc_analog_write);
897+
install_primitive(chip_ledc_setup);
898+
install_primitive(chip_ledc_attach_pin);
860899
}
861900

862901
//------------------------------------------------------

src/Primitives/emulated.cpp

Lines changed: 34 additions & 1 deletion
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 24
28+
#define NUM_PRIMITIVES_ARDUINO 27
2929

3030
#define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO)
3131

@@ -428,6 +428,34 @@ def_prim(subscribe_interrupt, threeToNoneU32) {
428428
pop_args(3);
429429
return true;
430430
}
431+
432+
// Temporary Primitives needed for analogWrite in ESP32
433+
def_prim(chip_ledc_analog_write, threeToNoneU32) {
434+
uint8_t channel = arg2.uint32;
435+
uint32_t value = arg1.uint32;
436+
uint32_t maxValue = arg0.uint32;
437+
// calculate duty, 4095 from 2 ^ 12 - 1
438+
printf("chip_ledc_analog_write(%u, %u, %u)\n", channel, value, maxValue);
439+
pop_args(3);
440+
return true;
441+
}
442+
443+
def_prim(chip_ledc_setup, threeToNoneU32) {
444+
uint32_t channel = arg2.uint32;
445+
uint32_t freq = arg1.uint32;
446+
uint32_t ledc_timer = arg0.uint32;
447+
printf("chip_ledc_setup(%u, %u, %u)\n", channel, freq, ledc_timer);
448+
pop_args(3);
449+
return true;
450+
}
451+
452+
def_prim(chip_ledc_attach_pin, twoToNoneU32) {
453+
uint32_t pin = arg1.uint32;
454+
uint32_t channel = arg0.uint32;
455+
printf("chip_ledc_attach_pin(%u,%u)\n", pin, channel);
456+
pop_args(2);
457+
return true;
458+
}
431459
//------------------------------------------------------
432460
// Installing all the primitives
433461
//------------------------------------------------------
@@ -459,6 +487,11 @@ void install_primitives() {
459487
install_primitive(set_pixel_color);
460488
install_primitive(clear_pixels);
461489
install_primitive(show_pixels);
490+
491+
// temporary mock primitives needed for analogWrite in ESP32
492+
install_primitive(chip_ledc_analog_write);
493+
install_primitive(chip_ledc_setup);
494+
install_primitive(chip_ledc_attach_pin);
462495
}
463496

464497
//------------------------------------------------------

0 commit comments

Comments
 (0)