That seems like a reasonable way to handle things.
People seem to like to reinvent state machines without
realizing that that is what they are doing.
The only mod I can think of would be to turn some or all
of the state functions into tasks that get posted.
If I had been smart on my most recent project this
pattern would have saved me a large number of debugging
headaches...maybe I'll steal and retrofit it.
thx
MS
Peter Mueller wrote:
> Hello,
>
> I have a question regarding the programming concept of Tinyos.
>
> Looking into the example RadioCountToLedsC I found that basically a
> state machine was realized. It is a bit hard to see because the machine
> is distributed over a number of callback (event handler) functions.
>
> I tried to draw the state machine and came up with the following
> diagram. Maybe it is not complete but I think it is very clear what the
> application is doing. I wonder if it isn't it possible to extract the
> behaviour and put it into an own function i.e. a state handler.
>
>
> ------------------------------------------------------------------------
>
>
>
>
>
> The RadioCountToLedsC looks then similar to the following code:
>
> module RadioCountToLedsC @safe() {
> uses {
> ....
> }
>
> implementation {
>
> enum States{
> Idle,
> Booted,
> AMIdle,
> AMSending
> };
>
> enum Events{
> evBooted,
> evStartDone,
> evTimerFired,
> evSendDone,
> evReceiveOK,
> evNoEvent
>
> };
>
> void stateHandler(uint8_t evt){
> switch(state){
>
> case Idle:
> if(evt==evBooted){
> call AMControl.start();
> state=Booted;
> }
> break;
>
> case Booted:
> if(evt==evStartDone){
> call MilliTimer.startPeriodic(250);
> state=AMIdle;
> }
> break;
>
>
> case AMIdle:
> if(evt==evTimerFired){
> sendMessage();
>
> if(sndRetVal==0){
> state=AMSending;
> }else{
> }
> }else if(evt==evReceiveOK){
> receiveMessage();
> }
> break;
>
> case AMSending:
> if(evt==evSendDone){
> state=AMIdle;
> }
> break;
> }
> }
>
>
> event void Boot.booted() {
> stateHandler( evBooted);
> }
>
> event void AMControl.startDone(error_t err) {
> stateHandler(evStartDone);
> }
>
> event void AMControl.stopDone(error_t err) {
> // do nothing
> }
>
> event void MilliTimer.fired() {
> stateHandler(evTimerFired);
> }
>
> event message_t* Receive.receive(message_t* bufPtr, void* payload,
> uint8_t len) {
> dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len);
> if (len == sizeof(radio_count_msg_t)) {
> rcm = (radio_count_msg_t*)payload;
> stateHandler(evReceiveOK);
> }
> return bufPtr;
> }
>
> event void AMSend.sendDone(message_t* bufPtr, error_t error) {
> if (&packet == bufPtr) {
> stateHandler(evSendDone);
> }
> }
>
> }
>
>
> I want to know if the example above is correct regarding event
> processing modell
> of if I missed something?
>
> Peter
>
>
> --
> Peter Mueller
> [email protected]
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tinyos-help mailing list
> [email protected]
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help