forked from nullbitsco/tidbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14seg_animation.h
118 lines (96 loc) · 3.11 KB
/
14seg_animation.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include "HT16K33_GFX.h"
#include <string.h>
#define DISP_MODE_SCROLL 0
#define DISP_MODE_BOUNCE 1
#define DISP_MODE_PUSH 2
#define BLANK " "
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
typedef struct animation_t {
uint8_t mode : 7;
uint8_t enabled: 1;
int8_t pos : 8;
int8_t dir : 3;
uint16_t speed : 13;
uint16_t delay_timer : 16;
HT16K33 *disp;
char *message;
} animation_t;
animation_t *newAnimation (HT16K33 *disp) {
animation_t *animation = malloc(sizeof(animation_t));
if (animation == NULL)
return NULL;
animation->mode = DISP_MODE_SCROLL;
animation->enabled = false;
animation->pos = 0;
animation->dir = 1;
animation->speed = 300;
animation->delay_timer = timer_read();
animation->disp = disp;
animation->message = "";
return animation;
}
__attribute__((weak)) void animation_done_cbk(animation_t *animation) {
}
void animation_scroll(animation_t *animation) {
uint8_t target = min(animation->disp->size, strlen(animation->message));
for (uint8_t n=0; n<target; n++) {
HT16K33_dispASCII(animation->disp, n, animation->message[animation->pos+n]);
}
if ((animation->enabled) && (strlen(animation->message) > animation->disp->size)) {
if (++animation->pos > strlen(animation->message) - animation->disp->size) {
animation->pos = 0;
animation_done_cbk(animation);
}
}
HT16K33_refresh(animation->disp);
animation->delay_timer = timer_read();
}
void animation_bounce(animation_t *animation) {
uint8_t target = min(animation->disp->size, strlen(animation->message));
for (uint8_t n=0; n<target; n++) {
HT16K33_dispASCII(animation->disp, n, animation->message[animation->pos+n]);
}
if ((animation->enabled) && (strlen(animation->message) > animation->disp->size)) {
animation->pos += animation->dir;
if (animation->pos < 0) {
animation->dir = 1;
animation->pos += animation->dir;
animation_done_cbk(animation);
}
else if (animation->pos > strlen(animation->message) - animation->disp->size) {
animation->dir = -1;
animation->pos += animation->dir;
}
}
HT16K33_refresh(animation->disp);
animation->delay_timer = timer_read();
}
void _refresh(animation_t *animation, bool force) {
if ((timer_elapsed(animation->delay_timer) > animation->speed) || force) {
if (strlen(animation->message) > INT8_MAX) {
// dprintf("Message too long! Max: %d\n", INT8_MAX);
return;
}
switch (animation->mode) {
case DISP_MODE_SCROLL:
animation_scroll(animation);
break;
case DISP_MODE_BOUNCE:
animation_bounce(animation);
break;
default:
break;
}
}
}
void animation_refresh(animation_t *animation) {
_refresh(animation, false);
}
void force_animation_refresh(animation_t *animation) {
animation->pos = 0;
animation->dir = 1;
_refresh(animation, true);
}