Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: fll mbed-rtos mbed
fll.cpp
- Committer:
- sierra
- Date:
- 2015-02-14
- Revision:
- 29:2f3d6d09eaac
- Parent:
- 6:d0348b7a2f05
File content as of revision 29:2f3d6d09eaac:
// Frame Level Language
// implementation
#include "mbed.h"
#include "rtos.h"
#include <stdint.h>
#include "fll.h"
Serial pc(USBTX, USBRX);
// ----
// Sink
// ----
Sink::Sink(Producer* src, rtos::Mail<button_t, MAIL_BOX_SIZE>* box, Mutex* mut)
{
source = src;
mail_box = box;
mutex = mut;
}
void Sink::run()
{
button_t *btn;
while(1) {
mutex->lock();
btn = mail_box->alloc();
if(!btn) {
mutex->unlock();
continue;
}
*btn = source->await();
mail_box->put(btn);
mutex->unlock();
}
}
void Sink::reset(Producer* src)
{
mutex->lock();
// queue を空にする
osEvent e;
do {
e = mail_box->get(0);
if(e.status == osEventMail) {
mail_box->free((button_t*)e.value.p);
}
} while (e.status == osEventMail);
// source の更新
source = src;
mutex->unlock();
}
// ------
// Output
// ------
Output::Output(rtos::Mail<button_t, MAIL_BOX_SIZE>* box)
{
mail_box = box;
}
void Output::run()
{
osEvent e = mail_box->get(0);
if (e.status == osEventMail) {
button_t b = *(button_t*)(e.value.p);
mail_box->free((button_t*)e.value.p);
press(b);
} else {
press(0); // Mail になかった
}
}
// fll_keymap.cpp
//
// | uint16 bit | button_t | mbed pin | low/high |
// +------------+--------+----------+----------+
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// | 0x0000 | | | |
// +------------+--------+----------+----------+
//
// Examples:
// 0000 0000 0000 0000 === nothing is pressed
// 0010 0100 1000 0001 === ?, ?, ? and ? are pressed
typedef struct _table {
button_t mask; // 0000 0000 0000 0001 ~ 0010 0000 0000 0000
DigitalOut *pin; // pin5~pin19
int on; // 1 or 0
} table;
DigitalOut pinR2(p10);
DigitalOut pinR1(p5);
DigitalOut pinTriangle(p6);
DigitalOut pinCircle(p7);
DigitalOut pinCross(p8);
DigitalOut pinSquare(p9);
DigitalOut pinStart(p11);
DigitalOut pinAnalog(p12);
DigitalOut pinSelect(p13);
DigitalOut pinRight(p18);
DigitalOut pinDown(p16);
DigitalOut pinLeft(p17);
DigitalOut pinUp(p15);
DigitalOut pinL1(p19);
DigitalOut pinL2(p14);
table tables[] = {
{ R1, &pinR1, 0 },
{ B_TRIANGLE, &pinTriangle, 0 },
{ B_CIRCLE, &pinCircle, 0 },
{ B_CROSS, &pinCross, 0 },
{ B_SQUARE, &pinSquare, 0 },
{ B_R2, &pinR2, 0 },
{ B_START, &pinStart, 0 },
{ B_ANALOG, &pinAnalog, 0 },
{ B_SELECT, &pinSelect, 0 },
{ B_L2, &pinL2, 0 },
{ B_UP, &pinUp, 0 },
{ B_DOWN, &pinDown, 0 },
{ B_LEFT, &pinLeft, 0 },
{ B_RIGHT, &pinRight, 0 },
{ B_L1, &pinL1, 0 }
};
// pc.printf("0x%02x\n", btn);
// 押されるボタンを押す
void press(button_t btn)
{
table t;
for (int i = 0; i < sizeof(tables)/sizeof(table); i++) {
t = tables[i];
if (btn & t.mask) {
t.pin->write(t.on);
} else {
t.pin->write(1 - t.on);
}
}
}
