Skip to content

Instantly share code, notes, and snippets.

@edgarogh
Created March 21, 2022 23:52
Show Gist options
  • Save edgarogh/3501b5287acd0939b00df56bddd68435 to your computer and use it in GitHub Desktop.
Save edgarogh/3501b5287acd0939b00df56bddd68435 to your computer and use it in GitHub Desktop.
SPC
#define CLK_PIN 13 // or SCK
#define DATA_PIN 11 // or MOSI
#define CS_PIN 10 // or SS
// Taille du buffer d'envoi (4 écrans * 2 octets par écran)
#define BUFFER_SIZE 8
#include <Arduino.h>
#include <SPI.h>
#include "screen.h"
#include "screen_glyphs.h"
namespace screen {
uint8_t buffer[BUFFER_SIZE];
// Écrit le contenu du buffer d'envoi sur la ligne SPI
void commit(void) {
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
for (uint16_t i = 0; i < BUFFER_SIZE; i++) SPI.transfer(buffer[i]);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
}
void setRow(uint8_t dev, uint8_t row, uint8_t value) {
dev *= 2;
buffer[dev+0] = row+1;
buffer[dev+1] = value;
}
// Envoie un code de contrôle aux 4 écrans
void control(uint8_t code, uint8_t arg) {
for (int i = 0; i < BUFFER_SIZE; i += 2) {
buffer[i+0] = code;
buffer[i+1] = arg;
}
commit();
}
// Met en place le SPI et initialise l'écran
void init(void) {
SPI.begin();
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
control(15, 0); // Display test disabled
control(11, 8-1); // Scan limit 8
control(10, 16); // Maximum led intensity
control(9, 0); // Decode mode disabled
control(12, 1); // Shutdown disabled
}
union {
uint32_t i[8];
uint8_t b[32];
} screen_buffer;
void writeCounter(uint32_t tenths) {
memset(screen_buffer.b, 0, 32);
// Dizièmes
for (uint8_t row = 0; row < 8; row++) {
screen_buffer.i[row] = ((uint32_t) TENTHS[tenths % 10][row]);
}
uint32_t offset = 8;
uint32_t time_units = tenths / 10;
bool zero = time_units == 0;
// Unités
if (zero) {
for (uint8_t row = 0; row < 8; row++) {
screen_buffer.i[row] |= (((uint32_t) INTEGERS[0][row]) << offset);
}
} else {
while (time_units > 0 && offset < 28) {
uint8_t digit = time_units % 10;
for (uint8_t row = 0; row < 8; row++) {
screen_buffer.i[row] |= (((uint32_t) INTEGERS[digit][row]) << offset);
}
offset += INTEGER_WIDTHS[digit];
time_units /= 10;
}
}
for (uint8_t row = 0; row < 8; row++) {
for (uint8_t dev = 0; dev < 4; dev++) {
setRow(3 - dev, row, screen_buffer.b[row * 4 + dev]);
}
commit();
}
}
}
namespace screen {
void init(void);
void writeCounter(uint32_t tenths);
}
const byte TENTHS[][8] = {
{
B00000000,
B00000000,
B00000100,
B00001010,
B00001010,
B01101010,
B01100100,
B00000000
},{
B00000000,
B00000000,
B00000100,
B00001100,
B00000100,
B01100100,
B01100100,
B00000000
},{
B00000000,
B00000000,
B00001110,
B00001010,
B00000010,
B01100100,
B01101110,
B00000000
},{
B00000000,
B00000000,
B00001110,
B00000010,
B00001110,
B01100010,
B01101110,
B00000000
},{
B00000000,
B00000000,
B00001010,
B00001010,
B00001110,
B01100010,
B01100010,
B00000000
},{
B00000000,
B00000000,
B00001110,
B00001000,
B00001100,
B01100010,
B01101100,
B00000000
},{
B00000000,
B00000000,
B00000110,
B00001000,
B00001110,
B01101010,
B01101110,
B00000000
},{
B00000000,
B00000000,
B00001110,
B00000010,
B00000100,
B01100100,
B01100100,
B00000000
},{
B00000000,
B00000000,
B00001110,
B00001010,
B00001110,
B01101010,
B01101110,
B00000000
},{
B00000000,
B00000000,
B00001110,
B00001010,
B00001110,
B01100010,
B01101100,
B00000000
}};
const byte INTEGERS[][8] = {
{
B00000000,
B00000110,
B00001001,
B00001101,
B00001011,
B00001001,
B00000110,
B00000000
},{
B00000000,
B00000010,
B00000110,
B00000010,
B00000010,
B00000010,
B00000111,
B00000000
},{
B00000000,
B00000110,
B00001001,
B00000001,
B00000110,
B00001000,
B00001111,
B00000000
},{
B00000000,
B00000110,
B00001001,
B00000010,
B00000001,
B00001001,
B00000110,
B00000000
},{
B00000000,
B00000010,
B00000110,
B00001010,
B00010010,
B00011111,
B00000010,
B00000000
},{
B00000000,
B00001111,
B00001000,
B00001110,
B00000001,
B00001001,
B00000110,
B00000000
},{
B00000000,
B00000111,
B00001000,
B00001110,
B00001001,
B00001001,
B00000110,
B00000000
},{
B00000000,
B00001111,
B00000001,
B00000001,
B00000010,
B00000010,
B00000010,
B00000000
},{
B00000000,
B00000110,
B00001001,
B00000110,
B00001001,
B00001001,
B00000110,
B00000000
},{
B00000000,
B00000110,
B00001001,
B00001001,
B00000111,
B00000001,
B00000110,
B00000000
}};
uint8_t INTEGER_WIDTHS[10] = { 5, 4, 5, 5, 6, 5, 5, 5, 5, 5 };
#include "screen.h"
uint32_t d = 0;
void setup() {
screen::init();
}
void loop() {
screen::writeCounter(d++);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment