Davi Souza
/
pymite
python
Fork of pymite by
Diff: platform/mbed/NativeClassInterface.cpp
- Revision:
- 2:a2bea117e22e
- Child:
- 3:559c4bd38111
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/platform/mbed/NativeClassInterface.cpp Tue Mar 12 11:40:41 2013 +0000 @@ -0,0 +1,145 @@ +// NativeClassInterface.cpp 2013/3/12 +#include "mbed.h" +#define __DEBUG__ 1 +#include "pm.h" +#include "NativeClassInterface.h" + +#undef __FILE_ID__ +#define __FILE_ID__ 0x71 + +template<> +int NativeClassInterface::argv<int>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + if (OBJ_GET_TYPE(pn) == OBJ_TYPE_STR) { + return ((pPmString_t)pn)->val[0]; + } + return ((pPmInt_t)pn)->val; +} + +template<> +bool NativeClassInterface::argv<bool>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return ((pPmInt_t)pn)->val; +} + +template<> +float NativeClassInterface::argv<float>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return ((pPmFloat_t)pn)->val; +} + +template<> +char NativeClassInterface::argv<char>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return ((pPmString_t)pn)->val[0]; +} + +template<> +char* NativeClassInterface::argv<char*>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return reinterpret_cast<char*>(((pPmString_t)pn)->val); +} + +template<> +const char* NativeClassInterface::argv<const char*>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return reinterpret_cast<const char*>(((pPmString_t)pn)->val); +} + +template<> +uint16_t NativeClassInterface::argv<uint16_t>(int n) +{ + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return ((pPmInt_t)pn)->val; +} + +template<> +PinName NativeClassInterface::argv<PinName>(int n) +{ + /* PinName lookup table. Converts pin number to PinName. */ + const PinName pinNumToName[] = { + NC, LED1, LED2, LED3, LED4, p5, p6, p7, p8, p9, + p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, + p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, + p30 + }; + + pPmObj_t pn = NATIVE_GET_LOCAL(n); + return pinNumToName[((pPmInt_t)pn)->val]; +} + + +template<> +PmReturn_t NativeClassInterface::set_return_value<int>(PmType_t ret_type, int value) +{ + pPmObj_t pn; + PmReturn_t retval; + if (ret_type == OBJ_TYPE_STR) { + retval = string_newFromChar((uint8_t)value, &pn); + } else { + retval = int_new(value, &pn); + } + NATIVE_SET_TOS(pn); + return retval; +} + +template<> +PmReturn_t NativeClassInterface::set_return_value<uint16_t>(PmType_t ret_type, uint16_t value) +{ + pPmObj_t pn; + PmReturn_t retval = int_new(value, &pn); + NATIVE_SET_TOS(pn); + return retval; +} + +template<> +PmReturn_t NativeClassInterface::set_return_value<float>(PmType_t ret_type, float value) +{ + pPmObj_t pn; + PmReturn_t retval = float_new(value, &pn); + NATIVE_SET_TOS(pn); + return retval; +} + +template<typename R> +PmReturn_t NativeClassInterface::set_return_value(PmType_t ret_type, R value) +{ + pPmObj_t pn; + PmReturn_t retval; + switch(ret_type) { + case OBJ_TYPE_INT: + retval = int_new(value, &pn); + break; + case OBJ_TYPE_FLT: + retval = float_new(value, &pn); + break; + } + NATIVE_SET_TOS(pn); + return retval; +} + +PmReturn_t NativeClassInterface::check_argv_type(int arg_n, ...) +{ + PmReturn_t retval = PM_RET_OK; + if (NATIVE_GET_NUM_ARGS() != (arg_n+1)) { + PM_RAISE(retval, PM_RET_EX_TYPE); + return retval; + } + va_list vl; + va_start(vl, arg_n); + for(int n = 1; n <= arg_n; n++) { + pPmObj_t pn = NATIVE_GET_LOCAL(n); + int ty = va_arg(vl, int); + if (OBJ_GET_TYPE(pn) != ty) { + PM_RAISE(retval, PM_RET_EX_TYPE); + return retval; + } + } + return retval; +}