今日の嫁
— いぬにゃん (@inunyann) June 18, 2024
昨日は32bitマイコン使っていたのですが、今日は8bit(ch552)で試しました! SPIは6MHzですが、そこそこ高速に描画できるなと思いました 最初の画像(かわいい嫁)表示はちょっと時間かかりますね pic.twitter.com/eiTAb6Mfcl
#include "WS2812.h"
#define ENABLE_Original_ColorHSV_Func 0
void neopixel_begin(uint8_t numLed, __xdata uint8_t *ledData);
void neopixel_begin_wpin(uint8_t numLed, __xdata uint8_t *ledData, uint8_t pin);
//void neopixel_clear();
void neopixel_setBrightness(uint8_t brightness);
//uint8_t neopixel_gamma8(uint8_t x);
__data uint32_t neoPixel_gamma32(__data uint32_t c);
//uint32_t neopixel_color(uint8_t r, uint8_t g, uint8_t b);
//void neoPixel_setPixelColor(uint8_t n, uint8_t r, uint8_t g, uint8_t b);
void neoPixel_setPixelColor32(__data uint8_t n, __data uint32_t c);
//uint32_t neoPixel_getPixelColor(uint8_t n);
void neopixel_fill(__data uint32_t c);
#if ENABLE_Original_ColorHSV_Func == 1
uint32_t neopixel_ColorHSV(uint16_t hue, uint8_t sat, uint8_t val);
#endif
__data uint32_t neopixel_ColorHSV_fast(__data uint16_t hue, __data uint8_t sat);
//void neopixel_rainbow(uint16_t first_hue);
void neopixel_rainbow_wsat(uint16_t first_hue, __data uint8_t saturation);
__xdata uint8_t np_numLed = 0;
__xdata uint8_t *np_ledData;
__data uint8_t np_brightness = 0x3f;
__code const uint8_t _NeoPixelGammaTable[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8,
8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23,
24, 24, 25, 25, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 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, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 88,
89, 90, 92, 93, 94, 96, 97, 99, 100, 102, 103, 105, 106, 108, 109, 111, 112, 114, 115, 117, 119, 120, 122, 124, 125, 127, 129, 130, 132, 134, 136, 137, 139, 141,
143, 145, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 191, 193, 195, 197, 199, 202, 204, 206, 209, 211,
213, 215, 218, 220, 223, 225, 227, 230, 232, 235, 237, 240, 242, 245, 247, 250, 252, 255 };
#define neopixel_clear() memset(np_ledData, 0, 3 * np_numLed)
void neopixel_begin(uint8_t numLed, __xdata uint8_t *ledData) {
np_numLed = numLed;
np_ledData = ledData;
neopixel_clear();
}
void neopixel_begin_wpin(uint8_t numLed, __xdata uint8_t *ledData, uint8_t pin) {
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT);
neopixel_begin(numLed, ledData);
}
void neopixel_setBrightness(uint8_t brightness) {
if (np_brightness != brightness) {
uint16_t scale = ((uint16_t)brightness << 8) / np_brightness;
for (uint8_t i = 0; i < 3 * np_numLed; i++) np_ledData[i] = scale * np_ledData[i] >> 8;
}
np_brightness = brightness;
}
#define neopixel_gamma8(x) _NeoPixelGammaTable[(__data uint8_t)(x)]
inline __data uint32_t neoPixel_gamma32(__data uint32_t c) {
__data uint8_t *y = (__data uint8_t *)&c;
for (__data uint8_t i = 0; i < 3; i++) y[i] = neopixel_gamma8(y[i]);
return c;
}
#define neopixel_color(r, g, b) (((__data uint32_t)((__data uint8_t)(r)) << 16) | ((__data uint16_t)((__data uint8_t)(g)) << 8) | (__data uint8_t)(b))
#define neoPixel_setPixelColor(n, r, g, b) set_pixel_for_GRB_LED(np_ledData, n, (__data uint16_t)((__data uint8_t)(r)) * np_brightness >> 8, \
(__data uint16_t)((__data uint8_t)(g)) * np_brightness >> 8, \
(__data uint16_t)((__data uint8_t)(b)) * np_brightness >> 8)
inline void neoPixel_setPixelColor32(__data uint8_t n, __data uint32_t c) {
__data uint8_t *y = (__data uint8_t *)&c;
neoPixel_setPixelColor(n, y[2], y[1], y[0]);
}
#define neoPixel_getPixelColor(n) ((__data uint32_t)((__data uint16_t)np_ledData[3 * (n) + 1] << 8) / np_brightness << 16 | \
((__data uint16_t)np_ledData[3 * (n) ] << 8) / np_brightness << 8 | \
((__data uint16_t)np_ledData[3 * (n) + 2] << 8) / np_brightness)
inline void neopixel_fill(__data uint32_t c) { for (__data uint8_t i = 0; i < np_numLed; i++) neoPixel_setPixelColor32(i, c); }
#if ENABLE_Original_ColorHSV_Func == 1
uint32_t neopixel_ColorHSV(uint16_t hue, uint8_t sat, uint8_t val) {
uint8_t r, g, b;
hue = ((uint32_t)hue * 1530L + 32768) / 65536;
if (hue < 510) {
b = 0;
if (hue < 255) { r = 255; g = hue; }
else { r = 510 - hue; g = 255; }
} else if (hue < 1020) {
r = 0;
if (hue < 765) { g = 255; b = hue - 510; }
else { g = 1020 - hue; b = 255; }
} else if (hue < 1530) {
g = 0;
if (hue < 1275) { r = hue - 1020; b = 255; }
else { r = 255; b = 1530 - hue; }
} else { r = 255; g = b = 0; }
uint16_t v1 = (uint16_t)val + 1;
uint16_t s1 = (uint16_t)sat + 1;
uint8_t s2 = 255 - sat;
return ((uint32_t)(((uint16_t)r * s1 >> 8) + s2) * v1 & 0xff00) << 8 |
(uint32_t)(((uint16_t)g * s1 >> 8) + s2) * v1 & 0xff00 |
((uint32_t)(((uint16_t)b * s1 >> 8) + s2) * v1 & 0xff00) >> 8;
}
#endif
__data uint32_t neopixel_ColorHSV_fast(__data uint16_t hue, __data uint8_t sat) {
__data uint8_t r, g, b, hue2 = hue + 128 >> 8;
if (hue2 < 85) {
b = 0;
if (hue2 < 43) { r = 255; g = hue2 * 6; }
else { r = 255 - (hue2 - 42) * 6; g = 255; }
} else if (hue2 < 170) {
r = 0;
if (hue2 < 128) { g = 255; b = (hue2 - 85) * 6; }
else { g = 255 - (hue2 - 127) * 6; b = 255; }
} else if (hue2 < 255) {
g = 0;
if (hue2 < 213) { r = (hue2 - 170) * 6; b = 255; }
else { r = 255; b = 255 - (hue2 - 212) * 6; }
} else { r = 255; g = b = 0; }
if (sat == 255) return (__data uint32_t)r << 16 | (__data uint16_t)g << 8 | b;
__data uint16_t s1 = (__data uint16_t)sat + 1;
__data uint8_t s2 = 255 - sat;
return (__data uint32_t)(((__data uint16_t)r * s1 >> 8) + s2) << 16 |
((__data uint16_t)g * s1 >> 8) + s2 << 8 |
((__data uint16_t)b * s1 >> 8) + s2;
}
inline void neopixel_rainbow_wsat(uint16_t first_hue, __data uint8_t saturation) {
for (__data uint8_t i = 0; i < np_numLed; i++) neoPixel_setPixelColor32(i, neoPixel_gamma32(neopixel_ColorHSV_fast(((__data uint32_t)i << 16) / np_numLed + first_hue, saturation)));
}
#define neopixel_rainbow(first_hue) neopixel_rainbow_wsat((first_hue), 255)
●spiカラー液晶表示ライブラリ(共通コードと、サンプルとしてST7735S用のもの CSピンの操作はバイトごとに必須ではないと思うので、まとめれば高速化できると思います)
#include "lcd_font.h"
void lcd_ClearScreen(uint16_t color);
void lcd_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
void lcd_PushColor(uint16_t color);
#define lcd_PushColorF(color) do { uint16_t c = (color); spiDcHigh(); spiCsLow(); SPI0_DATA = (uint8_t)(c >> 8); while(S0_FREE == 0); SPI0_DATA = (uint8_t)c; while(S0_FREE == 0); spiCsHigh(); } while (0)
void lcd_DrawStringB(uint8_t x, uint8_t y, const char *str, uint16_t color, uint8_t size);
void lcd_DrawString( uint8_t x, uint8_t y, const char *str, uint16_t color, uint16_t bg, uint8_t size);
void lcd_DrawCharB(uint8_t x, uint8_t y, char c, uint16_t color, uint8_t size);
void lcd_DrawChar( uint8_t x, uint8_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
void lcd_DrawPixel(uint8_t x, uint8_t y, uint16_t color);
void lcd_DrawVLine(uint8_t x, uint8_t y, uint8_t h, uint16_t color);
void lcd_DrawHLine(uint8_t x, uint8_t y, uint8_t w, uint16_t color);
#define spiWrCmdF(cmd) do { spiDcLow(); spiCsLow(); SPI0_DATA = (cmd); while(S0_FREE == 0); spiCsHigh(); } while (0)
#define spiWrDataF(data) do { spiDcHigh(); spiCsLow(); SPI0_DATA = (data); while(S0_FREE == 0); spiCsHigh(); } while (0)
void spiWrCmd(uint8_t cmd) {
spiDcLow();
spiCsLow();
SPI0_DATA = cmd;
while(S0_FREE == 0);
spiCsHigh();
}
void spiWrData(uint8_t data) {
spiDcHigh();
spiCsLow();
SPI0_DATA = data;
while(S0_FREE == 0);
spiCsHigh();
}
void lcd_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
spiWrCmdF(0x2a); // CMD_CASET Column addr set
spiDcHigh();
spiCsLow();
SPI0_DATA = 0x00;
while(S0_FREE == 0);
SPI0_DATA = LCD_OFFSET_X + x0;
while(S0_FREE == 0);
SPI0_DATA = 0x00;
while(S0_FREE == 0);
SPI0_DATA = LCD_OFFSET_X + x1;
while(S0_FREE == 0);
spiCsHigh();
spiWrCmdF(0x2b); // CMD_RASET Row addr set
spiDcHigh();
spiCsLow();
SPI0_DATA = 0x00;
while(S0_FREE == 0);
SPI0_DATA = LCD_OFFSET_Y + y0;
while(S0_FREE == 0);
SPI0_DATA = 0x00;
while(S0_FREE == 0);
SPI0_DATA = LCD_OFFSET_Y + y1;
while(S0_FREE == 0);
spiCsHigh();
spiWrCmdF(0x2c); // CMD_RAMWR Write to RAM
}
void lcd_PushColor(uint16_t color) {
spiDcHigh();
spiCsLow();
SPI0_DATA = (uint8_t)(color >> 8);
while(S0_FREE == 0);
SPI0_DATA = (uint8_t)color;
while(S0_FREE == 0);
spiCsHigh();
}
void lcd_DrawPixel(uint8_t x, uint8_t y, uint16_t color) {
lcd_SetAddressWindow(x, y, x, y);
lcd_PushColor(color);
}
void lcd_DrawCharB(uint8_t x, uint8_t y, char c, uint16_t color, uint8_t size) {
for (uint8_t py = 0; py < 8; py++) {
for (uint8_t px = 0; px < 5; px++) {
uint8_t fontBit = font[(uint16_t)c * 5 + px] >> py & 0x01;
if (fontBit) {
lcd_SetAddressWindow(x + px * size, y + 1 - (8 - py) * size, x + (px + 1) * size - 1, y - (7 - py) * size);
for (uint8_t i = 0; i < size * size; i++) lcd_PushColorF(color);
}
}
}
}
void lcd_DrawChar(uint8_t x, uint8_t y, char c, uint16_t color, uint16_t bg, uint8_t size) {
lcd_SetAddressWindow(x, y + 1 - 8 * size, x + 6 * size - 1, y);
for (uint8_t py = 0; py < 8; py++) {
for (uint8_t sy = 0; sy < size; sy++) {
for (uint8_t px = 0; px < 6; px++) {
uint8_t fontBit = px < 5 ? font[(uint16_t)c * 5 + px] >> py & 0x01 : 0x00;
for (uint8_t sx = 0; sx < size; sx++) {
if (fontBit) lcd_PushColorF(color);
else lcd_PushColorF(bg);
}
}
}
}
}
void lcd_DrawStringB(uint8_t x, uint8_t y, const char *str, uint16_t color, uint8_t size) {
while (*str) {
lcd_DrawCharB(x, y, *str, color, size);
x += 6 * size;
str++;
}
}
void lcd_DrawString(uint8_t x, uint8_t y, const char *str, uint16_t color, uint16_t bg, uint8_t size) {
while (*str) {
lcd_DrawChar(x, y, *str, color, bg, size);
x += 6 * size;
str++;
}
}
void lcd_ClearScreen(uint16_t color) {
lcd_SetAddressWindow(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1);
spiDcHigh();
spiCsLow();
for (uint16_t i = 0; i < LCD_WIDTH * LCD_HEIGHT; i++) {
while(S0_FREE == 0);
SPI0_DATA = (uint8_t)((color) >> 8);
while(S0_FREE == 0);
SPI0_DATA = (uint8_t)(color);
}
while(S0_FREE == 0);
spiCsHigh();
}
void lcd_DrawVLine(uint8_t x, uint8_t y, uint8_t h, uint16_t color) {
lcd_SetAddressWindow(x, y, x, y + h - 1);
for (uint8_t i = 0; i < h; i++) lcd_PushColorF(color);
}
void lcd_DrawHLine(uint8_t x, uint8_t y, uint8_t w, uint16_t color) {
lcd_SetAddressWindow(x, y, x + w - 1, y);
for (uint8_t i = 0; i < w; i++) lcd_PushColorF(color);
}
#define LCD_WIDTH 160
#define LCD_HEIGHT 80
#define LCD_OFFSET_X 1
#define LCD_OFFSET_Y 26
#define spiCsHigh() (P1_4 = HIGH)
#define spiCsLow() (P1_4 = LOW)
#define spiDcHigh() (P1_6 = HIGH)
#define spiDcLow() (P1_6 = LOW)
#define spiRstHigh() (P1_1 = HIGH)
#define spiRstLow() (P1_1 = LOW)
#include "lcd_common.h"
void lcd_Init() {
spiCsHigh();
spiDcHigh();
spiRstHigh();
delay(10);
spiRstLow();
delay(10);
spiRstHigh();
delay(150);
spiWrCmd(0x01); // CMD_SWRESET
delay(150);
spiWrCmd(0x11); // CMD_SLPOUT
delay(500);
spiWrCmd(0xb1); // CMD_FRMCTR1
spiWrData(0x01); spiWrData(0x2c); spiWrData(0x2d);
spiWrCmd(0xb2); // CMD_FRMCTR2
spiWrData(0x01); spiWrData(0x2c); spiWrData(0x2d);
spiWrCmd(0xb3); // CMD_FRMCTR3
spiWrData(0x01); spiWrData(0x2c); spiWrData(0x2d); spiWrData(0x01); spiWrData(0x2c); spiWrData(0x2d);
spiWrCmd(0xb4); // CMD_INVCTR
spiWrData(0x07);
spiWrCmd(0xc0); // CMD_PWCTR1
spiWrData(0xa2); spiWrData(0x02); spiWrData(0x84);
spiWrCmd(0xc1); // CMD_PWCTR2
spiWrData(0xc5);
spiWrCmd(0xc2); // CMD_PWCTR3
spiWrData(0x0a); spiWrData(0x00);
spiWrCmd(0xc3); // CMD_PWCTR4
spiWrData(0x8a); spiWrData(0x2a);
spiWrCmd(0xc4); // CMD_PWCTR5
spiWrData(0x8a); spiWrData(0xee);
spiWrCmd(0xc5); // CMD_VMCTR1
spiWrData(0x0e);
spiWrCmd(0xe0); // CMD_GMCTRP1
spiWrData(0x02); spiWrData(0x1c); spiWrData(0x07); spiWrData(0x12); spiWrData(0x37); spiWrData(0x32); spiWrData(0x29); spiWrData(0x2d);
spiWrData(0x29); spiWrData(0x25); spiWrData(0x2b); spiWrData(0x39); spiWrData(0x00); spiWrData(0x01); spiWrData(0x03); spiWrData(0x10);
spiWrCmd(0xe1); // CMD_GMCTRN1
spiWrData(0x03); spiWrData(0x1d); spiWrData(0x07); spiWrData(0x06); spiWrData(0x2e); spiWrData(0x2c); spiWrData(0x29); spiWrData(0x2d);
spiWrData(0x2e); spiWrData(0x2e); spiWrData(0x37); spiWrData(0x3f); spiWrData(0x00); spiWrData(0x00); spiWrData(0x02); spiWrData(0x10);
spiWrCmd(0x21); // CMD_INVON
spiWrCmd(0x3a); // CMD_COLMOD
spiWrData(0x05); // 16bit mode
spiWrCmd(0x36); // CMD_MADCTL
spiWrData(0x20 | 0x40 | 0x04 | 0x08); // BGR mode, rotation:1 (MAD_MV|MAD_MX|MAD_MH)
lcd_ClearScreen(0x0000);
spiWrCmd(0x13); // CMD_NORON
delay(10);
spiWrCmd(0x29); // CMD_DISPON
delay(100);
}
void spiInit() { // SPI1 P1.5(MOSI) P1.7(SCK) P1.4(CS) P1.6(DC) P1.1(RST)
P1_MOD_OC &= 0b00001101;
P1_DIR_PU |= 0b11110010;
SPI0_SETUP = 0;
SPI0_CK_SE = 0x04; // 24MHz / 0x04(SPI0_CK_SE) = 6 MHz
SPI0_CTRL = bS0_MOSI_OE | bS0_SCK_OE;
}
void spiStop() {
SPI0_CTRL = 0;
P1_DIR_PU &= 0b00001101;
}