@@ -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+
165178int 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