-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalking machine.cpp
88 lines (84 loc) · 2.82 KB
/
Walking machine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//Walking machine
//St3p40 aka Stepko
//11.04.23
//Not for small matrices
//Second name is "dreams in night"
void drawPixelXYF(float x, float y,
const CRGB & color) {
// extract the fractional parts and derive their inverses
uint8_t xx = (x - (int) x) * 255, yy = (y - (int) y) * 255, ix = 255 - xx, iy = 255 - yy;
// calculate the intensities for each affected pixel
#define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
uint8_t wu[4] = {
WU_WEIGHT(ix, iy),
WU_WEIGHT(xx, iy),
WU_WEIGHT(ix, yy),
WU_WEIGHT(xx, yy)
};
// multiply the intensities by the colour, and saturating-add them to the pixels
for (uint8_t i = 0; i < 4; i++) {
int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);
CRGB clr = leds[XY(xn, yn)];
clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);
clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);
clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);
leds[XY(xn, yn)] = clr;
}
#undef WU_WEIGHT
}
void drawCircleF(float cx, float cy, float radius, CRGB col) {
uint8_t rad = radius;
for (int8_t y = -radius; y < radius; y += 1) {
for (int8_t x = -radius; x < radius; x += 1) {
if (x * x + y * y < radius * radius)
drawPixelXYF(cx + x, cy + y, col);
}
}
}
static float fmap(const float x,
const float in_min,
const float in_max,
const float out_min,
const float out_max) {
return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
}
void drawLineF(float x1, float y1, float x2, float y2,
const CRGB & col1,
const CRGB & col2) {
float deltaX = fabs(x2 - x1);
float deltaY = fabs(y2 - y1);
float steps = 255 / max(deltaX, deltaY);
float error = deltaX - deltaY;
CRGB col = col1;
float signX = x1 < x2 ? 0.5 : -0.5;
float signY = y1 < y2 ? 0.5 : -0.5;
while (x1 != x2 || y1 != y2) {
if ((signX > 0. && x1 > x2 + signX) || (signX < 0. && x1 < x2 + signX)) break;
if ((signY > 0. && y1 > y2 + signY) || (signY < 0. && y1 < y2 + signY)) break;
drawPixelXYF(x1/*+random(-10,10)*0.1 */, y1/*+random(-10,10)*0.1*/, nblend(col, col2, steps++));
float error2 = error;
if (error2 > -deltaY) {
error -= deltaY;
x1 += signX;
}
if (error2 < deltaX) {
error += deltaX;
y1 += signY;
}
}
}
struct{
float posX;
float posY;
} dot[7];
void draw() {
FastLED.clear();
for (byte i = 0; i < 7; i++) {
dot[i].posX = (beatsin16(4, (LED_COLS >> 3) << 8, (LED_COLS - (LED_COLS >> 3) - 1) << 8, i*8192, i*8192)) / 255.f;
dot[i].posY = (beatsin16(4, (LED_ROWS >> 3) << 8, (LED_ROWS - (LED_ROWS >> 3) - 1) << 8, i*4096, 16384+i*8192)) / 255.f;
}
for(uint8_t i = 0; i < 7; i++){
drawCircleF(dot[i].posX, dot[i].posY, 4, CHSV(i*32,255,255));
drawLineF(dot[i].posX, dot[i].posY, dot[(i+1)%7].posX, dot[(i+1)%7].posY, CHSV(i*32,255,255), CHSV(((i+1)%7)*32,255,255));
}
}