Skip to content

Commit

Permalink
feat: add i2c scanner block (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben authored Mar 8, 2024
1 parent 27e7eae commit 65d1b2f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
7 changes: 7 additions & 0 deletions blocks/leaphy_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ function getBlocks(board) {
tooltip: "%{BKY_USE_I2C_CHANNEL_TOOLTIP}",
helpUrl: "",
},
{
type: "i2c_list_devices",
message0: "%{BKY_I2C_LIST_DEVICES}",
previousStatement: null,
nextStatement: null,
style: "leaphy_blocks",
},
{
type: "leaphy_tof_get_distance",
message0: "%%{BKY_LEAPHY_TOF_GET_DISTANCE}",
Expand Down
53 changes: 53 additions & 0 deletions generators/arduino/leaphy_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,59 @@ function getCodeGenerators(Arduino) {

return code;
};

Arduino.forBlock["i2c_list_devices"] = function (block) {
const LIST_DEVICES =
"void i2cListDevices() {\n" +
" for (int channel = 0; channel < 8; channel++) {\n" +
' Serial.print("Scanning channel ");\n' +
" Serial.print(channel);\n" +
' Serial.println(":");\n' +
" \n" +
" i2cSelectChannel(channel);\n" +
" \n" +
" for (DeviceAddress address : deviceMap) {\n" +
" Wire.beginTransmission(address.address);\n" +
" int error = Wire.endTransmission();\n" +
" \n" +
" if (error == 0) {\n" +
' Serial.print("Found: ");\n' +
" Serial.print(address.device);\n" +
' Serial.print(" at address 0x");\n' +
" \n" +
" if (address.address < 16) {\n" +
' Serial.print("0");\n' +
" }\n" +
" Serial.println(address.address, HEX);\n" +
" }\n" +
" }\n" +
" \n" +
" i2cRestoreChannel();\n" +
" }\n" +
"}\n";

const DEVICE_CHANNEL_MAP =
"struct DeviceAddress { \n" +
" uint8_t address;\n" +
" char* device;\n" +
"};\n" +
"\n" +
"DeviceAddress deviceMap[] = {\n" +
' {0x0D, "Compass"},\n' +
' {0x29, "Color Sensor / ToF Sensor"},\n' +
' {0x39, "RGB + Gesture Sensor"},\n' +
' {0x3C, "Screen"},\n' +
' {0x58, "Gas Sensor"},\n' +
' {0x76, "Air Pressure Sensor"}\n' +
"};\n";

Arduino.addSetup("serial", "Serial.begin(115200);", false);
addI2CDeclarations();
Arduino.addInclude("i2c_device_map", DEVICE_CHANNEL_MAP);
Arduino.addDeclaration("i2c_list_devices", LIST_DEVICES);

return "i2cListDevices();\n";
};
}

export default getCodeGenerators;
1 change: 1 addition & 0 deletions msg/js/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,6 @@ Blockly.Msg["COPY_ALL_TO_BACKPACK"] = "Copy all blocks to backpack";
Blockly.Msg["PASTE_ALL_FROM_BACKPACK"] = "Paste all blocks from backpack";
Blockly.Msg["USE_I2C_CHANNEL"] = "Use I2C channel";
Blockly.Msg["USE_I2C_CHANNEL_TOOLTIP"] = "Use the selected I2C channel for sensors in this block";
Blockly.Msg["I2C_LIST_DEVICES"] = "List connected I2C devices";

export default Blockly.Msg;
1 change: 1 addition & 0 deletions msg/js/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,5 +527,6 @@ Blockly.Msg["COPY_ALL_TO_BACKPACK"] = "Kopieer alles naar rugzak";
Blockly.Msg["PASTE_ALL_FROM_BACKPACK"] = "Plak alles uit rugzak";
Blockly.Msg["USE_I2C_CHANNEL"] = "Gebruik I2C kanaal";
Blockly.Msg["USE_I2C_CHANNEL_TOOLTIP"] = "Gebruik het geselecteerde I2C kanaal voor sensoren in dit blok";
Blockly.Msg["I2C_LIST_DEVICES"] = "Zoek naar verbonden I2C apparaten";

export default Blockly.Msg;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"theme",
"categories"
],
"version": "1.15.1",
"version": "1.16.0",
"description": "Leaphy custom Blockly blocks and arduino code generator",
"name": "@leaphy-robotics/leaphy-blocks"
}

0 comments on commit 65d1b2f

Please sign in to comment.