avr/animation/animation.c

100 lines
2.0 KiB
C

#include "animation.h"
#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;
}
}
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 };
}
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) {
self->y -= self->inc;
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);
}
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) {
error = m - dt;
} else {
error = m + dt;
}
return (time_line_t){ .y = y0, .m = m, .error = error, .inc = inc, .dt = dt };
}
*/
/*
int time_line_next(time_line_t *slope) {
slope->error += slope->m;
if (slope->m > 0) {
if (slope->error >= 0) {
slope->y += slope->inc;
slope->error -= 2 * slope->dt;
}
} else {
if (slope->error <= 0) {
slope->y += slope->inc;
slope->error += 2 * slope->dt;
}
}
return slope->y;
}
*/
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);
}