/* 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 colorMask,colorIndex;
  char *colorStrings[] = {"green ","yellow","red   "};
  char *periodStrings[] = {"short","long"};
  unsigned char *ptr;
  int i;
  printf ("Traffic Light Simulator\n") ;
  
  ptr = state;
  for (i=0;i<NO_OF_STATES; i++) {
    printf("State: %i ",i);
    colorMask = 1;
    for (colorIndex=0;colorIndex<3;colorIndex++) 
      if (*ptr & (colorMask<<colorIndex))
	printf("traffic light 1: %s ", colorStrings[colorIndex]);
      
    for (colorIndex=3;colorIndex<6;colorIndex++) 
      if (*ptr & (colorMask << (colorIndex)))
	printf("traffic light 2: %s", colorStrings[colorIndex-3]);
    
    printf(" period: %s\n",periodStrings[(*ptr++ & PERIOD_MASK)>>6]);
  }
  return 0 ;
}
