Skip to content

Instantly share code, notes, and snippets.

@kristopolous
Created December 14, 2022 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopolous/910f168afb3ccc26357afb150d5a80df to your computer and use it in GitHub Desktop.
Save kristopolous/910f168afb3ccc26357afb150d5a80df to your computer and use it in GitHub Desktop.
7segment scroll
#include <TM1637.h>
//
// This is just some mish-mash of a bunch of things but it's posted for the video over here:
// https://youtu.be/JN4j-aCagH0
//
// How you set the date:
// exec 3<> /dev/ttyUSB0
// printf "$(date "+obase=16;4096+(%H*60+%M)" | bc | sed -E s'/(..)/\\x\1/g')" > /dev/ttyUSB0
// exec 3<&-
// The wire protocol is 3 bytes
//
// |----|---- ... ---|
// cmd args
//
// 0x0 is brightness (4 bits) + clock set (minutes from midnight lsb, 2 bytes)
// 0x1 is time to display message in 100ms chunks (4bits) + 4 digits display
int CLK = 3;
int DIO = 2;
int offset = 0;
int cycle = 0;
int synctime = 0;
int ix = 0;
uint8_t inbuf[3];
TM1637 tm(CLK,DIO);
void setup() {
tm.init();
Serial.begin(9600);
tm.set(7);
}
void displayTime(int minute) {
displayNumber(100*(minute/60) + minute % 60);
}
static int8_t alphie[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,
// a b c d e
0x77, 0x7C, 0x39, 0x5e, 0x79,
// f g h i j
0x71, 0x7d, 0x76, 0x30, 0x1e,
// k l m n o
0x72, 0x38, 0x33, 0x54, 0x5c,
// p q r s t
0x73, 0x67, 0x50, 0x6d, 0x78,
// u v w x y
0x3e, 0x2a, 0x62, 0x52, 0x66,
// z
0x1b
};
int8_t shift(int8_t in, int dir) {
int8_t out = 0;
static int8_t map[] = {
0x02, 0x04,
0x01, 0x40,
0x20, 0x10,
0x40, 0x08,
};
int8_t off_1 = dir >= 0 ? 0 : 1;
int8_t off_2 = dir >= 0 ? 1 : 0;
for(int s = 0; s < 8; s += 2) {
out |= ((in & map[s + off_1]) > 0) ? map[s + off_2] : 0;
}
return out;
}
void segdo(uint8_t BitAddr,int8_t SegData)
{
tm.start(); //start signal sent to TM1637 from MCU
tm.writeByte(ADDR_FIXED);//
tm.stop(); //
tm.start(); //
tm.writeByte(BitAddr|0xc0);//
tm.writeByte(SegData);//
tm.stop(); //
tm.start(); //
tm.writeByte(tm.Cmd_DispCtrl);//
tm.stop(); //
}
void word(char toshow[]) {
for(int ix = 0; ix < 4 && ix < strlen(toshow); ix++) {
segdo(ix, alphie[toshow[ix] - 'a']);
}
}
int cycles = 0;
void show(int8_t seg, int8_t what, int dur) {
for(int iy=dur/5; iy > 0; iy--) {
segdo(seg, what); delay(1);
segdo(seg, 0); delay(4);
}
}
void loop() {
/* for(int i = 0; i < 4; i++) {
segdo(i,alphie[(i + ix)%26]);
}*/
int8_t up1, up2, down1, down2, tens;
for(int ix = 49; ix > 0; ix--) {
int8_t ones = ix % 10;
if(ones == 9) {
tens = ix / 10 % 10;
}
up1 = shift(alphie[ones], -1);
up2 = shift(up1, -1);
down1 = shift(alphie[ones], 1);
down2 = shift(down1, 1);
cycles ++;
if(ones == 9) {
segdo(0, shift(shift(alphie[tens], -1), -1));
}
segdo(1, up2); delay(150);
if(ones == 9) {
segdo(0, shift(alphie[tens], -1));
}
segdo(1, up1); delay(200);
if(ones == 9) {
segdo(0, alphie[tens]);
}
segdo(1, alphie[ones]); delay(300);
if(ones == 0) {
segdo(0, shift(alphie[tens], 1));
}
segdo(1, down1); delay(200);
if(ones == 0) {
segdo(0, shift(shift(alphie[tens], 1), 1));
}
segdo(1, down2); delay(150);//max(1,cycles * 1000 - millis()) );
}
/*
if (Serial.available() > 0){
Serial.readBytes(inbuf, 10);
segdo(0, inbuf[0]);
segdo(1, shift(inbuf[0], 1));
segdo(2, shift(inbuf[0], -1));
}
delay(200);
ix++;
/*
tm.point((ix++)&1);
displayTime( ((millis()-synctime) / 60000 + offset) % 1440);
delay(500);
if (Serial.available() > 0){
Serial.readBytes(inbuf, 3);
switch(inbuf[0]&0xF0) {
case 0x00:
offset = inbuf[2] + (inbuf[1]&0xf)*256;
synctime = millis();
tm.set(inbuf[0]&0xf);
break;
case 0x10:
int duration = (inbuf[0]&0x0f) * 250;
displayNumber(inbuf[2] + (inbuf[1]&0xf)*256);
delay(duration);
break;
}
}
*/
;}
void displayNumber(int num){
tm.display(3, num % 10);
tm.display(2, num / 10 % 10);
tm.display(1, num / 100 % 10);
tm.display(0, num / 1000 % 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment