-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardputer_i2c_scan.ino
68 lines (59 loc) · 1.75 KB
/
cardputer_i2c_scan.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @file ir_nec.ino
* @author Felix Mann (djflix@gmail.com)
* @brief M5Cardputer I2C scanner
* @version 1.0.0
* @date 2024-05-19
*
* @Hardwares: M5Cardputer
* @Platform Version: Arduino M5Stack Board Manager v2.0.7
* @Dependent Library:
* M5GFX: https://github.com/m5stack/M5GFX
* M5Unified: https://github.com/m5stack/M5Unified
*/
#include <Wire.h>
#include "M5Cardputer.h"
#define DEV_COUNT_X_OFFSET 72
#define DEV_COUNT_Y_OFFSET 0
#define ADDRESS_WIDTH_PIXELS 24
#define ADDRESS_Y_OFFSET_PX 10
void setup() {
auto cfg = M5.config();
M5.begin();
Wire.begin(G2, G1);
M5Cardputer.begin(cfg, true);
M5Cardputer.Display.setRotation(1);
M5Cardputer.Display.setTextColor(MAGENTA);
M5Cardputer.Display.setTextFont(1);
M5Cardputer.Display.setTextSize(1);
}
struct addrPos {
int x;
int y;
};
addrPos getAddressPrintPosition(int deviceCount);
addrPos getAddressPrintPosition(int deviceCount) {
int x = (ADDRESS_WIDTH_PIXELS * deviceCount) % M5Cardputer.Display.width();
if (x < ADDRESS_WIDTH_PIXELS) {
x = 0;
}
int y = 10 * (((ADDRESS_WIDTH_PIXELS * deviceCount) - x) / M5Cardputer.Display.width());
return { x, y + ADDRESS_Y_OFFSET_PX };
};
void loop() {
M5Cardputer.update();
int deviceCount = 0;
delay(2500);
M5Cardputer.Display.clear();
M5Cardputer.Display.drawString("I2C Scanner", 0, 0);
byte error, address;
for (address = 0x01; address < 0x7f; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
addrPos pos = getAddressPrintPosition(deviceCount++);
M5Cardputer.Display.drawString(String(address, HEX), pos.x, pos.y);
}
}
M5Cardputer.Display.drawString("total: " + String(deviceCount) + " I2C devices", DEV_COUNT_X_OFFSET, DEV_COUNT_Y_OFFSET);
}