Skip to content

Commit 316ce87

Browse files
authored
Merge pull request #14 from kienvo/fb
Add dynamic framebuffer
2 parents 590c66e + a69b87e commit 316ce87

File tree

4 files changed

+179
-35
lines changed

4 files changed

+179
-35
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_spi1.c \
5151
CH5xx_ble_firmware_library/RVMSIS/core_riscv.c \
5252
src/main.c \
5353
src/leddrv.c \
54-
src/button.c
54+
src/button.c \
55+
src/fb.c \
5556

5657

5758
# ASM sources

src/fb.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "fb.h"
2+
#include <memory.h>
3+
4+
volatile static fb_t *current, *head, *tail;
5+
6+
static void fb_add(fb_t *new, fb_t *prev, fb_t *next)
7+
{
8+
next->prev = new;
9+
new->next = next;
10+
new->prev = prev;
11+
prev->next = new;
12+
}
13+
14+
fb_t *fblist_insert(fb_t *at, fb_t *new)
15+
{
16+
fb_add(new, at, at->next);
17+
return new;
18+
}
19+
20+
fb_t *fblist_append(fb_t *new)
21+
{
22+
fblist_insert(new, tail);
23+
tail = new;
24+
return new;
25+
}
26+
27+
fb_t *fblist_gonext()
28+
{
29+
current = current->next;
30+
current->scroll = 0;
31+
return current;
32+
}
33+
34+
fb_t *fblist_goprev()
35+
{
36+
current = current->prev;
37+
current->scroll = 0;
38+
return current;
39+
}
40+
41+
fb_t *fblist_gohead()
42+
{
43+
current = head;
44+
current->scroll = 0;
45+
return current;
46+
}
47+
48+
fb_t *fblist_currentfb()
49+
{
50+
return current;
51+
}
52+
53+
static void list_del(fb_t *prev, fb_t *next)
54+
{
55+
prev->next = next;
56+
next->prev = prev;
57+
}
58+
59+
fb_t *fblist_drop(fb_t *fb)
60+
{
61+
list_del(fb->prev, fb->next);
62+
return fb->next;
63+
}
64+
65+
fb_t *fb_new(uint16_t width)
66+
{
67+
fb_t *fb = malloc(sizeof(fb_t));
68+
memset(fb, 0, sizeof(fb_t));
69+
70+
fb->width = width;
71+
fb->buf = malloc(width * sizeof(uint16_t));
72+
memset(fb->buf, 0, width * sizeof(uint16_t));
73+
74+
fb->modes = FIXED;
75+
76+
fb->next = fb;
77+
fb->prev = fb;
78+
79+
return fb;
80+
}
81+
82+
void fblist_init(uint16_t first_fb_width)
83+
{
84+
current = fb_new(first_fb_width);
85+
head = current;
86+
tail = current;
87+
}

src/fb.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifndef __FB_H__
2+
#define __FB_H__
3+
4+
#include <stdint.h>
5+
#include <stdlib.h>
6+
7+
enum ANIMATION_MODES {
8+
LEFT = 0,
9+
RIGHT,
10+
UP,
11+
DOWN,
12+
FIXED,
13+
SNOWFLAKE,
14+
PICTURE,
15+
ANIMATION,
16+
LASER,
17+
};
18+
19+
typedef struct fb_st {
20+
uint16_t *buf;
21+
uint16_t width;
22+
uint8_t modes;
23+
int is_flash;
24+
int is_marquee;
25+
// TODO: feat: Brightness for each fb
26+
int brightness;
27+
// TODO: feat: Timeout for each fb to switch to next fb
28+
uint32_t timeout; // zero mean no timeout
29+
uint16_t scroll;
30+
31+
struct fb_st *next;
32+
struct fb_st *prev;
33+
} fb_t;
34+
35+
fb_t *fb_new(uint16_t width);
36+
static inline void fb_free(fb_t *fb)
37+
{
38+
free((fb)->buf);
39+
free((fb));
40+
}
41+
42+
fb_t *fblist_insert(fb_t *at, fb_t *new);
43+
fb_t *fblist_append(fb_t *new);
44+
fb_t *fblist_drop(fb_t *fb);
45+
46+
fb_t *fblist_gonext();
47+
fb_t *fblist_goprev() ;
48+
fb_t *fblist_gohead();
49+
fb_t *fblist_currentfb();
50+
51+
void fblist_init(uint16_t first_fb_width);
52+
53+
#endif /* __FB_H__ */

src/main.c

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include "leddrv.h"
55
#include "button.h"
6+
#include "fb.h"
67

78
#define FB_WIDTH (LED_COLS * 4)
89
#define SCROLL_IRATIO (16)
9-
#define FB_NUM_SPARE (8)
1010
#define SCAN_F (2000)
1111
#define SCAN_T (FREQ_SYS / SCAN_F)
1212

@@ -23,8 +23,6 @@ enum MODES {
2323
};
2424
#define BRIGHTNESS_LEVELS (4)
2525

26-
uint16_t fb[FB_NUM_SPARE][FB_WIDTH];
27-
2826
uint8_t test_font[][11] = {
2927
0x00, 0xFE, 0x66, 0x62, 0x68, 0x78, 0x68, 0x60, 0x60, 0xF0, 0x00, // F
3028
0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, // O
@@ -49,9 +47,7 @@ void draw2fb(uint16_t *fb, int c, int col)
4947
}
5048
}
5149

52-
volatile int fb_sel, fb_num;
5350
volatile int mode, brightness;
54-
volatile uint64_t tick;
5551

5652
__HIGH_CODE
5753
static void change_brightness()
@@ -67,46 +63,50 @@ static void change_mode()
6763
}
6864

6965
__HIGH_CODE
70-
static void change_fb()
66+
static void fb_transition()
7167
{
72-
NEXT_STATE(fb_sel, 0, fb_num);
68+
fblist_gonext();
69+
}
70+
71+
void draw_testfb()
72+
{
73+
74+
fb_t *curr_fb = fblist_currentfb();
75+
curr_fb->modes = LEFT;
76+
77+
for (int i=0; i<8; i++) {
78+
draw2fb(curr_fb->buf, i, 8 * (i + 4));
79+
}
80+
81+
fb_t *fb_next = fb_new(8*12);
82+
fblist_append(fb_next);
83+
fb_next->modes = LEFT;
84+
85+
for (int i=4; i<12; i++) {
86+
draw2fb(fb_next->buf, i % 8, 8 * (i + 1));
87+
}
7388
}
7489

7590
int main()
7691
{
7792
SetSysClock(CLK_SOURCE_PLL_60MHz);
7893

7994
led_init();
80-
draw2fb(fb[0], 0, 8*(5-1));
81-
draw2fb(fb[0], 1, 8*(6-1));
82-
draw2fb(fb[0], 2, 8*(7-1));
83-
draw2fb(fb[0], 3, 8*(8-1));
84-
draw2fb(fb[0], 4, 8*(9-1));
85-
draw2fb(fb[0], 5, 8*(10-1));
86-
draw2fb(fb[0], 6, 8*(11-1));
87-
draw2fb(fb[0], 7, 8*(12-1));
88-
89-
draw2fb(fb[1], 4, 8*5);
90-
draw2fb(fb[1], 5, 8*6);
91-
draw2fb(fb[1], 6, 8*7);
92-
draw2fb(fb[1], 7, 8*8);
93-
draw2fb(fb[1], 0, 8*9);
94-
draw2fb(fb[1], 1, 8*10);
95-
draw2fb(fb[1], 2, 8*11);
96-
draw2fb(fb[1], 3, 8*12);
97-
fb_num = 2;
98-
9995
TMR0_TimerInit(SCAN_T / 2);
10096
TMR0_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
10197
PFIC_EnableIRQ(TMR0_IRQn);
10298

99+
fblist_init(FB_WIDTH);
100+
101+
draw_testfb();
102+
103103
btn_init();
104104
btn_onOnePress(KEY1, change_mode);
105-
btn_onOnePress(KEY2, change_fb);
105+
btn_onOnePress(KEY2, fb_transition);
106106
btn_onLongPress(KEY1, change_brightness);
107107

108108
while (1) {
109-
int i = 0;
109+
uint32_t i = 0;
110110
while (isPressed(KEY2)) {
111111
i++;
112112
if (i>10) {
@@ -121,16 +121,19 @@ __INTERRUPT
121121
__HIGH_CODE
122122
void TMR0_IRQHandler(void)
123123
{
124-
static int i, scroll;
124+
static int i;
125125

126126
if (TMR0_GetITFlag(TMR0_3_IT_CYC_END)) {
127127

128+
fb_t *fb = fblist_currentfb();
128129
i += 1;
129130
if (i >= LED_COLS) {
130131
i = 0;
131-
scroll++;
132-
if (scroll >= (FB_WIDTH-LED_COLS)*SCROLL_IRATIO) {
133-
scroll = 0;
132+
if ((fb->modes & 0x0f) == LEFT) {
133+
fb->scroll++;
134+
if (fb->scroll >= (fb->width-LED_COLS)*SCROLL_IRATIO) {
135+
fb->scroll = 0;
136+
}
134137
}
135138
}
136139

@@ -139,8 +142,8 @@ void TMR0_IRQHandler(void)
139142
leds_releaseall();
140143
} else {
141144
led_write2dcol(i/2,
142-
fb[fb_sel][i+scroll/SCROLL_IRATIO],
143-
fb[fb_sel][i+scroll/SCROLL_IRATIO+1]);
145+
fb->buf[i+ fb->scroll/SCROLL_IRATIO],
146+
fb->buf[i+ fb->scroll/SCROLL_IRATIO + 1]);
144147
}
145148

146149
TMR0_ClearITFlag(TMR0_3_IT_CYC_END);

0 commit comments

Comments
 (0)