Skip to content

Commit

Permalink
add read/write io functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed Oct 6, 2015
1 parent 333bd1a commit deb0a32
Show file tree
Hide file tree
Showing 9 changed files with 1,092 additions and 1 deletion.
112 changes: 112 additions & 0 deletions Arduino.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Arduino.h - Main include file for the Arduino SDK
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 20 Aug 2014 by MediaTek Inc.
*/

#ifndef Arduino_h
#define Arduino_h

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

// some libraries and sketches depend on this
// AVR stuff, assuming Arduino.h or WProgram.h
// automatically includes it...
//#include <avr/pgmspace.h>


#include "vmdcl.h"

#ifdef __cplusplus
extern "C"{
#endif // __cplusplus

#include "wiring_constants.h"

void yield(void);

/* sketch */
extern void setup( void ) ;
extern void loop( void ) ;


extern boolean changePinType(uint32_t ulPin, uint32_t ulPinType, VM_DCL_HANDLE* handle);
extern void spiPinsRest(void);
extern void setPinHandle(uint32_t ulPin, VM_DCL_HANDLE handle);

typedef enum _EExt_Interrupts
{
EXTERNAL_INT_0=0,
EXTERNAL_INT_1=1,
EXTERNAL_NUM_INTERRUPTS
} EExt_Interrupts ;


typedef void (*voidFuncPtr)( void ) ;

#define PIO_MAX_NUM 19

typedef enum _EPioType
{
PIO_DIGITAL,
PIO_ANALOG,
PIO_EINT,
PIO_PWM,
PIO_SPI,
PIO_UART,
PIO_I2C,
PIO_SD,
PIO_END
} EPioType ;

/* Types used for the tables below */
typedef struct _PinDescription
{
VM_DCL_HANDLE ulHandle;
uint32_t ulGpioId ;
EPioType ulPinType ;
uint32_t ulPupd;
} PinDescription ;

/* Pins table to be instanciated into variant.cpp */
extern PinDescription g_APinDescription[] ;

#ifdef __cplusplus
} // extern "C"


#endif // __cplusplus

// Include board variant
#include "variant.h"



#define USB_PID_LEONARDO 0x0034
#define USB_PID_MICRO 0x0035
#define USB_PID_DUE 0x003E

boolean noStopInterrupts(void);
extern void spiPinsRest(void);
extern void setPinHandle(uint32_t ulPin, VM_DCL_HANDLE handle);

#endif // Arduino_h
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# GCC_BIN = ../LinkIt-1.1-1.60/hardware/tools/gcc-arm-none-eabi-4.8.3-2014q1/bin/
PROJECT = lua
OBJECTS = main.o syscalls_mtk.o console.o shell.o laudiolib.o lgsmlib.o timer.o
OBJECTS = main.o syscalls_mtk.o console.o shell.o laudiolib.o lgsmlib.o timer.o variant.o wiring_digital.o gpio.o
OBJECTS += lua/linenoise.o lua/lapi.o lua/lcode.o lua/ldebug.o lua/ldo.o lua/ldump.o lua/lfunc.o lua/lgc.o lua/llex.o lua/lmem.o \
lua/lobject.o lua/lopcodes.o lua/lparser.o lua/lstate.o lua/lstring.o lua/ltable.o lua/ltm.o \
lua/lundump.o lua/lvm.o lua/lzio.o lua/lrotable.o \
Expand Down
80 changes: 80 additions & 0 deletions gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#include "Arduino.h"
#include "wiring_digital.h"

#include "lua.h"
#include "lauxlib.h"


int gpio_mode(lua_State *L)
{
int pin = luaL_checkinteger(L, 1);
int mode = luaL_checkinteger(L, 2);

pinMode(pin, mode);

return 0;
}

int gpio_read(lua_State *L)
{
int pin = luaL_checkinteger(L, 1);

lua_pushnumber(L, digitalRead(pin));

return 1;
}

int gpio_write(lua_State *L)
{
int pin = luaL_checkinteger(L, 1);
int value = luaL_checkinteger(L, 2);

digitalWrite(pin, value);

return 0;
}


#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 0
#include "lrodefs.h"

#define MOD_REG_NUMBER(L, name, value) lua_pushnumber(L, value); \
lua_setfield(L, -2, name);

const LUA_REG_TYPE gpio_map[] =
{
{LSTRKEY("mode"), LFUNCVAL(gpio_mode)},
{LSTRKEY("read"), LFUNCVAL(gpio_read)},
{LSTRKEY("write"), LFUNCVAL(gpio_write)},
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) },
{ LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) },
{ LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) },
{ LSTRKEY( "LOW" ), LNUMVAL( LOW ) },
{ LSTRKEY( "INPUT_PULLUP" ), LNUMVAL( INPUT_PULLUP ) },
#endif
{LNILKEY, LNILVAL}
};

LUALIB_API int luaopen_gpio(lua_State *L)
{
lua_register(L, "pinMode", gpio_mode);
lua_register(L, "digitalRead", gpio_read);
lua_register(L, "digitalWrite", gpio_write);

#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0

luaL_register(L, "gpio", gpio_map);
// Add constants
MOD_REG_NUMBER( L, "OUTPUT", OUTPUT );
MOD_REG_NUMBER( L, "INPUT", INPUT );
MOD_REG_NUMBER( L, "HIGH", HIGH );
MOD_REG_NUMBER( L, "LOW", LOW );
MOD_REG_NUMBER( L, "INPUT_PULLUP", INPUT_PULLUP );
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lua_State *L = NULL;
extern int luaopen_audio(lua_State *L);
extern int luaopen_gsm(lua_State *L);
extern int luaopen_timer(lua_State *L);
extern int luaopen_gpio(lua_State *L);

static int msleep_c(lua_State *L)
{
Expand All @@ -41,6 +42,7 @@ void setup_lua()
luaopen_audio(L);
luaopen_gsm(L);
luaopen_timer(L);
luaopen_gpio(L);

lua_register(L, "msleep", msleep_c);

Expand Down
Loading

0 comments on commit deb0a32

Please sign in to comment.