/*
ShiftRegister(74HC595)経由で8x8 Dot Matrix
LEDに日本語を表示する
*/
#include <FS.h>
#include <Fontx.h> //
https://github.com/h-nari/Fontx
#include <FsFontx.h> //
https://github.com/h-nari/Fontx
#include <ShiftRegister74HC595.h> //
https://github.com/Simsso/ShiftRegister74HC595
FsFontx fx("/MISAKIMN.FNT","/4X8.FNT");
//FsFontx fx("/MISAKI.FNT","/4X8.FNT");
#define INTERVAL 300 // You can change
// create a global shift register object
// parameters: (number of shift registers, data pin, clock
pin, latch pin)
//ShiftRegister74HC595 sr (1, 0, 1, 2);
// number of shift registers = 2
// data pin = GPI16(D0)
// clock pin = GPIO12(D6)
// latch pin = GPIO14(D5)
ShiftRegister74HC595 sr (2, 16, 12, 14);
static unsigned long lastMillis;
static unsigned long timeOut;
// 0x00xx : xxは表示時間(xx*10ミリ秒 最大2550ミリ秒)
uint16_t str[] = \
{u'こ',u'ん',u'に',u'ち',0x0064,u'は',u'今',u'日',u'は',u'6',u'月',u'4',u'日',u'で
',0x00FF,u'す'};
int stlen;
//
//
http://nuneno.cocolog-nifty.com/blog/2016/07/48x8led-0f0f.html
から借用しました
//
// 任意サイズビットマップのドットON/OFF反転
// bmp: スクロール対象バッファ
// w: バッファの幅(ドット)
// h: バッファの高さ(ドット)
void revBitmap(uint8_t *bmp, uint16_t w, uint16_t h) {
uint16_t bl =
(w+7)>>3;
// 横バイト数
uint16_t
addr;
// データアドレス
uint8_t d;
addr=0;
for (uint8_t i=0; i <h; i++) {
for (uint8_t j=0; j <bl; j++) {
d =
~bmp[addr+j];
if (j+1 == bl &&
(w%8)!=0) {
d &=
0xff<<(8-(w%8));
}
bmp[addr+j]=d;
}
addr+=bl;
}
}
uint8_t bitConv(uint8_t val) {
uint8_t mask = 1;
uint8_t rval = 0;
uint8_t bits[] = {0x80, 0x40, 0x20, 0x10, 0x08,
0x04, 0x02, 0x01};
for(int i=0;i<8;i++) {
if (val & mask) rval = rval +
bits[i];
mask=mask << 1;
}
return rval;
}
void setup() {
Serial.begin(9600);
if(!SPIFFS.begin())
Serial.println("SPIFFS failed.");
else {
Dir dir = SPIFFS.openDir("/");
int cnt = 0;
while(dir.next()){
File f = dir.openFile("r");
Serial.printf("[%d] %-12s
%12u\n",++cnt,f.name(), f.size());
f.close();
}
Serial.printf("%d files found.\n",cnt);
}
const uint8_t *p;
uint8_t w,h;
stlen = sizeof(str)/sizeof(str[0]);
Serial.println("stlen=" + String(stlen));
// for(int i=0;i<sizeof(str)/sizeof(str[0]);i++){
for(int i=0;i<stlen;i++){
if((str[i] & 0xFF00) == 0x0000)
continue;
if(!fx.getGlyph(str[i], &p, &w,
&h)){
Serial.printf("getGlyph
failed. code:%x\n",str[i]);
} else {
for(int y=0; y<h; y++){
Serial.printf("%02d: ",y);
for(int x=0;
x<w; x++){
Serial.print(p[x/8] & (0x80 >> (x % 8)) ? '*' :
'.');
}
Serial.println();
p += (w + 7)/8;
}
}
}
sr.setAllLow(); // set all pins LOW
lastMillis = millis();
timeOut = millis() + INTERVAL;
}
void loop() {
uint8_t bitmap[8];
static int pos = 0;
uint8_t buf[2];
const uint8_t *p;
uint8_t w,h;
if(!fx.getGlyph(str[pos], &p, &w, &h)){
Serial.printf("getGlyph failed.
code:%x\n",str[pos]);
} else {
// memcpy(bitmap,p,8);
for(int i=0;i<8;i++) {
bitmap[i] = bitConv(*p);
p++;
}
revBitmap(bitmap, 8, 8); //ビットマップの反転
//LED制御 点灯したい行はHIGH 点灯したいカラムはLOW
for(int i=0;i<8;i++) {
buf[0] = bitmap[i]; // Col
buf[1] = 0x01 << i;
// Row
sr.setAll(buf);
}
}
//次の文字を処理するかどうかの判定
unsigned long now = millis();
if (now < lastMillis) timeOut = now + INTERVAL;
if (now > timeOut) {
pos++;
if (pos == stlen) pos = 0;
int interval = INTERVAL;
if((str[pos] & 0xFF00) == 0x0000) {
interval = (str[pos] &
0x00FF) * 10;
pos++;
}
// Serial.print("pos=" + String(pos));
// Serial.print(" interval=" +
String(interval));
// Serial.println();
lastMillis = now;
// timeOut = now + INTERVAL;
timeOut = now + interval;
}
}
|