Skip to content

Instantly share code, notes, and snippets.

@mongonta0716
Created October 23, 2023 14:40
Show Gist options
  • Save mongonta0716/cc957be008a253e6dfadce7fea0bb34f to your computer and use it in GitHub Desktop.
Save mongonta0716/cc957be008a253e6dfadce7fea0bb34f to your computer and use it in GitHub Desktop.
M5Dial同士でI2C通信するサンプル
#include <Arduino.h>
#include <M5Dial.h>
#include "Wire.h"
#define I2C_DEV_ADDR 0x55
uint32_t i = 0;
void setup() {
auto cfg = M5.config();
cfg.output_power = true;
M5Dial.begin(cfg, true, true);
M5.Display.setTextSize(2);
M5.Display.drawString("I2C master", 30, 20, &fonts::efontCN_16_b);
Wire.begin(15, 13, 400000);
//Write message to the slave
Wire.beginTransmission(I2C_DEV_ADDR);
Wire.printf("S", i++);
uint8_t error = Wire.endTransmission(true);
Serial.printf("endTransmission: %u\n", error);
}
long oldPosition = -999;
long newPosition = 0;
void loop() {
M5Dial.update();
newPosition = M5Dial.Encoder.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Wire.beginTransmission(I2C_DEV_ADDR);
Wire.print(newPosition);
M5.Display.setCursor(100, 100);
M5.Display.printf("%d ", newPosition);
uint8_t error = Wire.endTransmission(true);
Serial.printf("endTransmission: %u\n", error);
}
}
#include <Arduino.h>
#include <M5Dial.h>
#include "Wire.h"
#define I2C_DEV_ADDR 0x55
uint32_t i = 0;
void onRequest(){
Wire.print(i++);
Wire.print(" Packets.");
M5_LOGI("onRequest");
}
void onReceive(int len){
uint8_t buff[20];
M5_LOGI("onReceive[%d]: ", len);
while(Wire.available()){
Wire.readBytes(buff, len);
M5.Display.setCursor(100, 100);
for (int i=0; i< len; i++) {
M5.Log.printf("%d", buff[i]);
M5.Display.printf("%c", buff[i]);
}
}
M5.Display.print(" ");
M5.Log.println();
}
void setup() {
M5Dial.begin();
M5.Display.setRotation(1);
M5.Display.setTextSize(2);
M5.Display.setCursor(70, 20);
M5.Display.println("I2C Slave");
Wire.onReceive(onReceive);
Wire.onRequest(onRequest);
Wire.begin((uint8_t)I2C_DEV_ADDR, 15, 13, 400000);
#if CONFIG_IDF_TARGET_ESP32
char message[64];
snprintf(message, 64, "%lu Packets.", i++);
Wire.slaveWrite((uint8_t *)message, strlen(message));
#endif
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment