led: add basic Charlieplexing scan
This commit is contained in:
86
src/main.c
86
src/main.c
@@ -1,30 +1,88 @@
|
||||
#include "CH58x_common.h"
|
||||
#include "CH58x_sys.h"
|
||||
#include "leddrv.h"
|
||||
|
||||
void led_init(void)
|
||||
#define FB_WIDTH LED_COLS*4
|
||||
#define SCROLL_IRATIO 3
|
||||
|
||||
uint16_t fb[2][FB_WIDTH];
|
||||
|
||||
uint8_t test_font[][11] = {
|
||||
0x00, 0xFE, 0x66, 0x62, 0x68, 0x78, 0x68, 0x60, 0x60, 0xF0, 0x00, // F
|
||||
0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, // O
|
||||
0x00, 0x7C, 0xC6, 0xC6, 0x60, 0x38, 0x0C, 0xC6, 0xC6, 0x7C, 0x00, // S
|
||||
0x00, 0x7C, 0xC6, 0xC6, 0x60, 0x38, 0x0C, 0xC6, 0xC6, 0x7C, 0x00, // S
|
||||
0x00, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, // A
|
||||
0x00, 0x7C, 0xC6, 0xC6, 0x60, 0x38, 0x0C, 0xC6, 0xC6, 0x7C, 0x00, // S
|
||||
0x00, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, // I
|
||||
0x00, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, // A
|
||||
};
|
||||
|
||||
void draw2fb(uint16_t *fb, int c, int col)
|
||||
{
|
||||
GPIOA_SetBits(GPIO_Pin_10);
|
||||
GPIOA_ResetBits(GPIO_Pin_12);
|
||||
GPIOA_ModeCfg(GPIO_Pin_10, GPIO_ModeOut_PP_5mA);
|
||||
GPIOA_ModeCfg(GPIO_Pin_12, GPIO_ModeOut_PP_5mA);
|
||||
uint16_t tmpfb[8] = {0};
|
||||
for (int i=0; i<8; i++) {
|
||||
for (int j=0; j<11; j++) {
|
||||
tmpfb[i] |= ((test_font[c][j] >> (8-i)) & 0x01) << j;
|
||||
}
|
||||
}
|
||||
for (int i=0; i<8; i++) {
|
||||
fb[col+i] = tmpfb[i];
|
||||
}
|
||||
}
|
||||
|
||||
void led_toggle(void)
|
||||
{
|
||||
GPIOA_InverseBits(GPIO_Pin_10);
|
||||
GPIOA_InverseBits(GPIO_Pin_12);
|
||||
}
|
||||
volatile int fb_sel = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
SetSysClock(CLK_SOURCE_PLL_60MHz);
|
||||
|
||||
led_init();
|
||||
draw2fb(fb[0], 0, 8*5);
|
||||
draw2fb(fb[0], 1, 8*6);
|
||||
draw2fb(fb[0], 2, 8*7);
|
||||
draw2fb(fb[0], 3, 8*8);
|
||||
draw2fb(fb[0], 4, 8*9);
|
||||
draw2fb(fb[0], 5, 8*10);
|
||||
draw2fb(fb[0], 6, 8*11);
|
||||
draw2fb(fb[0], 7, 8*12);
|
||||
|
||||
while(1)
|
||||
{
|
||||
mDelaymS(100);
|
||||
led_toggle();
|
||||
draw2fb(fb[1], 4, 8*5);
|
||||
draw2fb(fb[1], 5, 8*6);
|
||||
draw2fb(fb[1], 6, 8*7);
|
||||
draw2fb(fb[1], 7, 8*8);
|
||||
draw2fb(fb[1], 0, 8*9);
|
||||
draw2fb(fb[1], 1, 8*10);
|
||||
draw2fb(fb[1], 2, 8*11);
|
||||
draw2fb(fb[1], 3, 8*12);
|
||||
|
||||
TMR0_TimerInit(1500);
|
||||
TMR0_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
|
||||
PFIC_EnableIRQ(TMR0_IRQn);
|
||||
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((interrupt))
|
||||
void TMR0_IRQHandler(void)
|
||||
{
|
||||
static int i, scroll;
|
||||
|
||||
if (TMR0_GetITFlag(TMR0_3_IT_CYC_END)) {
|
||||
|
||||
i += 2;
|
||||
if (i >= LED_COLS) {
|
||||
i = 0;
|
||||
scroll++;
|
||||
if (scroll >= (FB_WIDTH-LED_COLS)*SCROLL_IRATIO) {
|
||||
scroll = 0;
|
||||
fb_sel = fb_sel == 0;
|
||||
}
|
||||
}
|
||||
// This is a mess
|
||||
led_write2dcol(i/2, fb[fb_sel][i+scroll/SCROLL_IRATIO], fb[fb_sel][i+scroll/SCROLL_IRATIO+1]);
|
||||
|
||||
TMR0_ClearITFlag(TMR0_3_IT_CYC_END);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user