The Squirrel interpreter for FRDM-K64F, extended with a set of classes that provide access to the mbed functionality (currently DigitalIn, DigitalInOut, DigitalOut, InterruptIn, PwmOut, Ticker, Timeout, Timer).

Dependencies:   SQUIRREL3 mbed sqbind-0_99

The Squirrel interpreter for FRDM-K64F.

NOTE: Currently of POC quality.

See http://www.squirrel-lang.org/ for information about the Squirrel language.

Currently the following (a subset of their functionality) mbed classes are available from within Squirrel:

  • DigitalIn
  • DigitalOut
  • DigitalInOut
  • PwmOut
  • Ticker
  • Timeout
  • Timer

In addition, InterruptIn is supported, but interrupts are noted when they occur, but only delivered from the main loop of the interpreter.

See also README.txt in the root of the project.

sqmbed/src/trigger.cpp

Committer:
jhnwkmn
Date:
2014-12-16
Revision:
0:6f55c7651ccc
Child:
1:540008bb92a2

File content as of revision 0:6f55c7651ccc:

/*
  @License@
*/

#include <sqmbed/trigger.h>
#include <sqmbed/pointerstorage.h>

namespace
{

class Triggers : public SqMbed::PointerStorage<SqMbed::Trigger>
{
public:
    static void notify()
    {
        // Do not store pointers here. Who knows, maybe the handler creates a
        // new InterruptIn and then both s_nSpace and s_ppInterrupts may change.
        for (uint32_t i = 0; i < s_nSpace; ++i) {
            SqMbed::Trigger* pTrigger = s_ppObjects[i];

            if (pTrigger) {
                pTrigger->call();
            }
        }
    }
};

}

namespace SqMbed
{

Trigger::Trigger(HSQUIRRELVM vm)
    : m_vm(vm)
    , m_triggered(false)
{
    m_object._type = OT_NULL;

    Triggers::add(this);
}

Trigger::~Trigger()
{
    Triggers::remove(this);
}

bool Trigger::reserve()
{
    return Triggers::reserve();
}

void Trigger::call()
{
    if (m_triggered) {
        m_triggered = false; // Should be atomic.

        int top = sq_gettop(m_vm);
        sq_pushobject(m_vm, m_object); // The function
        sq_pushroottable(m_vm);        // This
        sq_call(m_vm, 1, 0, 0);        // Only this as argument.
        sq_settop(m_vm, top);          // Restore the stack.
    }
}

// static
void Trigger::flushTriggers()
{
    Triggers::notify();
}

void Trigger::attach(HSQOBJECT o)
{
    if (m_object._type != OT_NULL) {
        sq_release(m_vm, &m_object);
        m_object._type = OT_NULL;
    }

    m_object = o;
    sq_addref(m_vm, &m_object);
}

// static
void Trigger::triggered()
{
    // TODO: Should be atomic.
    m_triggered = true;
}

}