(SSD1306)4chサウンドオシロスコープ(M5Stamp S3)(入力ランダムウォーク)(折れ線グラフ風)


x かころぐをみよ

![o_coq211.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/bbc7908b-34c0-a037-d03f-ef2d2ac5b0db.jpeg)



```c++



//秋月のOLEDとアイテンドウのOLEDのアドレスは3C
//ssd1306_Sound_Oscilloscope_point4_S3_1


//ヘッダー
#include <Arduino.h>
#include <Wire.h>


//定義
#define MAX_PAGE                   (7)
#define MAX_COL                    (127)

#define COMMAND_MODE               0x80 // continuation bit is set!
#define DATA_MODE                  0x40

#define SET_COLUMN_ADDRESS         0x21 // takes two bytes, start address and end address of display data RAM
#define SET_PAGE_ADDRESS           0x22 // takes two bytes, start address and end address of display data RAM

#define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte as given above
#define HORIZONTAL_ADDRESSING_MODE 0x00


//I2Cに配列を転送する
void write_s(uint8_t *str1, uint8_t len1) {

  Wire.beginTransmission(  0x3c  );

  for (int ii = 0; ii < len1; ii++) {

    //一文字出力
    Wire.write(*str1 ++);

  }//for

  Wire.endTransmission();

}//write_s


//セットページアドレス
void setPageAddress(uint8_t start, uint8_t end)
{
  uint8_t databytes[6] = {COMMAND_MODE, SET_PAGE_ADDRESS, COMMAND_MODE, start, COMMAND_MODE, end};
  write_s(databytes, 6);
}//setPageAddress


//セットカラムアクセス
void setColumnAddress(uint8_t start, uint8_t end)
{
  uint8_t databytes[6] = {COMMAND_MODE, SET_COLUMN_ADDRESS, COMMAND_MODE, start, COMMAND_MODE, end};
  write_s(databytes, 6);
}//setColumnAddress


//セットメモリーアドレシングモード
void setMemoryAddressingMode()
{
  uint8_t databytes[4] = {COMMAND_MODE, SET_MEMORY_ADDRESSING_MODE, COMMAND_MODE, HORIZONTAL_ADDRESSING_MODE};
  write_s(databytes, 4);
}//setMemoryAddressingMode


//座標
int SO_X = 0;
int SO_Y = 0;

//8ドット分のデータ
char dot_1ch[8];
char dot_2ch[8];
char dot_3ch[8];
char dot_4ch[8];

//ブロックカウント
char b_count = 0;

//パターンRAMの内容を液晶に転送
void Sound_Oscilloscope(int L1, int L2, int L3, int L4) {

  if (SO_Y >= 64) {

    //範囲の設定 (OLED内部のx,yカウンターを初期化してホームポジション0,0に)
    setPageAddress(0, MAX_PAGE);  // all pages
    setColumnAddress(0, MAX_COL); // all columns

    SO_Y = 0;

    //while(1){} //debug
  }//end if

  dot_1ch[b_count] = L1;
  dot_2ch[b_count] = L2;
  dot_3ch[b_count] = L3;
  dot_4ch[b_count] = L4;

  b_count++;
  //b_count = 7; //debug
  if (b_count > 7 ) {

    //L1 L1 L1 vL1 L1 L1 L1 L1 L1 
    int a; //一時
    //データの配列の定義
    uint8_t databytes[2] = {DATA_MODE, 0x00};
    for (int x = 0; x < 32; x++) {

      //dot8[0]=0;dot8[1]=8;dot8[2]=16;dot8[3]=32;
      //dot8[4]=0;dot8[5]=9;dot8[6]=16;dot8[7]=32;
      a = 0;
      if(dot_1ch[0] == x ) {a = a | 0b00000001;}
      if(dot_1ch[1] == x ) {a = a | 0b00000010;}
      if(dot_1ch[2] == x ) {a = a | 0b00000100;}
      if(dot_1ch[3] == x ) {a = a | 0b00001000;}
      if(dot_1ch[4] == x ) {a = a | 0b00010000;}
      if(dot_1ch[5] == x ) {a = a | 0b00100000;}
      if(dot_1ch[6] == x ) {a = a | 0b01000000;}
      if(dot_1ch[7] == x ) {a = a | 0b10000000;}
      
      databytes[1] = a;
      write_s(databytes, 2);

      b_count = 0;
    }//for x

    //L2 L2 L2 L2 L2 L2 L2 L2 L2 
    for (int x = 0; x < 32; x++) {

      //dot8[0]=0;dot8[1]=8;dot8[2]=16;dot8[3]=32;
      //dot8[4]=0;dot8[5]=9;dot8[6]=16;dot8[7]=32;
      a = 0;
      if(dot_2ch[0] == x ) {a = a | 0b00000001;}
      if(dot_2ch[1] == x ) {a = a | 0b00000010;}
      if(dot_2ch[2] == x ) {a = a | 0b00000100;}
      if(dot_2ch[3] == x ) {a = a | 0b00001000;}
      if(dot_2ch[4] == x ) {a = a | 0b00010000;}
      if(dot_2ch[5] == x ) {a = a | 0b00100000;}
      if(dot_2ch[6] == x ) {a = a | 0b01000000;}
      if(dot_2ch[7] == x ) {a = a | 0b10000000;}
      
      databytes[1] = a;
      write_s(databytes, 2);

      b_count = 0;
    }//for x
    
    //L3 L3 L3 L3 L3 L3 L3 L3 L3 L3 L3 L3 L3 L3 L3 
    for (int x = 0; x < 32; x++) {

      //dot8[0]=0;dot8[1]=8;dot8[2]=16;dot8[3]=32;
      //dot8[4]=0;dot8[5]=9;dot8[6]=16;dot8[7]=32;
      a = 0;
      if(dot_3ch[0] == x ) {a = a | 0b00000001;}
      if(dot_3ch[1] == x ) {a = a | 0b00000010;}
      if(dot_3ch[2] == x ) {a = a | 0b00000100;}
      if(dot_3ch[3] == x ) {a = a | 0b00001000;}
      if(dot_3ch[4] == x ) {a = a | 0b00010000;}
      if(dot_3ch[5] == x ) {a = a | 0b00100000;}
      if(dot_3ch[6] == x ) {a = a | 0b01000000;}
      if(dot_3ch[7] == x ) {a = a | 0b10000000;}
      
      databytes[1] = a;
      write_s(databytes, 2);

      b_count = 0;
    }//for x

    //L4 L4 L4 L4 L4 L4 L4 L4 L4 L4 L4 L4 L4 L4 L4 
    for (int x = 0; x < 32; x++) {

      //dot8[0]=0;dot8[1]=8;dot8[2]=16;dot8[3]=32;
      //dot8[4]=0;dot8[5]=9;dot8[6]=16;dot8[7]=32;
      a = 0;
      if(dot_4ch[0] == x ) {a = a | 0b00000001;}
      if(dot_4ch[1] == x ) {a = a | 0b00000010;}
      if(dot_4ch[2] == x ) {a = a | 0b00000100;}
      if(dot_4ch[3] == x ) {a = a | 0b00001000;}
      if(dot_4ch[4] == x ) {a = a | 0b00010000;}
      if(dot_4ch[5] == x ) {a = a | 0b00100000;}
      if(dot_4ch[6] == x ) {a = a | 0b01000000;}
      if(dot_4ch[7] == x ) {a = a | 0b10000000;}
      
      databytes[1] = a;
      write_s(databytes, 2);

      b_count = 0;
    }//for x


  }//end if b_count

  SO_Y++;

}//Sound_Oscilloscope


//SSD1306の初期化
void display_begin(void) {

  //I2Cの初期化
  Wire.begin(); //C011
  delay(200);

  //SSD1306の初期化スペル(魔法)
  //0x80,0x8D,0x80,0x14,0x80,0xAF
  write_s( (uint8_t*) "\200\215\200\024\200\257", 6);
  delay(100);

  //セットメモリーアドレシングモード (画面の終端に来たら画面の先頭に)
  setMemoryAddressingMode();

  //範囲の設定 (OLED内部のx,yカウンターを初期化してホームポジション0,0に)
  setPageAddress(0, MAX_PAGE);  // all pages
  setColumnAddress(0, MAX_COL); // all columns

}//display_begin


//初期化
void setup() {

  //SSD1306の初期化
  display_begin();

}//setup


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

  //L1 L2 L1 L2 L1 L2 L1 L2 L1 L2 L1 L2 L1 L2 L1 L2 L1 L2 

  static int L1 = 0;
  L1 = L1 + ( random(16) - 8 );
  if ( L1 < 0 ) {L1 = 0;} else if ( L1 > 32 ) {L1 = 32;}
  //L1 = 32;

  static int L2 = 0;
  L2 = L2 + ( random(16) - 8 );
  if( L2 < 0 )  {L2 = 0;} else if ( L2 > 32 ) {L2 = 32;}
  // //L1 = 32;

  //L3 L4 L3 L4 L3 L4 L3 L4 L3 L4 L3 L4 L3 L4 L3 L4 L3 L4 

  static int L3 = 0;
  L3 = L3 + ( random(16) - 8 );
  if ( L3 < 0 ) {L3 = 0;} else if ( L3 > 32 ) {L3 = 32;}
  //L1 = 32;

  static int L4 = 0;
  L4 = L4 + ( random(16) - 8 );
  if( L4 < 0 )  {L4 = 0;} else if ( L4 > 32 ) {L4 = 32;}
  // //L1 = 32;

  Sound_Oscilloscope(L1, L2, L3, L4); //画面の再表示

  //delay(1000); //1秒待つ debug

}//loop


```