blinken
This commit is contained in:
		
							parent
							
								
									e64fd212f0
								
							
						
					
					
						commit
						9a9003c496
					
				
					 1 changed files with 10 additions and 30 deletions
				
			
		| 
						 | 
					@ -5,8 +5,12 @@
 | 
				
			||||||
#include "timer/clock.h"
 | 
					#include "timer/clock.h"
 | 
				
			||||||
#include "timer/timer.h"
 | 
					#include "timer/timer.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Two flags that the two protothread functions use. */
 | 
					/* TIME_DELAY macro for convenience */
 | 
				
			||||||
static int protothread1_flag, protothread2_flag;
 | 
					#define TIMER_DELAY(pt, timer, t) \
 | 
				
			||||||
 | 
					  do { \
 | 
				
			||||||
 | 
					  timer_set(&timer, t); \
 | 
				
			||||||
 | 
					  PT_WAIT_UNTIL(pt, timer_expired(&timer)); \
 | 
				
			||||||
 | 
					  } while(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Two timers for the two protothreads. */
 | 
					/* Two timers for the two protothreads. */
 | 
				
			||||||
static struct timer timer1, timer2;
 | 
					static struct timer timer1, timer2;
 | 
				
			||||||
| 
						 | 
					@ -20,20 +24,10 @@ protothread1(struct pt *pt)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* We loop forever here. */
 | 
					  /* We loop forever here. */
 | 
				
			||||||
  while(1) {
 | 
					  while(1) {
 | 
				
			||||||
    /* Wait until the other protothread has set its flag. */
 | 
					 | 
				
			||||||
    PT_WAIT_UNTIL(pt, protothread2_flag != 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    PORTB |= _BV(PB3);
 | 
					    PORTB |= _BV(PB3);
 | 
				
			||||||
    timer_set(&timer1, CLOCK_SECOND/2);
 | 
					    TIMER_DELAY(pt, timer1, 20);
 | 
				
			||||||
    PT_WAIT_UNTIL(pt, timer_expired(&timer1));
 | 
					 | 
				
			||||||
    PORTB &= ~_BV(PB3);
 | 
					    PORTB &= ~_BV(PB3);
 | 
				
			||||||
    timer_set(&timer1, CLOCK_SECOND/2);
 | 
					    TIMER_DELAY(pt, timer1, 20);
 | 
				
			||||||
    PT_WAIT_UNTIL(pt, timer_expired(&timer1));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* We then reset the other protothread's flag, and set our own
 | 
					 | 
				
			||||||
       flag so that the other protothread can run. */
 | 
					 | 
				
			||||||
    protothread2_flag = 0;
 | 
					 | 
				
			||||||
    protothread1_flag = 1;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* And we loop. */
 | 
					    /* And we loop. */
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -52,24 +46,10 @@ protothread2(struct pt *pt)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* We loop forever here. */
 | 
					  /* We loop forever here. */
 | 
				
			||||||
  while(1) {
 | 
					  while(1) {
 | 
				
			||||||
    /* Let the other protothread run. */
 | 
					 | 
				
			||||||
    protothread2_flag = 1;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* Wait until the other protothread has set its flag. */
 | 
					 | 
				
			||||||
    PT_WAIT_UNTIL(pt, protothread1_flag != 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    PORTB |= _BV(PB2);
 | 
					    PORTB |= _BV(PB2);
 | 
				
			||||||
    timer_set(&timer2, CLOCK_SECOND/2);
 | 
					    TIMER_DELAY(pt, timer2, 60);
 | 
				
			||||||
    PT_WAIT_UNTIL(pt, timer_expired(&timer2));
 | 
					 | 
				
			||||||
    PORTB &= ~_BV(PB2);
 | 
					    PORTB &= ~_BV(PB2);
 | 
				
			||||||
    timer_set(&timer2, CLOCK_SECOND/2);
 | 
					    TIMER_DELAY(pt, timer2, 60);
 | 
				
			||||||
    PT_WAIT_UNTIL(pt, timer_expired(&timer2));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* We then reset the other protothread's flag, and set our own
 | 
					 | 
				
			||||||
       flag so that the other protothread can run. */
 | 
					 | 
				
			||||||
    protothread1_flag = 0;
 | 
					 | 
				
			||||||
    protothread2_flag = 1;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* And we loop. */
 | 
					    /* And we loop. */
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in a new issue