C Pointer/reference to array of structures from within function -
C Pointer/reference to array of structures from within function -
i got problem making custom function on top of fastled library arduino.
the array of leds, structures called crgb needs altered within function drawgradient
set colors of leds.
there must wrong pointers, references, parameters of functions can't seem figure out how right. how utilize pointers/references piece of code?
lines of interest
crgb leds[num_leds]; void loop() { drawgradient(&leds, 4, 0, crgb::white, crgb::lime); } void drawgradient(struct crgb leds[], unsigned int from, unsigned int to, struct crgb fromcolor, struct crgb tocolor) { leds[j] = pickfromgradient(fromcolor, fromcolor, position); }
full code
#include "fastled.h" #define num_leds 18 #define data_pin 7 crgb leds[num_leds]; void setup() { fastled.addleds<ws2812b, data_pin, grb>(leds, num_leds); } void loop() { leds[4] = crgb::red; fastled.show(); delay(1000); drawgradient(&leds, 4, 0, crgb::white, crgb::lime); drawgradient(&leds, 4, 8, crgb::white, crgb::lime); drawgradient(&leds, 13, 9, crgb::white, crgb::lime); drawgradient(&leds, 13, 17, crgb::white, crgb::lime); fastled.show(); while(1); // save energy :-) } void drawgradient(struct crgb leds[], unsigned int from, unsigned int to, struct crgb fromcolor, struct crgb tocolor) { unsigned int barlength = abs(to - from) + 1; int rangeperpixel = 255/barlength; for(int = 0; <= barlength; i++) { int j = 0; if(from < to) { j = + i; } else { j = max(from, to) - i; } float position = / barlength; leds[j] = pickfromgradient(fromcolor, tocolor, position); } } struct crgb pickfromgradient(struct crgb fromcolor, struct crgb tocolor, float position) { struct crgb newcolor; uint8_t r = fromcolor.r + position * (tocolor.r - fromcolor.r); uint8_t g = fromcolor.g + position * (tocolor.g - fromcolor.g); uint8_t b = fromcolor.b + position * (tocolor.b - fromcolor.b); newcolor.setrgb(r, g, b); homecoming newcolor; }
change function signature:
void drawgradient(struct crgb leds[], unsigned int from, unsigned int to, struct crgb fromcolor, struct crgb tocolor)
to
void drawgradient(struct crgb *leds, unsigned int from, unsigned int to, struct crgb fromcolor, struct crgb tocolor)
and phone call function drawgradient in next way:
drawgradient(leds, 4, 0, crgb::white, crgb::lime);
leds pointer of array of crgb structure. &leds refers address of pointer.
c arrays function arduino
Comments
Post a Comment