#include #include #define CMD 2 //コマンド取得の割り込みに使用 #define DISP 3 //画面表示データ取得の割り込みに使用 #define DATA 4 //コマンド or 画面表示データ #define DEVIDE 1023 #define RANGE 1.5 Adafruit_SSD1306 display(128, 64, &Wire, -1); static volatile uint8_t command, value; static volatile bool comp, exist; char chdata; int coordx[] = {0, 0, 1, 1, 2, 3, 5, 7, 8, 11, 13, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 59, 61, 64, 65, 67, 69, 70, 71, 71, 72, 72}; int coordy[] = {48, 45, 42, 39, 36, 33, 30, 27, 25, 23, 20, 19, 17, 15, 14, 13, 13, 12, 12, 12, 13, 13, 14, 15, 17, 19, 20, 23, 25, 27, 30, 33, 36, 39, 42, 45, 48}; int coordix[] = {8, 12, 22, 36, 50, 60, 64}; int coordiy[] = {48, 34, 24, 20, 24, 34, 48}; void setup() { pinMode(CMD, INPUT_PULLUP); pinMode(DISP, INPUT_PULLUP); pinMode(DATA, INPUT); Serial.begin(9600); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); command = 0; exist = false; chdata = 0; attachInterrupt(digitalPinToInterrupt(CMD), SetCommand, FALLING); attachInterrupt(digitalPinToInterrupt(DISP), GetData, FALLING); } void loop() { switch(command) { case 0: DebugPrint(); break; case 1: DegugPrintInter(); break; case 2: Voltmeter(); break; default: ShowNo(); break; } } void InitCommand(uint8_t icom) { switch(icom) { case 0: case 1: void DebugPrintInit(); break; case 2: VoltmeterInit(); break; } } void SetCommand() { static volatile uint8_t datain = 0, bitCount = 0; datain |= ((uint8_t)digitalRead(DATA) << bitCount); if(++bitCount > 7) { if(command != datain) { InitCommand(datain); command = datain; } bitCount = 0; datain = 0; } } void GetData() { static volatile uint8_t datain = 0, bitCount = 0; datain |= ((uint8_t)digitalRead(DATA) << bitCount); if(++bitCount > 7) { value = datain; bitCount = 0; datain = 0; exist = true; } } void DebugPrintInit() { display.clearDisplay(); display.setTextSize(1); display.setCursor(0, 0); } void DebugPrint() { if(Serial.available()) { chdata = Serial.read(); if(chdata == 0x0A) display.display(); //これがないと8行目一番最後の次の文字が0,0に表示されない if(((display.getCursorY() == 56) && (display.getCursorX() > 125)) || (display.getCursorY() > 56)) { display.display(); delay(2000); //縦スクロールがないので全面書き換え display.clearDisplay(); display.setCursor(0, 0); } display.write(chdata); } } void DegugPrintInter() { if(exist) { if(value == 0x0A) display.display(); //これがないと8行目一番最後の次の文字が0,0に表示されない if(((display.getCursorY() == 56) && (display.getCursorX() > 125)) || (display.getCursorY() > 56)) { display.display(); delay(2000); //縦スクロールがないので全面書き換え display.clearDisplay(); display.setCursor(0, 0); } display.write(value); exist = false; } } void VoltmeterInit() { display.setTextSize(2); } void Voltmeter() { float volt; uint8_t n; if(exist) { exist = false; volt = value << 8; exist = false; while(!exist); volt += value; volt *= 9.4; //3.29 * (300 / 100)から調整 //volt *= 15; //5 * (300 / 100) volt /= DEVIDE; display.clearDisplay(); display.drawCircle(36, 48, 36, WHITE); for(int i=0; i<7; i++) display.drawLine(coordx[i * 6], coordy[i * 6], coordix[i], coordiy[i], WHITE); n = 36 * volt / RANGE; if(n > 36) n = 36; display.drawLine(36, 48, coordx[n], coordy[n], WHITE); display.setCursor(80, 32); display.print(volt); display.display(); exist = false; } } void ShowNo() { display.clearDisplay(); display.setCursor(0, 0); display.print("Command : "); display.print(command); display.display(); }