LoginSignup
0
0

More than 5 years have passed since last update.

chibiosでLチカの練習 その5

Posted at

messageを使ってみた。

#include "ch.h"
#include "hal.h"

#define CONSOLE_WA_SIZE      THD_WORKING_AREA_SIZE(4096)
#define cputs(msg)            chMsgSend(cdtp, (msg_t) msg)
static thread_t * cdtp;
static THD_FUNCTION(console_thread, arg)
{
    (void) arg;
    while (!chThdShouldTerminateX())
    {
        thread_t * tp = chMsgWait();
        puts((char *) chMsgGet(tp));
        fflush(stdout);
        chMsgRelease(tp, MSG_OK);
    }
}
static THD_WORKING_AREA(waThread1, 1024);
static THD_FUNCTION(Thread1, arg)
{
    thread_t * tp;
    msg_t msg;
    (void) arg;
    chRegSetThreadName("green");
    while (TRUE)
    {
        tp = chMsgWait();
        msg = chMsgGet(tp);
        chMsgRelease(tp, msg);
        cputs("green on");
        chThdSleepMilliseconds(1000);
        cputs("green off");
    }
}
static THD_WORKING_AREA(waThread2, 1024);
static THD_FUNCTION(Thread2, arg)
{
    thread_t * tp;
    msg_t msg;
    (void) arg;
    chRegSetThreadName("yellow");
    while (TRUE)
    {
        tp = chMsgWait();
        msg = chMsgGet(tp);
        chMsgRelease(tp, msg);
        cputs("yellow on");
        chThdSleepMilliseconds(1000);
        cputs("yellow off");
    }
}
static THD_WORKING_AREA(waThread3, 1024);
static THD_FUNCTION(Thread3, arg)
{
    thread_t * tp;
    msg_t msg;
    (void) arg;
    chRegSetThreadName("red");
    while (TRUE)
    {
        tp = chMsgWait();
        msg = chMsgGet(tp);
        chMsgRelease(tp, msg);
        cputs("red on");
        chThdSleepMilliseconds(1000);
        cputs("red off");
    }
}
int main(void)
{
    halInit();
    chSysInit();
    int n;
    n = 0;
    cdtp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, NORMALPRIO, console_thread, NULL);
    thread_t * tp1 = chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
    thread_t * tp2 = chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO + 1, Thread2, NULL);
    thread_t * tp3 = chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO + 1, Thread3, NULL);
    while (!chThdShouldTerminateX())
    {
        n++;
        if (n > 3) n = 1;
        chThdSleepMilliseconds(1000);
        if (n == 1) (void) chMsgSend(tp1, 1);
        if (n == 2) (void) chMsgSend(tp2, 1);
        if (n == 3) (void) chMsgSend(tp3, 1);
    }
    return 0;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0