Advertisement
SpyPeter

Untitled

May 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. includes IntMsg;
  2. module HomeWorkM
  3. {
  4. provides
  5. {
  6. interface StdControl;
  7. }
  8. uses
  9. {
  10. interface Timer as Timer1;
  11. interface Timer as Timer2;
  12. interface SendMsg;
  13. interface ReceiveMsg;
  14. }
  15. }
  16. implementation
  17. {
  18. bool sending_enabled;
  19. char serialNumber=0;
  20. struct TOS_Msg data;
  21. char max[4];
  22. uint8_t num[4];
  23. command result_t StdControl.init()
  24. {
  25. return SUCCESS;
  26. }
  27. command result_t StdControl.start()
  28. {
  29. sending_enabled=FALSE;
  30. return call Timer1.start(TIMER_ONE_SHOT, 10000);
  31. }
  32. command result_t StdControl.stop()
  33. {
  34. return rcombine(call Timer1.stop(), call Timer2.stop());
  35. }
  36. event result_t Timer1.fired()
  37. {
  38.     IntMsg *message = (IntMsg *)data.data;
  39.     if (sending_enabled)
  40.     {
  41.         if (call Timer1.start(TIMER_REPEAT, 1000))
  42.         {
  43.             sending_enabled=TRUE;
  44.         }
  45.     }
  46.     else
  47.     {
  48.         if (serialNumber < 100)
  49.         {
  50.             serialNumber++;
  51.             message->val = serialNumber;
  52.             atomic
  53.             {
  54.                 message->src = TOS_LOCAL_ADDRESS;
  55.             }
  56.         call SendMsg.send(TOS_BCAST_ADDR, sizeof(IntMsg), &data);
  57.         }
  58.     }
  59.     return SUCCESS;
  60. }
  61. event result_t Timer2.fired()
  62. {
  63. uint8_t k;
  64. for (k=0; k<4; k++)
  65. {
  66. store(k,num[k],max[k]); // stores num and max for each k
  67. }
  68. return SUCCESS;
  69. }
  70. event result_t SendMsg.sendDone(TOS_MsgPtr msg, bool success)
  71. {
  72. return SUCCESS;
  73. }
  74. event TOS_MsgPtr ReceiveMsg.receive(TOS_MsgPtr recv_packet)
  75. {
  76. IntMsg *message = (IntMsg *)recv_packet->data;
  77. if (message->val > max[message->src])
  78. {
  79. max[message->src]=message->val;
  80. }
  81. num[message->src]++;
  82. call Timer2.stop();
  83. call Timer2.start(TIMER_ONE_SHOT, 60000);
  84. return recv_packet;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement