/* trafficLight: a program simulating traffic lights on 6 LEDs
 * copyright U. Raich 2017
 * This program is part of the workshop on embedded systems held at 
 * the University of Cape Coast in the winter semester 2017
 * It will be demonstrated at the University Open Day on Oct. 18 2017
 * It has been released under the GNU General Public License
 * see gnu.org/licenses/gpl.html
 */
 

#include <stdio.h>
#include <wiringPi.h>
#define NO_OF_LIGHTS 6
#define NO_OF_STATES 6
#define LONG_PERIOD  15000   /* 15 s */
#define SHORT_PERIOD 1000    /* 1s   */
#define PERIOD_MASK  0x40

/*
  the state defines the state of each lamp in bits 0-5 
  bit 6 defines if we have a long or a short period to stay in this state
  bit layout:
  0  period  TL2red TL2yellow TL2green TL1red TL1yellow TL1green
*/

int main (void)
{
  unsigned char state[] = {0x61,0x22,0x24,0x4c,0x14,0x24};
  unsigned char *ptr;
  int i,period;
  printf ("Traffic Light Simulator\n") ;

  wiringPiSetup () ;            // this will initialize the wiringPi system
  for (i=0;i<NO_OF_LIGHTS;i++)
    pinMode (i, OUTPUT) ;       // pinMode allows to set the pin to input or output 

  for(;;) {
    ptr = state;
    for (i=0;i<NO_OF_STATES; i++) {
      period = (*ptr & PERIOD_MASK) ? LONG_PERIOD : SHORT_PERIOD;
      digitalWriteByte((int)(*ptr++ & ~PERIOD_MASK));
      delay(period);
    }
  }
  return 0 ;
}
