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:
- 3:edbf31a8589f
- Parent:
- 2:165723d41023
- Child:
- 4:9ee673ca05ad
File content as of revision 3:edbf31a8589f:
// Frame Level Language
// implementation
#include "mbed.h"
#include "rtos.h"
#include <stdint.h>
#include <vector>
#include "fll.h"
Serial pc(USBTX, USBRX);
// ------
// Source
// ------
Source::Source(button_t *bs, int s, bool l)
{
button_t_seq = bs;
size = s;
loop = l;
i = 0;
}
// TODO: without loop
button_t Source::await()
{
if (size == 0) {
return 0;
}
i = i % size;
return button_t_seq[i++];
}
// ----
// Flow
// ----
button_t Flow::await()
{
std::vector<button_t> bs;
for (int k = 0; k < sources.size(); k++) {
button_t b = sources[k]->await();
bs.push_back(b);
}
return fold(bs);
}
// ----
// Sink
// ----
Sink::Sink(Source* src, rtos::Mail<button_t, MAIL_BOX_SIZE>* box, Mutex* mut)
{
source = src;
mail_box = box;
mutex = mut;
}
// 何ミリ秒かごとに呼ばれる
void Sink::run()
{
button_t *btn;
for (int i = 0; i < 8; i++) { // FIXME: 8?
mutex->lock();
btn = mail_box->alloc();
if(!btn) break;
*btn = source->await();
mail_box->put(btn);
mutex->unlock();
}
}
void Sink::reset(Source* 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);
}
}
}
