avr/animation/animation.c

100 lines
2.0 KiB
C
Raw Normal View History

#include "animation.h"
2022-07-18 03:01:27 +00:00
#include <stdio.h>
int bound(int value, uint8_t min, uint8_t max) {
if (value > max) {
return max;
} else if (value < min) {
return min;
} else {
return value;
}
}
2022-07-18 03:01:27 +00:00
time_line_t time_line_new(int t0, int y0, int t1, int y1) {
int dt = t1 - t0;
int dy = y1 - y0;
int de = 2 * dy;
int inc = dy > 0 ? 1 : dy == 0 ? 0 : -1;
int error;
if (de > 0) {
error = de - dt;
} else {
error = de + dt;
}
return (time_line_t){ .y = y0, .dt = dt, .de = de, .error = error, .inc = inc, .dy = dy, .y0 = y0, .y1 = y1 };
2022-07-18 03:01:27 +00:00
}
int time_line_next(time_line_t *self) {
int error = self->error + self->de;
if (self->de > 0) {
while (error >= 0) {
self->y += self->inc;
error -= 2 * self->dt;
}
} else {
while (error < 0) {
2022-07-18 04:39:38 +00:00
self->y += self->inc;
2022-07-18 03:01:27 +00:00
error += 2 * self->dt;
}
}
if (self->y0 < self->y1) {
self->y = bound(self->y, self->y0, self->y1);
} else {
self->y = bound(self->y, self->y1, self->y0);
}
2022-07-18 03:01:27 +00:00
self->error = error;
return self->y;
}
/*
time_line_t time_line_new(int t0, int y0, int t1, int y1) {
int dt = t1 - t0;
int dy = y1 - y0;
int inc = dy > 0 ? 1 : dy == 0 ? 0 : -1;
int m = 2 * dy;
int error = 0;
if (m > 0) {
2022-07-18 03:01:27 +00:00
error = m - dt;
} else {
2022-07-18 03:01:27 +00:00
error = m + dt;
}
2022-07-18 03:01:27 +00:00
return (time_line_t){ .y = y0, .m = m, .error = error, .inc = inc, .dt = dt };
}
2022-07-18 03:01:27 +00:00
*/
2022-07-18 03:01:27 +00:00
/*
int time_line_next(time_line_t *slope) {
slope->error += slope->m;
if (slope->m > 0) {
if (slope->error >= 0) {
slope->y += slope->inc;
2022-07-18 03:01:27 +00:00
slope->error -= 2 * slope->dt;
}
} else {
if (slope->error <= 0) {
slope->y += slope->inc;
2022-07-18 03:01:27 +00:00
slope->error += 2 * slope->dt;
}
}
return slope->y;
}
2022-07-18 03:01:27 +00:00
*/
2022-07-18 03:01:27 +00:00
void time_line_print(time_line_t *self) {
printf("y: %d\tde: %d\terror: %d\tinc: %d\t:dt: %d\n", self->y, self->de, self->error, self->inc, self->dt);
}