Skip to content

Commit

Permalink
removed use of min/max macros (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
moose4lord authored Dec 1, 2024
1 parent ca19d32 commit d4fce39
Show file tree
Hide file tree
Showing 13 changed files with 1,068 additions and 44 deletions.
10 changes: 4 additions & 6 deletions examples/esp8266_webinterface/esp8266_webinterface.ino
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ extern const char main_js[];
IPAddress subnet(255,255,255,0);
#endif

// QUICKFIX...See https://github.com/esp8266/Arduino/issues/263
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

#define LED_PIN 2 // 0 = GPIO0, 2=GPIO2
#define LED_COUNT 24

Expand Down Expand Up @@ -243,7 +239,8 @@ void srv_handle_set() {
if(server.arg(i)[0] == '-') {
ws2812fx.setBrightness(ws2812fx.getBrightness() * 0.8);
} else if(server.arg(i)[0] == ' ') {
ws2812fx.setBrightness(min(max(ws2812fx.getBrightness(), 5) * 1.2, 255));
int newBrightness = constrain(ws2812fx.getBrightness() * 1.2, 6, 255);
ws2812fx.setBrightness(newBrightness);
} else { // set brightness directly
uint8_t tmp = (uint8_t) strtol(server.arg(i).c_str(), NULL, 10);
ws2812fx.setBrightness(tmp);
Expand All @@ -253,7 +250,8 @@ void srv_handle_set() {

if(server.argName(i) == "s") {
if(server.arg(i)[0] == '-') {
ws2812fx.setSpeed(max(ws2812fx.getSpeed(), 5) * 1.2);
int newSpeed = constrain(ws2812fx.getSpeed() * 1.2, 6, 65535);
ws2812fx.setSpeed(newSpeed);
} else if(server.arg(i)[0] == ' ') {
ws2812fx.setSpeed(ws2812fx.getSpeed() * 0.8);
} else {
Expand Down
993 changes: 993 additions & 0 deletions examples/tester_Dec2024.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/ws2812fx_matrix/ws2812fx_matrix.ino
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void loop() {
color_index = 0; // reset the rainbow color index
}
matrix.setTextColor(color_wheel(color_index));
color_index += max(1, 256 / scroll_limit);
color_index += (256 / scroll_limit) > 1 ? (256 / scroll_limit) : 1;
matrix.show();
delay(100);
}
Expand Down
23 changes: 16 additions & 7 deletions examples/ws2812fx_patterns_web/ws2812fx_patterns_web.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@
#define DYNAMIC_JSON_DOCUMENT_SIZE 4096 /* used by AsyncJson. Default 1024 bytes is too small */

#include <WS2812FX.h>
#include <ESPAsyncWebSrv.h>
#include <AsyncJson.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>

#ifdef ESP32
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(TARGET_RP2040)
#include <WebServer.h>
#include <WiFi.h>
#endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>

#define VERSION "2.4.0"

uint8_t dataPin = 14; // default digital pin used to drive the LED strip
Expand Down Expand Up @@ -188,14 +199,14 @@ void configServer() {
// optionally set the running state or run custom auxiliary functions
server.on("/status", [](AsyncWebServerRequest * request) {
if (request->hasParam("running")) {
AsyncWebParameter* p = request->getParam("running");
const AsyncWebParameter* p = request->getParam("running");
const char* running = p->value().c_str();
if (strcmp(running, "true") == 0) ws2812fx.start();
else ws2812fx.stop();
}

if (request->hasParam("auxFunc")) {
AsyncWebParameter* p = request->getParam("auxFunc");
const AsyncWebParameter* p = request->getParam("auxFunc");
int auxFuncIndex = atoi(p->value().c_str());
size_t customAuxFuncSize = sizeof(customAuxFunc) / sizeof(customAuxFunc[0]);
if (auxFuncIndex >= 0 && (size_t)auxFuncIndex < customAuxFuncSize) {
Expand Down Expand Up @@ -301,7 +312,6 @@ int modeName2Index(const char* name) {
return 0;
}

#if ARDUINOJSON_VERSION_MAJOR == 6
//#pragma message("Compiling for ArduinoJson v6")
bool json2patterns(JsonObject deviceJson) {
ws2812fx.setPin(deviceJson["dataPin"]);
Expand Down Expand Up @@ -372,4 +382,3 @@ bool json2patterns(JsonObject deviceJson) {
}
return true;
}
#endif
21 changes: 16 additions & 5 deletions examples/ws2812fx_segments_web/ws2812fx_segments_web.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@
#define DYNAMIC_JSON_DOCUMENT_SIZE 2048 /* used by AsyncJson. Default is 1024, which is a little too small */

#include <WS2812FX.h>
#include <ESPAsyncWebSrv.h> /* https://github.com/me-no-dev/ESPAsyncWebServer */
#include <AsyncJson.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>

#ifdef ESP32
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(TARGET_RP2040)
#include <WebServer.h>
#include <WiFi.h>
#endif
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>

#include "bundle.css.h"
#include "bundle.js.h"
Expand Down Expand Up @@ -262,7 +273,7 @@ void showReqParams(AsyncWebServerRequest * request) {
Serial.println("HTTP request parameters:");
int params = request->params();
for (int i = 0; i < params; i++) {
AsyncWebParameter* p = request->getParam(i);
const AsyncWebParameter* p = request->getParam(i);
if (p->isFile()) { //p->isPost() is also true
Serial.printf("FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
} else if (p->isPost()) {
Expand Down
9 changes: 9 additions & 0 deletions extras/WS2812FX change log.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
WS2182FX Change Log


v1.4.5 changes 12/01/2024
-------------------------

1) Removed use of min and max preprocessor macros, which were causing
compile issues for various board cores (notably ESP32 v2.0.x and
ESP32 v3.0.x). Goodbye, Farewell and Amen!


v1.4.4 changes 06/16/2024
-------------------------

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "Harm Aldick",
"url": "https://github.com/kitesurfer1404/WS2812FX"
},
"version": "1.4.4",
"version": "1.4.5",
"frameworks": "arduino",
"platforms": "*",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=WS2812FX
version=1.4.4
version=1.4.5
author=Harm Aldick
maintainer=Harm Aldick
sentence=WS2812 FX Library for Arduino and ESP microprocessors.
Expand Down
2 changes: 1 addition & 1 deletion src/WS2812FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool WS2812FX::service() {
SET_FRAME;
doShow = true;
uint16_t delay = (MODE_PTR(_seg->mode))();
_seg_rt->next_time = now + max(delay, SPEED_MIN);
_seg_rt->next_time = now + (delay > SPEED_MIN ? delay : SPEED_MIN);
_seg_rt->counter_mode_call++;
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/WS2812FX.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@
#include <Adafruit_NeoPixel.h>
#endif

// include max macro for ESP boards
#ifndef max
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif

#define DEFAULT_BRIGHTNESS (uint8_t)50
#define DEFAULT_BRIGHTNESS (uint8_t)16 /* set default brightness quite low to minimize current draw on startup */
#define DEFAULT_MODE (uint8_t)0
#define DEFAULT_SPEED (uint16_t)1000
#define DEFAULT_COLOR (uint32_t)0xFF0000
Expand Down Expand Up @@ -163,7 +155,7 @@ class WS2812FX : public tinyNeoPixel {
WS2812FX(uint16_t num_leds, uint8_t pin, neoPixelType type, byte* pixelPtr)
: tinyNeoPixel(num_leds, pin, type, pixelPtr) {

brightness = 16; // default the brightness quite low to limit the current draw
brightness = 8; // default the brightness quite low to limit the current draw

/* since an ATtiny is so memory constrained, we only allow one LED segment. Since the
number of segments is fixed, we can initialize the segment variable in the class
Expand Down
3 changes: 2 additions & 1 deletion src/custom/RainbowFireworks.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ uint16_t rainbowFireworks(void) {
}

// randomly create a red pixel
for(uint16_t i=0; i<max(1, seglen/20); i++) {
uint16_t numPixels = seglen/20 > 1 ? seglen/20 : 1;
for(uint16_t i=0; i<numPixels; i++) {
if(ws2812fx.random8(10) == 0) {
uint16_t index = seg->start + 6 + ws2812fx.random16(seglen - 12);
ws2812fx.setPixelColor(index, RED);
Expand Down
6 changes: 4 additions & 2 deletions src/modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ uint16_t WS2812FX::mode_theater_chase_rainbow(void) {
*/
uint16_t WS2812FX::mode_running_lights(void) {
uint8_t size = 1 << SIZE_OPTION;
uint8_t sineIncr = max((uint8_t)1, (256 / _seg_len) * size);
uint8_t sineIncr = (256 / _seg_len) * size;
sineIncr = sineIncr > 1 ? sineIncr : 1;
for(uint16_t i=0; i < _seg_len; i++) {
int lum = (int)sine8(((i + _seg_rt->counter_mode_step) * sineIncr));
uint32_t color = color_blend(_seg->colors[0], _seg->colors[1], lum);
Expand Down Expand Up @@ -865,7 +866,8 @@ uint16_t WS2812FX::mode_rainbow_fireworks(void) {

// occasionally create a random red pixel
if(random8(4) == 0) {
uint16_t index = _seg->start + 6 + random16(max((uint8_t)1, _seg_len - 12));
uint16_t rand16 = random16(_seg_len - 12 > 1 ? _seg_len - 12 : 1);
uint16_t index = _seg->start + 6 + rand16;
setRawPixelColor(index, RED); // set the raw pixel color (ignore global brightness)
SET_CYCLE;
}
Expand Down
27 changes: 18 additions & 9 deletions src/modes_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,17 @@ uint16_t WS2812FX::fireworks(uint32_t color) {

uint8_t size = 2 << SIZE_OPTION;
if(!_triggered) {
for(uint16_t i=0; i<max((uint8_t)1, _seg_len/20); i++) {
uint16_t numBursts = _seg_len/20 > 1 ? _seg_len/20 : 1;
for(uint16_t i=0; i<numBursts; i++) {
if(random8(10) == 0) {
uint16_t index = _seg->start + random16(_seg_len - size + 1);
fill(color, index, size);
SET_CYCLE;
}
}
} else {
for(uint16_t i=0; i<max((uint8_t)1, _seg_len/10); i++) {
uint16_t numBursts = _seg_len/10 > 1 ? _seg_len/10 : 1;
for(uint16_t i=0; i<numBursts; i++) {
uint16_t index = _seg->start + random16(_seg_len - size + 1);
fill(color, index, size);
SET_CYCLE;
Expand All @@ -408,14 +410,21 @@ uint16_t WS2812FX::fireworks(uint32_t color) {
* Fire flicker function
*/
uint16_t WS2812FX::fire_flicker(int rev_intensity) {
byte w = (_seg->colors[0] >> 24) & 0xFF;
byte r = (_seg->colors[0] >> 16) & 0xFF;
byte g = (_seg->colors[0] >> 8) & 0xFF;
byte b = (_seg->colors[0] & 0xFF);
byte lum = max(w, max(r, max(g, b))) / rev_intensity;
uint8_t w = (_seg->colors[0] >> 24) & 0xFF;
uint8_t r = (_seg->colors[0] >> 16) & 0xFF;
uint8_t g = (_seg->colors[0] >> 8) & 0xFF;
uint8_t b = (_seg->colors[0] & 0xFF);
uint8_t maxLum = g > b ? g : b;
maxLum = maxLum > r ? maxLum : r;
maxLum = maxLum > w ? maxLum : w;
uint8_t lum = maxLum / rev_intensity;
for(uint16_t i=_seg->start; i <= _seg->stop; i++) {
int flicker = random8(lum);
setPixelColor(i, max(r - flicker, 0), max(g - flicker, 0), max(b - flicker, 0), max(w - flicker, 0));
uint8_t flicker = random8(lum);
uint8_t r2 = (r - flicker) > 0 ? (r - flicker) : 0;
uint8_t g2 = (g - flicker) > 0 ? (g - flicker) : 0;
uint8_t b2 = (b - flicker) > 0 ? (b - flicker) : 0;
uint8_t w2 = (w - flicker) > 0 ? (w - flicker) : 0;
setPixelColor(i, r2, g2, b2, w2);
}

SET_CYCLE;
Expand Down

0 comments on commit d4fce39

Please sign in to comment.