Skip to content

Commit 36fa346

Browse files
committed
Added support for pinMode
1 parent c77ac62 commit 36fa346

15 files changed

Lines changed: 228 additions & 15 deletions

File tree

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ It comes with the hardware schematics, production files and the Megahub firmware
1515

1616
- **KiCad**: All schematics and PCB production files are available in KiCad and Gerber format
1717
- **WiFi Enabled**: Built-in wireless connectivity for remote control and programming
18+
- **SD-Card**: Built-in SD-card reader to store multiple projects (autostart possible)
1819
- **4 UART Ports**: Connect up to 4 LEGO© WeDo/Powered Up! devices simultaneously (motors, sensors, etc.)
1920
- **Built-in IMU**: Integrated MPU6050 6-axis accelerometer and gyroscope for motion sensing
2021
- **FastLED/NeoPixel Support**: Built-in support for addressable RGB LED strips
@@ -24,7 +25,13 @@ It comes with the hardware schematics, production files and the Megahub firmware
2425
- **ROS(Robot Operating System)**: Micro ROS support is comming soon...
2526
- **MQTT**: MQTT support is comming soon...
2627
- **Gamepad**: Bluetooth Gamepad support is comming soon...
27-
- **BT/WiFi switch**: Allow configuration over Bluetooth API instead of Restful HTTP API
28+
- **BT/WiFi switch**: Allow configuration over Bluetooth API instead of Restful HTTP API is comming soon...
29+
30+
## Why not Micropython on existing hardware?
31+
32+
- I wanted to learn something new
33+
- Designing Hard- and Software work together is fun
34+
- There is no truly open sysstem with the same capabilities available on the market
2835

2936
## How It Differs from Existing Solutions
3037

@@ -38,7 +45,7 @@ It comes with the hardware schematics, production files and the Megahub firmware
3845

3946
### vs. fischertechnik© RX Controller
4047

41-
- **LEGO Compatibility**: Native support for LEGO WeDo 2.0 protocol
48+
- **LEGO Compatibility**: Native support for LEGO© WeDo 2.0 protocol
4249
- **Web-based Programming**: No proprietary software required
4350
- **Cost-effective**: Based on affordable ESP32 hardware
4451
- **Visual Programming**: Blockly support makes it accessible for all skill levels

frontend/src/components/blockly/component.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ const customBlocks = {
325325
"previousStatement": true,
326326
"nextStatement": true,
327327
"colour": 230,
328-
"tooltip": "Setzt einen GPIO-Pin auf HIGH oder LOW",
328+
"tooltip": "Set the state of a GPIO pin",
329329
"helpUrl": ""
330330
},
331331
generator: (block, generator) => {
@@ -336,6 +336,59 @@ const customBlocks = {
336336
return "hub.digitalWrite(" + pin + "," + valueCode + ")\n";
337337
}
338338
},
339+
"mh_pinmode": {
340+
category: 'I/O',
341+
colour: 230,
342+
blockdefinition: {
343+
"type": "mh_pinmode",
344+
"message0": "Set pin mode of %1 to %2",
345+
"args0": [
346+
{
347+
"type": "field_dropdown",
348+
"name": "PIN",
349+
"options": [
350+
["GPIO13", "GPIO13"],
351+
["GPIO16", "GPIO16"],
352+
["GPIO17", "GPIO17"],
353+
["GPIO25", "GPIO25"],
354+
["GPIO26", "GPIO26"],
355+
["GPIO27", "GPIO27"],
356+
["GPIO32", "GPIO32"],
357+
["GPIO33", "GPIO33"],
358+
["UART1_GP4", "UART1_GP4"],
359+
["UART1_GP5", "UART1_GP5"],
360+
["UART1_GP6", "UART1_GP6"],
361+
["UART1_GP7", "UART1_GP7"],
362+
["UART2_GP4", "UART2_GP4"],
363+
["UART2_GP5", "UART2_GP5"],
364+
["UART2_GP6", "UART2_GP6"],
365+
["UART2_GP7", "UART2_GP7"]
366+
]
367+
},
368+
{
369+
"type": "field_dropdown",
370+
"name": "MODE",
371+
"options": [
372+
["PINMODE_INPUT", "PINMODE_INPUT"],
373+
["PINMODE_INPUT_PULLUP", "PINMODE_INPUT_PULLUP"],
374+
["PINMODE_INPUT_PULLDOWN", "PINMODE_INPUT_PULLDOWN"],
375+
["PINMODE_OUTPUT", "PINMODE_OUTPUT"]
376+
]
377+
}
378+
],
379+
"previousStatement": true,
380+
"nextStatement": true,
381+
"colour": 230,
382+
"tooltip": "Set the mode of a GPIO pin",
383+
"helpUrl": ""
384+
},
385+
generator: (block, generator) => {
386+
const pin = block.getFieldValue('PIN');
387+
const mode = block.getFieldValue('MODE');
388+
389+
return "hub.pinMode(" + pin + "," + mode + ")\n";
390+
}
391+
},
339392
"mh_digitalread": {
340393
category: 'I/O',
341394
colour: 230,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef BTCONTROLLER_H
2+
#define BTCONTROLLER_H
3+
4+
#include <Arduino.h>
5+
6+
#include <BLEGamepadClient.h>
7+
8+
class BluetoothController {
9+
private:
10+
XboxController *controller_;
11+
12+
public:
13+
BluetoothController();
14+
virtual ~BluetoothController();
15+
16+
void init();
17+
void loop();
18+
};
19+
20+
#endif // BTCONTROLLER_H
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "btcontroller.h"
2+
3+
BluetoothController::BluetoothController() {
4+
// controller_ = new XboxController();
5+
}
6+
7+
BluetoothController ::~BluetoothController() {
8+
// delete controller_;
9+
}
10+
11+
void BluetoothController::init() {
12+
// controller_->begin();
13+
}
14+
15+
void BluetoothController::loop() {
16+
}

lib/lpfuart/include/legodevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LegoDevice {
3838
void setM2(bool status);
3939
void setMotorSpeed(int speed);
4040

41+
void setPinMode(int pin, int mode);
4142
int digitalRead(int pin);
4243
void digitalWrite(int pin, int value);
4344

lib/lpfuart/include/serialio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class SerialIO {
1616
virtual void setM1(bool status) = 0;
1717
virtual void setM2(bool status) = 0;
1818

19+
virtual void setPinMode(int pin, int mode) = 0;
1920
virtual int digitalRead(int pin) = 0;
2021
virtual void digitalWrite(int pin, int value) = 0;
2122
};

lib/lpfuart/src/legodevice.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "legodevice.h"
22

3+
#include "i2csync.h"
34
#include "logging.h"
45
#include "waitingstate.h"
56

6-
#include "i2csync.h"
7-
87
LegoDevice::LegoDevice(SerialIO *serialIO)
98
: serialSpeed(2400)
109
, numModes(-1)
@@ -170,7 +169,7 @@ void LegoDevice::setM1(bool status) {
170169
void LegoDevice::setM2(bool status) {
171170
i2c_lock();
172171
serialIO_->setM2(status);
173-
i2c_unlock();
172+
i2c_unlock();
174173
}
175174

176175
void LegoDevice::setMotorSpeed(int speed) {
@@ -203,15 +202,20 @@ void LegoDevice::loop() {
203202
}
204203
}
205204

205+
void LegoDevice::setPinMode(int pin, int mode) {
206+
i2c_lock();
207+
serialIO_->setPinMode(pin, mode);
208+
i2c_unlock();
209+
}
210+
206211
int LegoDevice::digitalRead(int pin) {
207212
i2c_lock();
208213
int value = serialIO_->digitalRead(pin);
209214
i2c_unlock();
210215
return value;
211-
212216
}
213217
void LegoDevice::digitalWrite(int pin, int value) {
214218
i2c_lock();
215219
serialIO_->digitalWrite(pin, value);
216220
i2c_unlock();
217-
}
221+
}

lib/lua/include/luaconf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
** Define it if you want Lua to avoid the use of a few C99 features
4242
** or Windows-specific features on Windows.
4343
*/
44-
/* #define LUA_USE_C89 */
44+
#define LUA_USE_C89
4545

4646

4747
/*
@@ -122,7 +122,7 @@
122122
/*
123123
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats.
124124
*/
125-
#define LUA_32BITS 0
125+
#define LUA_32BITS 1
126126

127127

128128
/*

lib/megahub/include/megahub.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626

2727
#define FORMAT_SIMPLE 2000
2828

29+
#define PINMODE_INPUT 3000
30+
#define PINMODE_INPUT_PULLUP 3001
31+
#define PINMODE_INPUT_PULLDOWN 3002
32+
#define PINMODE_OUTPUT 3003
33+
2934
struct LuaCheckResult {
3035
bool success;
3136
int parseTime;
@@ -53,6 +58,7 @@ class Megahub {
5358
void executeLUACode(String luaCode);
5459
bool stopLUACode();
5560

61+
void setPinMode(int pin, int mode);
5662
int digitalReadFrom(int pin);
5763
void digitalWriteTo(int pin, int value);
5864

lib/megahub/src/megahub.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,25 @@ int hub_digitalwrite(lua_State *luaState) {
162162
return 0;
163163
}
164164

165+
int hub_set_pin_mode(lua_State *luaState) {
166+
167+
int pin = lua_tointeger(luaState, 1);
168+
int value = lua_tointeger(luaState, 2);
169+
170+
DEBUG("Setting pin mode of pin %d to %d", pin, value);
171+
172+
Megahub *megahub = getMegaHubRef(luaState);
173+
megahub->setPinMode(pin, value);
174+
175+
return 0;
176+
}
177+
165178
int hub_library(lua_State *luaState) {
166179
const luaL_Reg hubfunctions[] = {
167180
{"main_control_loop", hub_register_main_control_loop},
168181
{ "init", hub_init},
169182
{ "setmotorspeed", hub_setmotorspeed},
183+
{ "pinMode", hub_set_pin_mode},
170184
{ "digitalRead", hub_digitalread},
171185
{ "digitalWrite", hub_digitalwrite},
172186
{ NULL, NULL}
@@ -543,6 +557,15 @@ lua_State *Megahub::newLuaState() {
543557
lua_pushinteger(ls, UART2_GP7);
544558
lua_setglobal(ls, "UART2_GP7");
545559

560+
lua_pushinteger(ls, PINMODE_INPUT);
561+
lua_setglobal(ls, "PINMODE_INPUT");
562+
lua_pushinteger(ls, PINMODE_INPUT_PULLUP);
563+
lua_setglobal(ls, "PINMODE_INPUT_PULLUP");
564+
lua_pushinteger(ls, PINMODE_INPUT_PULLDOWN);
565+
lua_setglobal(ls, "PINMODE_INPUT_PULLDOWN");
566+
lua_pushinteger(ls, PINMODE_OUTPUT);
567+
lua_setglobal(ls, "PINMODE_OUTPUT");
568+
546569
// FastLED constants
547570

548571
lua_pushinteger(ls, NEOPIXEL_TYPE);
@@ -795,3 +818,51 @@ void Megahub::digitalWriteTo(int pin, int value) {
795818
break;
796819
}
797820
}
821+
822+
void Megahub::setPinMode(int pin, int mode) {
823+
DEBUG("Setting mode of pin %d to %d", pin, mode);
824+
switch (pin) {
825+
case UART1_GP4:
826+
device1_->setPinMode(4, mode);
827+
break;
828+
case UART1_GP5:
829+
device1_->setPinMode(5, mode);
830+
break;
831+
case UART1_GP6:
832+
device1_->setPinMode(6, mode);
833+
break;
834+
case UART1_GP7:
835+
device1_->setPinMode(7, mode);
836+
break;
837+
case UART2_GP4:
838+
device3_->setPinMode(4, mode);
839+
break;
840+
case UART2_GP5:
841+
device3_->setPinMode(5, mode);
842+
break;
843+
case UART2_GP6:
844+
device3_->setPinMode(6, mode);
845+
break;
846+
case UART2_GP7:
847+
device3_->setPinMode(7, mode);
848+
break;
849+
default:
850+
switch (mode) {
851+
case PINMODE_INPUT:
852+
pinMode(pin, INPUT);
853+
break;
854+
case PINMODE_INPUT_PULLUP:
855+
pinMode(pin, INPUT_PULLUP);
856+
break;
857+
case PINMODE_INPUT_PULLDOWN:
858+
pinMode(pin, INPUT_PULLDOWN);
859+
break;
860+
case PINMODE_OUTPUT:
861+
pinMode(pin, OUTPUT);
862+
break;
863+
default:
864+
WARN("Unsupported pin mode %d for pin %d", mode, pin);
865+
}
866+
break;
867+
}
868+
}

0 commit comments

Comments
 (0)