Arduino OLED SSD1306 : 7SEG表示プログラム

# 概要
OLED SSD1306の文字を表示するライブラリの数値は7SEGのような形ではありません。
7SEGのような数値表示にするためのプログラムを作りました。

https://qiita.com/GANTZ/items/75fe66e550e91cdbf087

![WIN_20231228_14_08_23_Pro.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/405484/ff744890-cb75-b96e-91de-366f998cb563.jpeg)

こんな感じで表示できます。
textSize(3)と同じサイズ固定です。

# プログラム

```main.c
#include "seg.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
                         OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

Seg seg;

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  seg = Seg(display);


  display.clearDisplay();
  seg.setCursor(0, 20);
  seg.print(123456,6);
  display.display();
}

void loop() {
  
}
```

```seg.h
#ifndef SEG
#define SEG
#include "Arduino.h"
#include "seg_data.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

class Seg {
  private:
    int x;         //左から右方向へ
    int y;         //上から下方向へ
    Adafruit_SSD1306 display_seg;
  public:
    Seg(Adafruit_SSD1306 display);
    Seg();
    void setCursor(int x, int y);
    void print(long num, int digit);
};

#endif
```


```seg.cpp
#include "seg.h"
#include "seg_data.h"
#include "Arduino.h"

#define LOGO_WIDTH_SEG 24
#define LOGO_HEIGHT_SEG 32

Seg::Seg(Adafruit_SSD1306 display) {
  Seg::display_seg = display;
}
Seg::Seg(){

}

void Seg::setCursor(int x_g, int y_g) {
  Seg::x = x_g;
  Seg::y = y_g;
}

void Seg::print(long num, int digit) {
  Serial.print("digit  ");
  Serial.println(digit);
  if (digit < 1) {
    Serial.println("digit_error");
    return;
  }
  //numをString化する
  char buffer_num[10];
  dtostrf(num, digit, 0, buffer_num);
  //String num_string = String(buffer_num);

  int x = Seg::x;
  int y = Seg::y;
  Serial.print("switch - ");
  for (int i = 0; i < digit ; i++) {
    //Serial.println(buffer_num[i]);
    switch (buffer_num[i]) {
      case '0':
        Seg::display_seg.drawBitmap(x, y, seg_0, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        Serial.println("0");
        break;
      case '1':
        Serial.println("1");
        Seg::display_seg.drawBitmap(x, y, seg_1, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '2':
        Serial.println("2");
        Seg::display_seg.drawBitmap(x, y, seg_2, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '3':
        Serial.println("3");
        Seg::display_seg.drawBitmap(x, y, seg_3, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '4':
        Serial.println("4");
        Seg::display_seg.drawBitmap(x, y, seg_4, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '5':
        Serial.println("5");
        Seg::display_seg.drawBitmap(x, y, seg_5, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '6':
        Serial.println("6");
        Seg::display_seg.drawBitmap(x, y, seg_6, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '7':
        Serial.println("7");
        Seg::display_seg.drawBitmap(x, y, seg_7, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '8':
        Serial.println("8");
        Seg::display_seg.drawBitmap(x, y, seg_8, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      case '9':
        Serial.println("9");
        Seg::display_seg.drawBitmap(x, y, seg_9, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
        break;
      default:
        Serial.println("default");
        Seg::display_seg.drawBitmap(x, y, seg_1, LOGO_WIDTH_SEG, LOGO_HEIGHT_SEG, 1);
    }
    x += LOGO_WIDTH_SEG;
  }

}
```

```seg_data.h
#ifndef SEG_DATA
#define SEG_DATA

static const unsigned char PROGMEM seg_1[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b00010000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00110000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01110000,
  0b00000000, 0b00000000, 0b01110000,
  0b00000000, 0b00000000, 0b01110000,
  0b00000000, 0b00000000, 0b01100000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_2[] =
{ 0b00000001, 0b11111111, 0b11100000,
  0b00000011, 0b11111111, 0b11110000,
  0b00000011, 0b11111111, 0b11110000,
  0b00000001, 0b11111111, 0b11101100,
  0b00000000, 0b00000000, 0b00011110,
  0b00000000, 0b00000000, 0b00011110,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000111, 0b11111111, 0b11010000,
  0b00001111, 0b11111111, 0b11100000,
  0b00010111, 0b11111111, 0b11000000,
  0b01111000, 0b00000000, 0b00000000,
  0b01111000, 0b00000000, 0b00000000,
  0b01111000, 0b00000000, 0b00000000,
  0b01111000, 0b00000000, 0b00000000,
  0b01111000, 0b00000000, 0b00000000,
  0b01111000, 0b00000000, 0b00000000,
  0b11110000, 0b00000000, 0b00000000,
  0b11110000, 0b00000000, 0b00000000,
  0b11110000, 0b00000000, 0b00000000,
  0b11110000, 0b00000000, 0b00000000,
  0b01101111, 0b11111111, 0b00000000,
  0b00011111, 0b11111111, 0b10000000,
  0b00011111, 0b11111111, 0b10000000,
  0b00001111, 0b11111111, 0b00000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_3[] =
{0b00000000, 0b00000000, 0b00000000,  
0b00000011, 0b11111111, 0b11110000,  
0b00000011, 0b11111111, 0b11110000,  
0b00000001, 0b11111111, 0b11101100,  
0b00000000, 0b00000000, 0b00011100,  
0b00000000, 0b00000000, 0b00011100,  
0b00000000, 0b00000000, 0b00011100,  
0b00000000, 0b00000000, 0b00111100,  
0b00000000, 0b00000000, 0b00111100,  
0b00000000, 0b00000000, 0b00111100,  
0b00000000, 0b00000000, 0b00111100,  
0b00000000, 0b00000000, 0b00111100,  
0b00000000, 0b00000000, 0b00111100,  
0b00000000, 0b00000000, 0b00111000,  
0b00000111, 0b11111111, 0b11010000,  
0b00001111, 0b11111111, 0b11100000,  
0b00000111, 0b11111111, 0b11110000,  
0b00000000, 0b00000000, 0b01111000,  
0b00000000, 0b00000000, 0b01111000,  
0b00000000, 0b00000000, 0b01111000,  
0b00000000, 0b00000000, 0b01111000,  
0b00000000, 0b00000000, 0b01111000,  
0b00000000, 0b00000000, 0b01111000,  
0b00000000, 0b00000000, 0b01110000,  
0b00000000, 0b00000000, 0b01110000,  
0b00000000, 0b00000000, 0b01110000,  
0b00000000, 0b00000000, 0b11110000,  
0b00001111, 0b11111111, 0b01100000,  
0b00011111, 0b11111111, 0b11000000,  
0b00001111, 0b11111111, 0b00000000,  
0b00000000, 0b00000000, 0b00000000,  
0b00000000, 0b00000000, 0b00000000 
};
static const unsigned char PROGMEM seg_4[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00001100, 0b00000000, 0b00001100,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011100, 0b00000000, 0b00011110,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00011011, 0b11111111, 0b11011000,
  0b00001111, 0b11111111, 0b11100000,
  0b00000011, 0b11111111, 0b11011000,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b00110000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_5[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000001, 0b11111111, 0b11110000,
  0b00000011, 0b11111111, 0b11111000,
  0b00000101, 0b11111111, 0b11110000,
  0b00001110, 0b00000000, 0b00000000,
  0b00001110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011100, 0b00000000, 0b00000000,
  0b00001011, 0b11111111, 0b11000000,
  0b00000111, 0b11111111, 0b11110000,
  0b00000011, 0b11111111, 0b11101000,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000111, 0b11111111, 0b10110000,
  0b00001111, 0b11111111, 0b11000000,
  0b00001111, 0b11111111, 0b10000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_6[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000001, 0b11111111, 0b11100000,
  0b00000011, 0b11111111, 0b11110000,
  0b00000101, 0b11111111, 0b11100000,
  0b00001110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011110, 0b00000000, 0b00000000,
  0b00011100, 0b00000000, 0b00000000,
  0b00011100, 0b00000000, 0b00000000,
  0b00111100, 0b00000000, 0b00000000,
  0b00011101, 0b11111111, 0b10000000,
  0b00001111, 0b11111111, 0b11000000,
  0b00000111, 0b11111111, 0b11100000,
  0b00011011, 0b11111111, 0b10110000,
  0b00111100, 0b00000000, 0b01111000,
  0b00111100, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01110000,
  0b01111000, 0b00000000, 0b01110000,
  0b01111000, 0b00000000, 0b01110000,
  0b01111000, 0b00000000, 0b11110000,
  0b01111000, 0b00000000, 0b11110000,
  0b00110111, 0b11111111, 0b11100000,
  0b00011111, 0b11111111, 0b10000000,
  0b00001111, 0b11111111, 0b10000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_7[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000001, 0b11111111, 0b11100000,
  0b00000011, 0b11111111, 0b11110000,
  0b00000001, 0b11111111, 0b11101000,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00011000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00010000,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01110000,
  0b00000000, 0b00000000, 0b01110000,
  0b00000000, 0b00000000, 0b00100000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_8[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000001, 0b11111111, 0b11100000,
  0b00000011, 0b11111111, 0b11110000,
  0b00000101, 0b11111111, 0b11110000,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00111100,
  0b00011100, 0b00000000, 0b00111100,
  0b00011100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00011011, 0b11111111, 0b11011000,
  0b00000111, 0b11111111, 0b11100000,
  0b00000111, 0b11111111, 0b11100000,
  0b00011011, 0b11111111, 0b10111000,
  0b00111100, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b01110000, 0b00000000, 0b01110000,
  0b00101111, 0b11111111, 0b10100000,
  0b00011111, 0b11111111, 0b11000000,
  0b00001111, 0b11111111, 0b00000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_9[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b11111111, 0b11110000,
  0b00000011, 0b11111111, 0b11111000,
  0b00000101, 0b11111111, 0b11110100,
  0b00001110, 0b00000000, 0b00001110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00011100,
  0b00011100, 0b00000000, 0b00111100,
  0b00011100, 0b00000000, 0b00111100,
  0b00011101, 0b11111111, 0b11011000,
  0b00000111, 0b11111111, 0b11100000,
  0b00000111, 0b11111111, 0b11100000,
  0b00000011, 0b11111111, 0b11011000,
  0b00000000, 0b00000000, 0b00111100,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b00111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00000000, 0b00000000, 0b01111000,
  0b00001111, 0b11111111, 0b10100000,
  0b00011111, 0b11111111, 0b11000000,
  0b00001111, 0b11111111, 0b10000000,
  0b00000000, 0b00000000, 0b00000000
};

static const unsigned char PROGMEM seg_0[] =
{ 0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b11111111, 0b11110000,
  0b00000011, 0b11111111, 0b11111000,
  0b00000101, 0b11111111, 0b11110100,
  0b00001110, 0b00000000, 0b00001110,
  0b00001110, 0b00000000, 0b00011110,
  0b00001110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011110,
  0b00011110, 0b00000000, 0b00011100,
  0b00011110, 0b00000000, 0b00011100,
  0b00011100, 0b00000000, 0b00011100,
  0b00000000, 0b00000000, 0b00000000,
  0b00000000, 0b00000000, 0b00000000,
  0b00011000, 0b00000000, 0b00011000,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111100,
  0b00111100, 0b00000000, 0b00111000,
  0b00111100, 0b00000000, 0b00111000,
  0b00111000, 0b00000000, 0b00111000,
  0b00111000, 0b00000000, 0b01111000,
  0b00111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b01111000, 0b00000000, 0b01111000,
  0b00011111, 0b11111111, 0b10100000,
  0b00001111, 0b11111111, 0b11000000,
  0b00000111, 0b11111111, 0b10000000,
  0b00000000, 0b00000000, 0b00000000
};

#endif
```