0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

小マイコンでST7735S Pを使いAdafruit_を都合よく改造した。(ラインバッファー方式)

Last updated at Posted at 2023-11-09

目的
小マイコンSRAM約8キロバイトを
使い、2000ドットまで表示する。
パラレルインターフェース

●結果

o_cop767.jpg

●プログラム

main.cpp



//OLED_SSD1306_BITMAP_DV67_linebuf_SPI_35P_UNO


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

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


// ビットマップデータ
uint8_t databytes[8] =
{

  0b01100110,
  0b10101111,
  0b10111111,
  0b11011111,

  0b01111110,
  0b01111110,
  0b00111100,
  0b00011000

};


//初期化
void setup() {

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

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

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

  //ビットマップの表示
  display.drawBitmap(3, 3, databytes, 8, 8,  RED);

  // 画面の左側に長方形(塗りつぶしなし)を描画
  // display.drawRect(左上x, 左上y, 幅, 高さ, 線の色)
  display.drawRect(0, 0, 127, 159, WHITE);

  // テキストサイズを設定
  display.setTextSize(2);
  // テキスト色を設定
  display.setTextColor(WHITE);
  //display.setTextColor(RED);
  // テキストの開始位置を設定
  display.setCursor(0, 30);

  // 1行目に46を表示
  display.println("linebuf");
  display.println("linebuf");
  display.println("linebuf");
  display.println(display.getD_ID());
  
  display.setTextColor(RED);
  display.println("liuebuf");

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


/*
  delay(3000); //1秒待つ

unsigned long p_time;
p_time = millis();

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

  //ビットマップの表示
  display.drawBitmap(10, 10, databytes, 8, 8,  RED);

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

p_time = millis() - p_time;

  delay(2000); //1秒待つ

  // 画面表示をクリア
  display.clearDisplay();  
  // テキストの開始位置を設定
  display.setCursor(0, 0);

  // 1行目を表示
  display.println();
  display.print((int)p_time);
  display.println("ms");

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

}//setup


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



hh.cpp

hh.cpp



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

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

///*
//GPIOの設定1 開始

//GPIO
#define GPIO_A0_P A0
#define GPIO_A1_P A1
#define GPIO_A2_P A2
#define GPIO_A3_P A3

//GPIO
#define GPIO_A0(s) digitalWrite(GPIO_A0_P,s)
#define GPIO_A1(s) digitalWrite(GPIO_A1_P,s)
#define GPIO_A2(s) digitalWrite(GPIO_A2_P,s)
#define GPIO_A3(s) digitalWrite(GPIO_A3_P,s)

#define GPIO_RD_P     GPIO_A3_P //RD=1
#define GPIO_WR_P     GPIO_A2_P //WR=1
#define GPIO_RS_P     GPIO_A1_P //RS=0
#define GPIO_RESET_P  GPIO_A0_P //RESET=1


#define GPIO_RD(y)     GPIO_A3(y) //RD=1
#define GPIO_WR(y)     GPIO_A2(y) //WR=1
#define GPIO_RS(y)     GPIO_A1(y) //RS=0
#define GPIO_RESET(y)  GPIO_A0(y) //RESET=1


#define GPIO_DB7  7
#define GPIO_DB6  6
#define GPIO_DB5  5
#define GPIO_DB4  4
#define GPIO_DB3  3
#define GPIO_DB2  2
#define GPIO_DB1  A5
#define GPIO_DB0  A4

//GPIOの設定1 終了
//*/


void NA_ST7735_P::GPIO_8BIT(uint8_t s)
{
  digitalWrite( GPIO_DB7, (s >> 7) & 1);
  digitalWrite( GPIO_DB6, (s >> 6) & 1);
  digitalWrite( GPIO_DB5, (s >> 5) & 1);
  digitalWrite( GPIO_DB4, (s >> 4) & 1);
  digitalWrite( GPIO_DB3, (s >> 3) & 1);
  digitalWrite( GPIO_DB2, (s >> 2) & 1);
  digitalWrite( GPIO_DB1, (s >> 1) & 1);
  digitalWrite( GPIO_DB0, s & 1);
} //GPIO_8BIT


//コマンドの書き込み
void NA_ST7735_P::LCD_Write_CMD(uint8_t a)
{
  GPIO_RS(0); //A0=0;
  GPIO_8BIT(a);//P1=a; data
  GPIO_WR(0);//WRB=0;
  GPIO_WR(1);//WRB=1;
} //LCD_Write_CMD


//データ書き込み
void NA_ST7735_P::LCD_Write_Data(uint8_t a)
{
  GPIO_RS(1);//A0=1;
  GPIO_8BIT(a);//P1=a; data
  GPIO_WR(0);//WRB=0;
  GPIO_WR(1);//WRB=1;
} //LCD_Write_Data


//液晶の初期化処理
void NA_ST7735_P::TXDT144TF_ST7735S_Init(void)
{

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

  GPIO_RESET(1);//LCD_RESET=1;
  delay(1); //Delay 1ms

  GPIO_RESET(0);//LCD_RESET=0;
  delay(1); //Delay 1ms

  GPIO_RESET(1);//LCD_RESET=1;
  delay(120); //Delay 120ms

  LCD_Write_CMD(0x01);//SOFTWARE RESET
  delay(50);

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

} //TXDT144TF_ST7735S_Init


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

{
}


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


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

  if ((!buffer) && !(buffer = (uint8_t *)malloc(MAX_D_ID * 4)   ))
    return false;

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

  //ポートのモード設定
  //アウトプットモード
  pinMode(GPIO_DB7, OUTPUT);
  pinMode(GPIO_DB6, OUTPUT);
  pinMode(GPIO_DB5, OUTPUT);
  pinMode(GPIO_DB4, OUTPUT);
  pinMode(GPIO_DB3, OUTPUT);
  pinMode(GPIO_DB2, OUTPUT);

  pinMode(GPIO_RESET_P, OUTPUT);
  pinMode(GPIO_RS_P, OUTPUT);
  pinMode(GPIO_WR_P, OUTPUT);
  pinMode(GPIO_RD_P, OUTPUT);
  pinMode(GPIO_DB0, OUTPUT);
  pinMode(GPIO_DB1, OUTPUT);

  //ポートの初期化
  GPIO_RD(1);//RD=1
  GPIO_WR(1);//WR=1
  GPIO_RS(0);//RS=0
  GPIO_RESET(1);//RESET=1

  delay(500); //0.5秒待つ

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

  //画面の書き込み開始
  //display();

  return true; // Success
}//begin


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


    //ドットアイテムのカラーの設定
    //printf("%d(%d,%d)=%x \n",D_ID,x,y,color);
    buffer[ D_ID * 4 + 0] = y;
    buffer[ D_ID * 4 + 1] = x;
    buffer[ D_ID * 4 + 2] = (color >> 8) & 0xff;
    buffer[ D_ID * 4 + 3] =  color       & 0xff;

    if (D_ID < (MAX_D_ID - 2)) {
      D_ID++;
    }//end if


  }//if
}//drawPixel

uint16_t NA_ST7735_P::getD_ID(void) {
  return D_ID;
}


//バッファのクリア
void NA_ST7735_P::clearDisplay(void) {
  memset(buffer, 0xff, MAX_D_ID * 4 );
  D_ID=0;
}


bool NA_ST7735_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_ST7735_P_swap(x, y);
        x = WIDTH - x - 1;
        break;
      case 2:
        x = WIDTH - x - 1;
        y = HEIGHT - y - 1;
        break;
      case 3:
        NA_ST7735_P_swap(x, y);
        y = HEIGHT - y - 1;
        break;
    }
    return ( 0xff );
  }
  return false; // Pixel out of bounds
}


uint8_t *NA_ST7735_P::getBuffer(void) {
  return buffer;
}


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

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

  //画面の表示
  int     kk;
  int     fy,fx,fh,fl;
  uint8_t linebuf[WIDTH * 2];

  for (int y = 0; y < HEIGHT; y++) {

    // y
    //ラインバッファの初期化
    for (int ii = 0; ii < (WIDTH * 2); ii++) {
      linebuf[ii] = 0;
    }
    kk = 0;
    //無限ループ
    while (1) { //直線検索(全件検索)
      fy = buffer[kk * 4 + 0];
      //fx = buffer[kk*4 + 1];
      //fh = buffer[kk*4 + 2];
      //fl = buffer[kk*4 + 3];
      if (fy == 0xff ) {break;}

      if (fy == y) {

        //fy = buffer[kk*4 + 0];
        fx = buffer[kk * 4 + 1];
        fh = buffer[kk * 4 + 2];
        fl = buffer[kk * 4 + 3];

        linebuf[ fx * 2 + 0] = fh;
        linebuf[ fx * 2 + 1] = fl;

      }//end if fy

      kk++;

    }//while


    for (int x = 0; x < WIDTH; x++) {

      //---------------- 1バイト目
      LCD_Write_Data(linebuf[ x * 2 + 0]);

      //---------------- 2バイト目
      LCD_Write_Data(linebuf[ x * 2 + 1]);

    }//x
  }//y

}//display



hh.h

hh.h



#ifndef _NA_ST7735_P_H_
#define _NA_ST7735_P_H_

#include <Adafruit_GFX.h>

#ifndef NO_ADAFRUIT_NA_ST7735_P_COLOR_COMPATIBILITY
#define BLACK NA_ST7735_P_BLACK     ///< Draw 'off' pixels
#define WHITE NA_ST7735_P_WHITE     ///< Draw 'on' pixels
#define RED NA_ST7735_P_RED     ///< Draw 'on' pixels
#endif
/// fit into the SSD1306_ naming scheme
#define NA_ST7735_P_BLACK 0x0000   ///< Draw 'off' pixels
#define NA_ST7735_P_WHITE 0xffff   ///< Draw 'on' pixels
#define NA_ST7735_P_RED 0b1111100000000000 //red


class NA_ST7735_P : public Adafruit_GFX {
  public:

    NA_ST7735_P(uint8_t w, uint8_t h);

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

    uint16_t getD_ID(void);

  protected:


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

    uint16_t D_ID = 0;

};

#endif // _NA_ST7735_P_H_



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?