Skip to content

Commit 277d753

Browse files
add note about ESP32Servo library, add __has_include magic
1 parent 5abc6ef commit 277d753

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ For more about the original ulisp-esp see <http://www.ulisp.com/show?3M#esp-vers
77
This is based off of uLisp 4.6. For the old patches (some of which don't work) for
88
uLisp 4.3a please see the [4.3a-old](https://github.com/dragoncoder047/ulisp-esp32/tree/4.3a-old) branch.
99

10+
> [!NOTE]
11+
> This version includes (requires?) the [ESP32Servo](https://www.arduino.cc/reference/en/libraries/esp32servo/) library to get the analogWrite() and tone() functioning correctly. If you don't have it installed uLisp will compile but you won't have analogWrite() and tone().
12+
1013
New features, some care in editing required:
1114
* Lisp `:keywords` that auto-quote themselves
1215
* Ability to add multiple (more than one) extension tables (using `calloc()`) *may not be portable to other platforms*

ulisp-esp32.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define sdcardsupport
1717
// #define gfxsupport
1818
// #define lisplibrary
19-
#define toneimplemented
2019

2120
// Includes
2221
#include "ulisp.hpp"

ulisp.hpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
4444
#endif
4545
#endif
4646

47+
#ifdef __has_include
48+
#if __has_include(<ESP32Servo.h>)
49+
#include <ESP32Servo.h>
50+
#include <analogWrite.h>
51+
#include <ESP32Tone.h>
52+
#include <ESP32PWM.h>
53+
#define toneimplemented
54+
#endif
55+
#endif
56+
4757
#include <SD.h>
4858
#define SDSIZE 172
4959

@@ -57,10 +67,6 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
5767
#include "FS.h"
5868
#include <LittleFS.h>
5969

60-
#ifndef analogWrite
61-
#define analogWrite(x,y) dacWrite((x),(y))
62-
#endif
63-
6470
#ifndef LED_BUILTIN
6571
#define LED_BUILTIN 13
6672
#endif
@@ -2224,33 +2230,36 @@ void checkanalogread (int pin) {
22242230
}
22252231

22262232
void checkanalogwrite (int pin) {
2227-
// if (!(pin>=25 && pin<=26)) error("invalid pin", number(pin));
2228-
(void)pin;
2233+
#ifdef toneimplemented
2234+
// ERROR PWM channel unavailable on pin requested! 1
2235+
// PWM available on: 2,4,5,12-19,21-23,25-27,32-33
2236+
if (!(pin==2 || pin==4 || pin==5 || (pin>=12 && pin<=19) || (pin>=21 && pin<=23) || (pin>=25 && pin<=27) || pin==32 || pin==33)) error("not a PWM-capable pin", number(pin));
2237+
#else
2238+
if (!(pin>=25 && pin<=26)) error("not a DAC pin", number(pin));
2239+
#endif
22292240
}
22302241

22312242
// Note
22322243

2233-
#ifndef toneimplemented
2234-
void tone (int pin, int note) {
2235-
(void) pin, (void) note;
2236-
}
2237-
2238-
void noTone (int pin) {
2239-
(void) pin;
2240-
}
2241-
#endif
2242-
22432244
const int scale[] = {4186,4435,4699,4978,5274,5588,5920,6272,6645,7040,7459,7902};
22442245

22452246
void playnote (int pin, int note, int octave) {
2247+
#ifdef toneimplemented
22462248
int oct = octave + note/12;
22472249
int prescaler = 8 - oct;
22482250
if (prescaler<0 || prescaler>8) error("octave out of range", number(prescaler));
22492251
tone(pin, scale[note%12]>>prescaler);
2252+
#else
2253+
error2("not available");
2254+
#endif
22502255
}
22512256

22522257
void nonote (int pin) {
2258+
#ifdef toneimplemented
22532259
noTone(pin);
2260+
#else
2261+
error2("not available");
2262+
#endif
22542263
}
22552264

22562265
// Sleep
@@ -5027,7 +5036,12 @@ object* fn_analogwrite (object* args, object* env) {
50275036
else pin = checkinteger(arg);
50285037
checkanalogwrite(pin);
50295038
object* value = second(args);
5030-
analogWrite(pin, checkinteger(value));
5039+
#ifdef toneimplemented
5040+
analogWrite
5041+
#else
5042+
dacWrite
5043+
#endif
5044+
(pin, checkinteger(value));
50315045
return value;
50325046
}
50335047

0 commit comments

Comments
 (0)