Adafruit_を改造しST7735(パラレル)にイメージを表示する1(雑に)(白黒)
目的
俺の考えた最強のhhを作る
ハード
OLED_SSD1306_BITMAP_TEST_DV55_UNO
//OLED_SSD1331_BITMAP_TEST1_UNO
//ヘッダーファイル
#include <Wire.h>
#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h>
#include "hh.h"
//定義
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
hh_Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET, 400000UL, 100000UL);
// ビットマップデータ
uint8_t databytes[8] =
{
0b01100110,
0b10101111,
0b10111111,
0b11011111,
0b01111110,
0b01111110,
0b00111100,
0b00011000
};
//初期化
void setup() {
// I2Cアドレスは使用するディスプレイに合わせて変更する
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
}//setup
//メインループ
void loop() {
// 画面表示をクリア
display.clearDisplay();
//ビットマップの表示
display.drawBitmap(0, 0, databytes, 8, 8, WHITE);
// テキストサイズを設定
//display.setTextSize(3);
// テキスト色を設定
//display.setTextColor(WHITE);
// テキストの開始位置を設定
//display.setCursor(20, 20);
// 1行目に46を表示
//display.println("123");
// 描画バッファの内容を画面に表示
display.display();
delay(1000); //1秒待つ
}//loop
hh.cpp
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <pgmspace.h>
#else
#define pgm_read_byte(addr) \
(*(const unsigned char *)(addr)) ///< PROGMEM workaround for non-AVR
#endif
#if !defined(__ARM_ARCH) && !defined(ENERGIA) && !defined(ESP8266) && \
!defined(ESP32) && !defined(__arc__)
#include <util/delay.h>
#endif
//#include "Adafruit_SSD1306.h"
#include "hh.h"
#include <Adafruit_GFX.h>
#define ssd1306_swap(a, b) \
(((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) ///< No-temp-var swap operation
//GPIO
#define GPIO_A0(s) digitalWrite(A0,s)
#define GPIO_A1(s) digitalWrite(A1,s)
#define GPIO_A2(s) digitalWrite(A2,s)
#define GPIO_A3(s) digitalWrite(A3,s)
void hh_Adafruit_SSD1306::GPIO_8BIT(uint8_t s)
{
digitalWrite( 7, (s >> 7) & 0x01);
digitalWrite( 6, (s >> 6) & 0x01);
digitalWrite( 5, (s >> 5) & 0x01);
digitalWrite( 4, (s >> 4) & 0x01);
digitalWrite( 3, (s >> 3) & 0x01);
digitalWrite( 2, (s >> 2) & 0x01);
digitalWrite(A5, (s >> 1) & 0x01);
digitalWrite(A4, s & 0x01);
} //GPIO_8BIT
//コマンドの書き込み
void hh_Adafruit_SSD1306::LCD_Write_CMD(uint8_t ww)
{
GPIO_A1(0); //A0=0;
GPIO_8BIT(ww);//P1=a; data
GPIO_A2(0);//WRB=0;
GPIO_A2(1);//WRB=1;
} //LCD_Write_CMD
//データ書き込み
void hh_Adafruit_SSD1306::LCD_Write_Data(uint8_t ii)
{
GPIO_A1(1);//A0=1;
GPIO_8BIT(ii);//P1=a; data
GPIO_A2(0);//WRB=0;
GPIO_A2(1);//WRB=1;
} //LCD_Write_Data
//液晶の初期化処理
void hh_Adafruit_SSD1306::TXDT144TF_ST7735S_Init(void)
{
//---------- ST7735S Reset Sequence --------//
GPIO_A0(1);//LCD_RESET=1;
delay(1); //Delay 1ms
GPIO_A0(0);//LCD_RESET=0;
delay(1); //Delay 1ms
GPIO_A0(1);//LCD_RESET=1;
delay(120); //Delay 120ms
LCD_Write_CMD(0x01);//SOFTWARE RESET
delay(50);
LCD_Write_CMD(0x11);//SLEEP OUT
delay(200);
LCD_Write_CMD(0x29);//display on
delay(100);
LCD_Write_CMD(0x3a);//Interface pixel format
LCD_Write_Data(0x05);//16bit mode
delay(100);
LCD_Write_CMD(0x36);//RGB-RGR format
LCD_Write_Data(0x08);//RGB mode
delay(100);
//画面の書き込み開始
LCD_Write_CMD(0x2C); //memory write
//for(int i=0;i<128*10;i++){
//LCD_Write_Data(0xff);
//LCD_Write_Data(0xff);
//} //for
//RGB
for(int i=0;i<128*160;i++){
LCD_Write_Data(0b00000111); //G
LCD_Write_Data(0b11100000);
} //for
} //TXDT144TF_ST7735S_Init
hh_Adafruit_SSD1306::hh_Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi,
int8_t rst_pin, uint32_t clkDuring,
uint32_t clkAfter)
: Adafruit_GFX(w, h), spi(NULL), wire(twi ? twi : &Wire), buffer(NULL),
mosiPin(-1), clkPin(-1), dcPin(-1), csPin(-1), rstPin(rst_pin)
#if ARDUINO >= 157
,
wireClk(clkDuring), restoreClk(clkAfter)
#endif
{
}
//バッファのクリア
hh_Adafruit_SSD1306::~hh_Adafruit_SSD1306(void) {
if (buffer) {
free(buffer);
buffer = NULL;
}
}
//初期処理
bool hh_Adafruit_SSD1306::begin(uint8_t vcs, uint8_t addr, bool reset,
bool periphBegin) {
if ((!buffer) && !(buffer = (uint8_t *)malloc(WIDTH * ((HEIGHT + 7) / 8))))
return false;
clearDisplay();
//ポートのモード設定
//アウトプットモード
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
//ポートの初期化
GPIO_A3(1);//RD=1
GPIO_A2(0);//WR=0
GPIO_A1(0);//RS=0
GPIO_A0(1);//RESET=1
delay(500); //0.5秒待つ
//液晶の初期化処理
TXDT144TF_ST7735S_Init();
return true; // Success
}
//点の表示
void hh_Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
// Pixel is in-bounds. Rotate coordinates if needed.
switch (getRotation()) {
case 1:
ssd1306_swap(x, y);
x = WIDTH - x - 1;
break;
case 2:
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3:
ssd1306_swap(x, y);
y = HEIGHT - y - 1;
break;
}
switch (color) {
case SSD1306_WHITE:
buffer[(x/8) + (y * 16) ] |= (1 << (7-(x & 7)));
break;
case SSD1306_BLACK:
buffer[(x/8) + (y * 16) ] &= ~(1 << (7-(x & 7)));
break;
case SSD1306_INVERSE:
buffer[(x/8) + (y * 16) ] ^= (1 << (7-(x & 7)));
break;
}
}
}
//バッファのクリア
void hh_Adafruit_SSD1306::clearDisplay(void) {
memset(buffer, 0, WIDTH * ((HEIGHT + 7) / 8));
}
bool hh_Adafruit_SSD1306::getPixel(int16_t x, int16_t y) {
if ((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
// Pixel is in-bounds. Rotate coordinates if needed.
switch (getRotation()) {
case 1:
ssd1306_swap(x, y);
x = WIDTH - x - 1;
break;
case 2:
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3:
ssd1306_swap(x, y);
y = HEIGHT - y - 1;
break;
}
return (buffer[x + (y / 8) * WIDTH] & (1 << (y & 7)));
}
return false; // Pixel out of bounds
}
uint8_t *hh_Adafruit_SSD1306::getBuffer(void) { return buffer; }
void hh_Adafruit_SSD1306::display(void) {
#if defined(ESP8266)
// ESP8266 needs a periodic yield() call to avoid watchdog reset.
// With the limited size of SSD1306 displays, and the fast bitrate
// being used (1 MHz or more), I think one yield() immediately before
// a screen write and one immediately after should cover it. But if
// not, if this becomes a problem, yields() might be added in the
// 32-byte transfer condition below.
yield();
#endif
uint16_t count = WIDTH * ((HEIGHT + 7) / 8);
uint8_t *ptr = buffer;
while (count--) {
//WIRE_WRITE(*ptr++);
}
//画面の書き込み開始
LCD_Write_CMD(0x2C); //memory write
GPIO_A1(1);//A0=1;
int jjk=0,vg=0;
int hp[8]={0x80,0x40,0x20,0x10, 0x08,0x04,0x02,0x01};
for (int i = 0; i < 64; i++) {
for (int j = 0; j < (128/8); j++) {
vg=ptr[jjk];
for(int k=0;k < 8;k++){
if ( ( vg & hp[k]) == 0 ) {GPIO_8BIT(0x00);} else {GPIO_8BIT(0xff);}
GPIO_A2(0);//WRB=0; 10
GPIO_A2(1);//WRB=1; 10
GPIO_A2(0);//WRB=0; 10
GPIO_A2(1);//WRB=1; 10
}//k
jjk++;
} //j
}//i
#if defined(ESP8266)
yield();
#endif
}//display
hh.h
#ifndef _Adafruit_SSD1306_H_
#define _Adafruit_SSD1306_H_
#define SSD1306_128_32 ///< DEPRECATED: old way to specify 128x32 screen
#if defined(ARDUINO_STM32_FEATHER)
typedef class HardwareSPI SPIClass;
#endif
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#if defined(__AVR__)
typedef volatile uint8_t PortReg;
typedef uint8_t PortMask;
#define HAVE_PORTREG
#elif defined(__SAM3X8E__)
typedef volatile RwReg PortReg;
typedef uint32_t PortMask;
#define HAVE_PORTREG
#elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \
!defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040)
typedef volatile uint32_t PortReg;
typedef uint32_t PortMask;
#define HAVE_PORTREG
#endif
/// The following "raw" color names are kept for backwards client compatability
/// They can be disabled by predefining this macro before including the Adafruit
/// header client code will then need to be modified to use the scoped enum
/// values directly
#ifndef NO_ADAFRUIT_SSD1306_COLOR_COMPATIBILITY
#define BLACK SSD1306_BLACK ///< Draw 'off' pixels
#define WHITE SSD1306_WHITE ///< Draw 'on' pixels
#define INVERSE SSD1306_INVERSE ///< Invert pixels
#endif
/// fit into the SSD1306_ naming scheme
#define SSD1306_BLACK 0 ///< Draw 'off' pixels
#define SSD1306_WHITE 1 ///< Draw 'on' pixels
#define SSD1306_INVERSE 2 ///< Invert pixels
#define SSD1306_MEMORYMODE 0x20 ///< See datasheet
#define SSD1306_COLUMNADDR 0x21 ///< See datasheet
#define SSD1306_PAGEADDR 0x22 ///< See datasheet
#define SSD1306_SETCONTRAST 0x81 ///< See datasheet
#define SSD1306_CHARGEPUMP 0x8D ///< See datasheet
#define SSD1306_SEGREMAP 0xA0 ///< See datasheet
#define SSD1306_DISPLAYALLON_RESUME 0xA4 ///< See datasheet
#define SSD1306_DISPLAYALLON 0xA5 ///< Not currently used
#define SSD1306_NORMALDISPLAY 0xA6 ///< See datasheet
#define SSD1306_INVERTDISPLAY 0xA7 ///< See datasheet
#define SSD1306_SETMULTIPLEX 0xA8 ///< See datasheet
#define SSD1306_DISPLAYOFF 0xAE ///< See datasheet
#define SSD1306_DISPLAYON 0xAF ///< See datasheet
#define SSD1306_COMSCANINC 0xC0 ///< Not currently used
#define SSD1306_COMSCANDEC 0xC8 ///< See datasheet
#define SSD1306_SETDISPLAYOFFSET 0xD3 ///< See datasheet
#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 ///< See datasheet
#define SSD1306_SETPRECHARGE 0xD9 ///< See datasheet
#define SSD1306_SETCOMPINS 0xDA ///< See datasheet
#define SSD1306_SETVCOMDETECT 0xDB ///< See datasheet
#define SSD1306_SETLOWCOLUMN 0x00 ///< Not currently used
#define SSD1306_SETHIGHCOLUMN 0x10 ///< Not currently used
#define SSD1306_SETSTARTLINE 0x40 ///< See datasheet
#define SSD1306_EXTERNALVCC 0x01 ///< External display voltage source
#define SSD1306_SWITCHCAPVCC 0x02 ///< Gen. display voltage from 3.3V
#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 ///< Init rt scroll
#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 ///< Init left scroll
#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 ///< Init diag scroll
#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A ///< Init diag scroll
#define SSD1306_DEACTIVATE_SCROLL 0x2E ///< Stop scroll
#define SSD1306_ACTIVATE_SCROLL 0x2F ///< Start scroll
#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 ///< Set scroll range
// Deprecated size stuff for backwards compatibility with old sketches
#if defined SSD1306_128_64
#define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_64 defined
#define SSD1306_LCDHEIGHT 64 ///< DEPRECATED: height w/SSD1306_128_64 defined
#endif
#if defined SSD1306_128_32
#define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_32 defined
#define SSD1306_LCDHEIGHT 32 ///< DEPRECATED: height w/SSD1306_128_32 defined
#endif
#if defined SSD1306_96_16
#define SSD1306_LCDWIDTH 96 ///< DEPRECATED: width w/SSD1306_96_16 defined
#define SSD1306_LCDHEIGHT 16 ///< DEPRECATED: height w/SSD1306_96_16 defined
#endif
/*!
@brief Class that stores state and functions for interacting with
SSD1306 OLED displays.
*/
class hh_Adafruit_SSD1306 : public Adafruit_GFX {
public:
// NEW CONSTRUCTORS -- recommended for new projects
hh_Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
uint32_t clkAfter = 100000UL);
~hh_Adafruit_SSD1306(void);
bool begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = 0,
bool reset = true, bool periphBegin = true);
void display(void);
void clearDisplay(void);
void drawPixel(int16_t x, int16_t y, uint16_t color);
bool getPixel(int16_t x, int16_t y);
uint8_t *getBuffer(void);
void GPIO_8BIT(uint8_t s);
void LCD_Write_CMD(uint8_t ww);
void LCD_Write_Data(uint8_t ii);
void TXDT144TF_ST7735S_Init(void);
protected:
SPIClass *spi; ///< Initialized during construction when using SPI. See
///< SPI.cpp, SPI.h
TwoWire *wire; ///< Initialized during construction when using I2C. See
///< Wire.cpp, Wire.h
uint8_t *buffer; ///< Buffer data used for display buffer. Allocated when
///< begin method is called.
int8_t i2caddr; ///< I2C address initialized when begin method is called.
int8_t vccstate; ///< VCC selection, set by begin method.
int8_t page_end; ///< not used
int8_t mosiPin; ///< (Master Out Slave In) set when using SPI set during
///< construction.
int8_t clkPin; ///< (Clock Pin) set when using SPI set during construction.
int8_t dcPin; ///< (Data Pin) set when using SPI set during construction.
int8_t
csPin; ///< (Chip Select Pin) set when using SPI set during construction.
int8_t rstPin; ///< Display reset pin assignment. Set during construction.
#ifdef HAVE_PORTREG
PortReg *mosiPort, *clkPort, *dcPort, *csPort;
PortMask mosiPinMask, clkPinMask, dcPinMask, csPinMask;
#endif
#if ARDUINO >= 157
uint32_t wireClk; ///< Wire speed for SSD1306 transfers
uint32_t restoreClk; ///< Wire speed following SSD1306 transfers
#endif
uint8_t contrast; ///< normal contrast setting for this device
#if defined(SPI_HAS_TRANSACTION)
protected:
// Allow sub-class to change
SPISettings spiSettings;
#endif
};
#endif // _Adafruit_SSD1306_H_