ST7735(Parallel)に16x16カラーの「なぞの落書き」を表示する。


x 過去ログを見ょ!!!


目的
カラー変換のテスト

![o_coq087.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/9978eb20-b4cb-65b7-c841-113ae8943408.jpeg)


1.もとデータ

![o_coq085.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/d24e6383-638b-cee5-302b-e589c7c58727.jpeg)


2.ダンプにデータ変換


https://colorcodesearch.com/image-hex/


![o_coq086.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/1a838391-1661-8353-ddd5-30e8e8c98787.jpeg)


3.オンラインコンパイラで32bitを16ビットにする


```c++



char bitmap[] = {
    
"000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000"
"FFFFFF000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000"
"FFFFFFFFFFFF000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000"
"FFFFFFFFFFFFFFFFFF000000000000FF0000FF0000FF0000FF0000FF0000FFFFFFFFFFFFFFFFFF000000000000FFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFFFFFF0000FF0000FF0000000000000000FFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FFFFFF000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000FFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FFFFFF000000000000FFFFFFFFFFFFFFFFFFFFFFFF000000000000FFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFF000000000000FFFFFF000000000000000000FF0000FFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFF000000000000000000FFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FF0000FFFFFFFFFFFFFFFFFFFFFFFFFF0000FF0000FF0000FF0000FFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFFFFFFFF0000FF0000FF0000FF0000FF0000FF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FF0000FFFFFFFF0000FF0000FF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFF0000FF0000FF0000FF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
"FFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFFFF0000FF0000FF0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"

}; //bitmap


#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int size_b = ( sizeof bitmap ) - 1;
    int R,G,B;
    int RGB;
    
    printf("\nSTART\n\n");
        for(int ii = 0;ii < (size_b / 6) ;ii++){

#define hex_cov(aa)   ((aa<':')?aa-'0':aa-'A'+10)

        R = (hex_cov(bitmap[ii*6+0])<<4) + hex_cov(bitmap[ii*6+1]);
        R = R >> 3;
        
        G = (hex_cov(bitmap[ii*6+2])<<4) + hex_cov(bitmap[ii*6+3]);
        G = G >> 2;
    
        B = (hex_cov(bitmap[ii*6+4])<<4) + hex_cov(bitmap[ii*6+5]);
        B = B >> 3;
        
        
 
        RGB = (R<<11)+(G<<5)+(B);
          
        //  1234512345612345
        //  6543210987654321
       

        //printf("%c",bitmap[ii*6+0]);
        //printf("%c",bitmap[ii*6+1]);
        //printf("%c",bitmap[ii*6+2]);
        //printf("%c",bitmap[ii*6+3]);
        //printf("%c",bitmap[ii*6+4]);
        //printf("%c",bitmap[ii*6+5]);
        //printf("\n");


        
        //printf("%d-",  R); 
        //printf("%d-",  G);
        //printf("%d\n", B);

        printf("%04X,", RGB);
        
        //printf("\n");
        //printf("ppppppp\n");
        
        


    } //for ii
    
    printf("\n\n");
    
}


```


----------------------------------------


4, プログラムに張り付ける


プログラム


```c++



//ST7735S_P_BITMAP_M5S3_1


//ヘッダーファイル
#include <Adafruit_GFX.h>
#include "hh.h"

 
//定義
#define SCREEN_WIDTH  128 // LCD display width,  in pixels
#define SCREEN_HEIGHT 160 // LCD display height, in pixels
NA_ST7735S_P display(SCREEN_WIDTH, SCREEN_HEIGHT);


// ビットマップデータ
const uint16_t databytes[] =
{
  

0x0000,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x0000,
0xFFFF,0x0000,0x0000,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x0000,
0xFFFF,0xFFFF,0x0000,0x0000,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x0000,0x0000,
0xFFFF,0xFFFF,0xFFFF,0x0000,0x0000,0xF800,0xF800,0xF800,0xF800,0xF800,0xFFFF,0xFFFF,0xFFFF,0x0000,0x0000,0xFFFF,
0xFFFF,0xFFFF,0xFFFF,0xF800,0x0000,0x0000,0xFFFF,0xFFFF,0xFFFF,0xF800,0x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xF800,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
0xFFFF,0xFFFF,0xF800,0xFFFF,0xFFFF,0xFFFF,0xF800,0xF800,0xF800,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF

  
};


//初期化
void setup() {

  //ディスプレイの初期化
  display.begin();


  //表示方向
  display.setRotation(0);


  // 画面表示をクリア
  display.clearDisplay();


  //ビットマップの表示
  display.drawRGBBitmap(3, 3, databytes, 16,16);

//  void drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w,
//                     int16_t h);
//  void drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w,
//                     int16_t h);
//  void drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[],
//                     const uint8_t mask[], int16_t w, int16_t h);
//  void drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, uint8_t *mask,
//                     int16_t w, int16_t h);


  // 描画バッファの内容を画面に表示
  display.display();

}//setup


//メインループ
void loop() {
}//loop


```

hh.cpp


```c++



//インクルド
#include "hh.h"
#include <Adafruit_GFX.h>


//定義
///< No-temp-var swap operation
#define NA_ST7735S_P_swap(a, b)        \
  (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))

//GPIO
#define GPIO1(s) digitalWrite(1,s)  //RESET
#define GPIO2(s) digitalWrite(2,s)  //A0
#define GPIO3(s) digitalWrite(3,s)  //WR
#define GPIO4(s) digitalWrite(4,s)  //RD


//データの出力
void NA_ST7735S_P::GPIO_8BIT(uint8_t s)
{
  digitalWrite(12, (s >> 7) & 0x01);
  digitalWrite(11, (s >> 6) & 0x01);
  digitalWrite(10, (s >> 5) & 0x01);
  digitalWrite( 9, (s >> 4) & 0x01);
  digitalWrite( 8, (s >> 3) & 0x01);
  digitalWrite( 7, (s >> 2) & 0x01);
  digitalWrite( 6, (s >> 1) & 0x01);
  digitalWrite( 5,  s       & 0x01);
} //GPIO_8BIT


//コマンドの書き込み
void NA_ST7735S_P::LCD_Write_CMD(uint8_t a)
{

  GPIO2(0); //A0=0;
  GPIO_8BIT(a);//P1=a; data
  GPIO3(0);//WRB=0;
  GPIO3(1);//WRB=1;

} //LCD_Write_CMD


//データ書き込み
void NA_ST7735S_P::LCD_Write_Data(uint8_t a)
{

  GPIO2(1);//A0=1;
  GPIO_8BIT(a);//P1=a; data
  GPIO3(0);//WRB=0;
  GPIO3(1);//WRB=1;

} //LCD_Write_Data


//液晶の初期化処理
void NA_ST7735S_P::ST7735S_P_Init(void)
{

  //----------  ST7735S Reset Sequence  --------//

  GPIO1(1);//LCD_RESET=1;

  delay(1); //Delay 1ms

  GPIO1(0);//LCD_RESET=0;

  delay(1); //Delay 1ms

  GPIO1(1);//LCD_RESET=1;

  delay(120); //Delay 120ms

  //----------  ST7735S Reset Sequence  --------//


  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);

} //ST7735S_P_Init


NA_ST7735S_P::NA_ST7735S_P(uint8_t w, uint8_t h)
  : Adafruit_GFX(w, h), buffer(NULL)

{
}


//バッファのクリア
NA_ST7735S_P::~NA_ST7735S_P(void) {
  if (buffer) {
    free(buffer);
    buffer = NULL;
  }
}//~NA_ST7735S_P


//初期処理
bool NA_ST7735S_P::begin(void) {

  if ((!buffer) && !(buffer = (uint16_t *)malloc(WIDTH * HEIGHT * 2)))
    return false;

  //バッファーのクリア
  clearDisplay();

  //ポートのモード設定
  //アウトプットモード
  pinMode(1, OUTPUT); //REST
  pinMode(2, OUTPUT); //RS
  pinMode(3, OUTPUT); //WR
  pinMode(4, OUTPUT); //RD

  pinMode(5, OUTPUT); //DB0
  pinMode(6, OUTPUT); //DB1
  pinMode(7, OUTPUT); //DB2
  pinMode(8, OUTPUT); //DB3

  pinMode( 9, OUTPUT); //DB4
  pinMode(10, OUTPUT); //DB5
  pinMode(11, OUTPUT); //DB6
  pinMode(12, OUTPUT); //DB7

  //ポートの初期化
  GPIO4(1);//RD=1
  GPIO3(0);//WR=0
  GPIO2(0);//RS=0
  GPIO1(1);//RESET=1

  //液晶の初期化処理
  ST7735S_P_Init();

  return true; // Success
}//begin


//点の表示
void NA_ST7735S_P::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:
        NA_ST7735S_P_swap(x, y);
        x = WIDTH - x - 1;
        break;
      case 2:
        x = WIDTH - x - 1;
        y = HEIGHT - y - 1;
        break;
      case 3:
        NA_ST7735S_P_swap(x, y);
        y = HEIGHT - y - 1;
        break;
    }//end switch

    //ドットのカラーの設定
    buffer[ (y << 7) | x ] = color;

  }//if
}//drawPixel


//バッファのクリア
void NA_ST7735S_P::clearDisplay(void) {
  memset(buffer, 0, WIDTH * HEIGHT * 2 );
}


bool NA_ST7735S_P::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:
        NA_ST7735S_P_swap(x, y);
        x = WIDTH - x - 1;
        break;
      case 2:
        x = WIDTH - x - 1;
        y = HEIGHT - y - 1;
        break;
      case 3:
        NA_ST7735S_P_swap(x, y);
        y = HEIGHT - y - 1;
        break;
    }
    return (buffer[ (y << 7) | x ] );
  }
  return false; // Pixel out of bounds
}


uint16_t *NA_ST7735S_P::getBuffer(void) {
  return buffer;
}


//画面のリフレッシュ
void NA_ST7735S_P::display(void) {

  //画面の書き込み開始
  LCD_Write_CMD(0x2C); //memory write
  delay(1);

  int vg;
  for (int i = 0; i < (HEIGHT * WIDTH); i++) {
    vg = buffer[i];
    //---------------- 1バイト目
    LCD_Write_Data((vg >> 7) & 0xff);
    //---------------- 2バイト目
    LCD_Write_Data(       vg & 0xff);
  }//for i

}//display



```

hh.h

```c++



#ifndef _NA_ST7735S_P_H_
#define _NA_ST7735S_P_H_

#include <Adafruit_GFX.h>

#ifndef NO_ADAFRUIT_NA_ST7735S_P_COLOR_COMPATIBILITY
#define BLACK NA_ST7735S_P_BLACK     ///< Draw 'off' pixels
#define WHITE NA_ST7735S_P_WHITE     ///< Draw 'on' pixels
#define RED   NA_ST7735S_P_RED       ///< Draw red
#endif
/// fit into the ST7735S_P_ naming scheme
#define NA_ST7735S_P_BLACK 0x0000           ///< Draw 'off' pixels
#define NA_ST7735S_P_WHITE 0xffff           ///< Draw 'on' pixels
#define NA_ST7735S_P_RED 0b1111100000000000 // < Draw red

///< Draw 'on' pixels
//#define NA_ST7735S_P_INVERSE 2 ///< Invert pixels


class NA_ST7735S_P : public Adafruit_GFX {
  public:

    NA_ST7735S_P(uint8_t w, uint8_t h);

    ~NA_ST7735S_P(void);

    bool begin(void);
    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);
    uint16_t *getBuffer(void);

    void GPIO_8BIT(uint8_t s);
    void LCD_Write_CMD(uint8_t ww);
    void LCD_Write_Data(uint8_t ii);
    void ST7735S_P_Init(void);


  protected:

    uint16_t *buffer; ///< Buffer data used for display buffer. Allocated when
                      ///< begin method is called.
 
};

#endif // _NA_ST7735S_P_H_



```