python

Dependencies:   TSI mbed

Fork of pymite by Norimasa Okamoto

platform/mbed/main_nat.cpp

Committer:
va009039
Date:
2013-03-10
Revision:
1:28afb064a41c
Parent:
0:65f1469d6bfb
Child:
2:a2bea117e22e

File content as of revision 1:28afb064a41c:

#undef __FILE_ID__
#define __FILE_ID__ 0x0A
/**
 * PyMite usr native function file
 *
 * automatically created by pmImgCreator.py
 * on Sun Mar 10 18:24:12 2013
 *
 * DO NOT EDIT THIS FILE.
 * ANY CHANGES WILL BE LOST.
 *
 * @file    main_nat.cpp
 */

#define __IN_LIBNATIVE_C__
#include "pm.h"

/* From: mbed.py */
#include "mbed.h"

/* PinName lookup table.  Converts pin number to PinName. */
static PinName const 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
};

PmReturn_t
nat_placeholder_func(pPmFrame_t *ppframe)
{

    /*
     * Use placeholder because an index 
     * value of zero denotes the stdlib.
     * This function should not be called.
     */
    PmReturn_t retval;
    PM_RAISE(retval, PM_RET_EX_SYS);
    return retval;

}

PmReturn_t
nat_01_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        DigitalOut *dout;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        dout = new DigitalOut(pinNumToName[((pPmInt_t)pn)->val]);

        /* Save the pointer to adc as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)dout, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_02_mbed_write(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        DigitalOut *dout;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        dout = (DigitalOut *)((pPmInt_t)pn)->val;

        /* Write value to DAC */
        pn = NATIVE_GET_LOCAL(1);
        dout->write(((pPmInt_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_03_mbed_read(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        DigitalOut *dout;
        int32_t n;

        /* If wrong number of args, throw type exception */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        dout = (DigitalOut *)((pPmInt_t)pn)->val;

        /* Return input value on the stack */
        n = dout->read();
        retval = int_new(n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_04_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        DigitalIn *din;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        din = new DigitalIn(pinNumToName[((pPmInt_t)pn)->val]);

        /* Save the pointer to adc as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)din, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_05_mbed_read(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        DigitalIn *din;
        int32_t n;

        /* If wrong number of args, throw type exception */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        din = (DigitalIn *)((pPmInt_t)pn)->val;

        /* Return input value on the stack */
        n = din->read();
        retval = int_new(n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_06_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogIn *adc;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        adc = new AnalogIn(pinNumToName[((pPmInt_t)pn)->val]);

        /* Save the pointer to adc as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)adc, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_07_mbed_read_u16(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogIn *adc;
        int32_t n;

        /* If wrong number of args, throw type exception */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        adc = (AnalogIn *)((pPmInt_t)pn)->val;

        /* Return input value on the stack */
        n = adc->read_u16();
        retval = int_new(n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_08_mbed_read(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogIn *adc;
        float n;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        adc = (AnalogIn *)((pPmInt_t)pn)->val;

        /* Return input value on the stack */
        n = adc->read();
        retval = float_new(n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_09_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogOut *dac;
        uint8_t objid;


        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the object */
        dac = new AnalogOut(pinNumToName[((pPmInt_t)pn)->val]);

        /* Save the pointer to adc as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)dac, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_10_mbed_write_u16(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogOut *dac;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        dac = (AnalogOut *)((pPmInt_t)pn)->val;

        /* Write value to DAC */
        pn = NATIVE_GET_LOCAL(1);
        dac->write_u16(((pPmInt_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_11_mbed_write(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogOut *dac;
        float n;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_FLT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        dac = (AnalogOut *)((pPmInt_t)pn)->val;

        /* Saturate and write value to DAC */
        pn = NATIVE_GET_LOCAL(1);
        n = ((pPmFloat_t)pn)->val;
        if (n < 0.0)
        {
            n = 0.0;
        }
        else if (n > 1.0)
        {
            n = 1.0;
        }
        dac->write(n);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_12_mbed_read(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        AnalogOut *dac;
        float n;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        dac = (AnalogOut *)((pPmInt_t)pn)->val;

        /* Return output value on the stack */
        n = dac->read();
        retval = float_new(n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_13_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        PwmOut *pwm;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        pwm = new PwmOut(pinNumToName[((pPmInt_t)pn)->val]);

        /* Save the pointer to pwm as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)pwm, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_14_mbed_write(pPmFrame_t *ppframe)
{

        PmReturn_t retval = PM_RET_OK;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pPmObj_t pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pPmObj_t pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_FLT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pPmObj_t pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        PwmOut* pwm = (PwmOut *)((pPmInt_t)pn)->val;

        pn = NATIVE_GET_LOCAL(1);
        float n = ((pPmFloat_t)pn)->val;
        pwm->write(n);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_15_mbed_read(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        DigitalOut *dout;
        int32_t n;

        /* If wrong number of args, throw type exception */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        dout = (DigitalOut *)((pPmInt_t)pn)->val;

        /* Return input value on the stack */
        n = dout->read();
        retval = int_new(n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_16_mbed_period(pPmFrame_t *ppframe)
{

        PmReturn_t retval = PM_RET_OK;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pPmObj_t pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pPmObj_t pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_FLT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pPmObj_t pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        PwmOut* pwm = (PwmOut *)((pPmInt_t)pn)->val;

        pn = NATIVE_GET_LOCAL(1);
        pwm->period(((pPmFloat_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_17_mbed_period_ms(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        PwmOut *pwm;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        pwm = (PwmOut *)((pPmInt_t)pn)->val;

        pn = NATIVE_GET_LOCAL(1);
        pwm->period_ms(((pPmInt_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_18_mbed_period_us(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        PwmOut *pwm;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        pwm = (PwmOut *)((pPmInt_t)pn)->val;

        pn = NATIVE_GET_LOCAL(1);
        pwm->period_us(((pPmInt_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_19_mbed_pulsewidth(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_FLT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        PwmOut* pwm = (PwmOut *)((pPmInt_t)pn)->val;

        pn = NATIVE_GET_LOCAL(1);
        pwm->pulsewidth(((pPmFloat_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_20_mbed_puslewidth_ms(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        PwmOut *pwm;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        pwm = (PwmOut *)((pPmInt_t)pn)->val;

        /* Write value to DAC */
        pn = NATIVE_GET_LOCAL(1);
        pwm->pulsewidth_ms(((pPmInt_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_21_mbed_pulsewidth_us(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        PwmOut *pwm;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        pwm = (PwmOut *)((pPmInt_t)pn)->val;

        pn = NATIVE_GET_LOCAL(1);
        pwm->pulsewidth_us(((pPmInt_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_22_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t ptx;
        pPmObj_t prx;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        Serial *ser;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 3)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        ptx = NATIVE_GET_LOCAL(1);
        prx = NATIVE_GET_LOCAL(2);
        if ((OBJ_GET_TYPE(ptx) != OBJ_TYPE_INT)
            || (OBJ_GET_TYPE(prx) != OBJ_TYPE_INT))
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        ser = new Serial(pinNumToName[((pPmInt_t)ptx)->val],
                         pinNumToName[((pPmInt_t)prx)->val]);

        /* Save the pointer to ser as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)ser, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_23_mbed_putc(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        Serial *ser;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_STR)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        ser = (Serial *)((pPmInt_t)pn)->val;

        /* Write value to DAC */
        pn = NATIVE_GET_LOCAL(1);
        ser->putc(((pPmString_t)pn)->val[0]);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_24_mbed_puts(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        Serial *ser;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pn = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pn) != OBJ_TYPE_STR)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        ser = (Serial *)((pPmInt_t)pn)->val;

        /* Write value to DAC */
        pn = NATIVE_GET_LOCAL(1);
        ser->puts((const char *)((pPmString_t)pn)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_25_mbed_getc(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        Serial *ser;
        int32_t n;

        /* If wrong number of args, throw type exception */
        if (NATIVE_GET_NUM_ARGS() != 1)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        ser = (Serial *)((pPmInt_t)pn)->val;

        /* Return char (as string) on the stack */
        n = ser->getc();
        retval = string_newFromChar((uint8_t)n, &pn);
        NATIVE_SET_TOS(pn);

        return retval;
        
}

PmReturn_t
nat_26_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pmosi;
        pPmObj_t pmiso;
        pPmObj_t psclk;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        SPI *spi;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 4)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pmosi = NATIVE_GET_LOCAL(1);
        pmiso = NATIVE_GET_LOCAL(2);
        psclk = NATIVE_GET_LOCAL(3);
        if ((OBJ_GET_TYPE(pmosi) != OBJ_TYPE_INT)
            || (OBJ_GET_TYPE(pmiso) != OBJ_TYPE_INT)
            || (OBJ_GET_TYPE(psclk) != OBJ_TYPE_INT))
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        spi = new SPI(pinNumToName[((pPmInt_t)pmosi)->val],
                      pinNumToName[((pPmInt_t)pmiso)->val],
                      pinNumToName[((pPmInt_t)psclk)->val]);

        /* Save the pointer to ser as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)spi, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_27_mbed_format(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pbits;
        pPmObj_t pmode;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        SPI *spi;

        /* Raise TypeError if wrong number of args */
        if ((NATIVE_GET_NUM_ARGS() < 2) || (NATIVE_GET_NUM_ARGS() > 3))
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pbits = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pbits) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the mode arg if it exists */
        pmode = PM_ZERO;
        if (NATIVE_GET_NUM_ARGS() == 3)
        {
            pmode = NATIVE_GET_LOCAL(3);
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        spi = (SPI *)((pPmInt_t)pn)->val;

        /* Set format args */
        spi->format(((pPmInt_t)pbits)->val, ((pPmInt_t)pmode)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_28_mbed_frequency(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t phz;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        SPI *spi;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        phz = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(phz) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        spi = (SPI *)((pPmInt_t)pn)->val;

        /* Set frequency */
        spi->frequency(((pPmInt_t)phz)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_29_mbed_write(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t pv;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        SPI *spi;
        int32_t r;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        pv = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(pv) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        spi = (SPI *)((pPmInt_t)pn)->val;

        /* Write the value and return the response */
        r = spi->write(((pPmInt_t)pv)->val);
        retval = int_new(r, &pn);
        NATIVE_SET_TOS(pn);
        return retval;
        
}

PmReturn_t
nat_30_mbed___init__(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t psda;
        pPmObj_t pscl;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        I2C *i2c;
        uint8_t objid;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 3)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        psda = NATIVE_GET_LOCAL(1);
        pscl = NATIVE_GET_LOCAL(2);
        if ((OBJ_GET_TYPE(psda) != OBJ_TYPE_INT)
            || (OBJ_GET_TYPE(pscl) != OBJ_TYPE_INT))
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Instantiate the C++ object */
        i2c = new I2C(pinNumToName[((pPmInt_t)psda)->val],
                      pinNumToName[((pPmInt_t)pscl)->val]);

        /* Save the pointer to ser as an inaccessible attribute */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = int_new((uint32_t)i2c, &pn);
        PM_RETURN_IF_ERROR(retval);
        heap_gcPushTempRoot(pn, &objid);
        retval = dict_setItem(pattrs, PM_NONE, pn);
        heap_gcPopTempRoot(objid);
        PM_RETURN_IF_ERROR(retval);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_31_mbed_frequency(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t phz;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        I2C *i2c;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 2)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        phz = NATIVE_GET_LOCAL(1);
        if (OBJ_GET_TYPE(phz) != OBJ_TYPE_INT)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        i2c = (I2C *)((pPmInt_t)pn)->val;

        /* Set frequency */
        i2c->frequency(((pPmInt_t)phz)->val);

        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_32_mbed_read(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t paddr;
        pPmObj_t pdata;
        pPmObj_t plen;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        I2C *i2c;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 4)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        paddr = NATIVE_GET_LOCAL(1);
        pdata = NATIVE_GET_LOCAL(2);
        plen = NATIVE_GET_LOCAL(3);
        if ((OBJ_GET_TYPE(paddr) != OBJ_TYPE_INT)
            || (OBJ_GET_TYPE(pdata) != OBJ_TYPE_STR)
            || (OBJ_GET_TYPE(plen) != OBJ_TYPE_INT))
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        i2c = (I2C *)((pPmInt_t)pn)->val;

        /* Read the bytes into the string */
        /* WARNING: Changing the bytes of a string object is BAD. */
        i2c->read(((pPmInt_t)paddr)->val,
                  (char *)((pPmString_t)pdata)->val,
                  ((pPmInt_t)plen)->val);
        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

PmReturn_t
nat_33_mbed_write(pPmFrame_t *ppframe)
{

        pPmObj_t pself;
        pPmObj_t pn;
        pPmObj_t paddr;
        pPmObj_t pdata;
        pPmObj_t plen;
        pPmObj_t pattrs;
        PmReturn_t retval = PM_RET_OK;
        I2C *i2c;

        /* Raise TypeError if wrong number of args */
        if (NATIVE_GET_NUM_ARGS() != 4)
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }
        pself = NATIVE_GET_LOCAL(0);

        /* Raise TypeError if arg is not the right type */
        paddr = NATIVE_GET_LOCAL(1);
        pdata = NATIVE_GET_LOCAL(2);
        plen = NATIVE_GET_LOCAL(3);
        if ((OBJ_GET_TYPE(paddr) != OBJ_TYPE_INT)
            || (OBJ_GET_TYPE(pdata) != OBJ_TYPE_STR)
            || (OBJ_GET_TYPE(plen) != OBJ_TYPE_INT))
        {
            PM_RAISE(retval, PM_RET_EX_TYPE);
            return retval;
        }

        /* Get the the C++ instance */
        pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
        retval = dict_getItem(pattrs, PM_NONE, &pn);
        PM_RETURN_IF_ERROR(retval);
        i2c = (I2C *)((pPmInt_t)pn)->val;

        /* Write the value and return the response */
        i2c->write(((pPmInt_t)paddr)->val,
                   (char *)((pPmString_t)pdata)->val,
                   ((pPmInt_t)plen)->val);
        NATIVE_SET_TOS(PM_NONE);
        return retval;
        
}

/* Native function lookup table */
pPmNativeFxn_t const usr_nat_fxn_table[] =
{
    nat_placeholder_func,
    nat_01_mbed___init__,
    nat_02_mbed_write,
    nat_03_mbed_read,
    nat_04_mbed___init__,
    nat_05_mbed_read,
    nat_06_mbed___init__,
    nat_07_mbed_read_u16,
    nat_08_mbed_read,
    nat_09_mbed___init__,
    nat_10_mbed_write_u16,
    nat_11_mbed_write,
    nat_12_mbed_read,
    nat_13_mbed___init__,
    nat_14_mbed_write,
    nat_15_mbed_read,
    nat_16_mbed_period,
    nat_17_mbed_period_ms,
    nat_18_mbed_period_us,
    nat_19_mbed_pulsewidth,
    nat_20_mbed_puslewidth_ms,
    nat_21_mbed_pulsewidth_us,
    nat_22_mbed___init__,
    nat_23_mbed_putc,
    nat_24_mbed_puts,
    nat_25_mbed_getc,
    nat_26_mbed___init__,
    nat_27_mbed_format,
    nat_28_mbed_frequency,
    nat_29_mbed_write,
    nat_30_mbed___init__,
    nat_31_mbed_frequency,
    nat_32_mbed_read,
    nat_33_mbed_write,
};