CODE
//Preprocessor Directives
#include "mbed.h"
#include "stdio.h"
//Global Variables
int hour = 0;
int minute = 0;
long second = 0;
int red=0;
int green=0;
int blue=0;
//Just for debuging
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalOut latch(p15);
DigitalOut enable(p16);
DigitalIn sw1(p21);
DigitalIn sw2(p22);
DigitalIn sw4(p23);
DigitalIn sw8(p24);
DigitalIn cram(p25);
SPI spi(p11, p12, p13);
//Function prototypes and Definitions
void RGB_LED(int red, int green, int blue) {
unsigned int low_color=0;
unsigned int high_color=0;
high_color=(blue<<4)|((red&0x3C0)>>6);
low_color=(((red&0x3F)<<10)|(green));
spi.write(high_color);
spi.write(low_color);
latch=1;
latch=0;
}
void timeprocessor(int pass)
{
if(pass % 60 == 0)
{
minute +=1;
if(minute >= 60)
{
hour+=1;
minute = 0;
}
wait(2); //Wait 2 seconds to be sure I don't add minutes at modulus 60
}
if(hour >= 13 )
{
hour = 1;
}
if(minute >= 60)
{
minute = 0;
}
wait(.1); //debug serial out to terminal
printf(" Hour: %d", hour);
wait(.1);
printf(" Minute: %d\n\r", minute);
} //End of timeprocessor()
void color_calculator(void)
{
//Because you want the transistion to be contenous and the minutes roll over every hour, you have to
//break it at the hour.
//12-1-2-3 White at top to Green on right
if(hour == 12 || hour < 3)
{
printf("12-1-2-3 White at top to Green on right");
green = 200;
red = 200;
blue = 200;
if(hour == 12) { red = 200 - (minute * 1.111); blue = red; }
if(hour == 1 ) { red = 200 - (66.66 + (minute * 1.111)); blue = red; }
if(hour == 2 ) { red = 200 - (133.33 +(minute * 1.111)); blue = red; }
}
//3-4-5-6 Green on right to Blue on bottom
if(hour >= 3 && hour < 6)
{
printf("3-4-5-6 Green on right to Blue on bottom");
red = 0;
green = 200;
blue = 0;
if(hour == 3) { green = 200 - (minute * 1.111); blue = minute * 1.111; }
if(hour == 4) { green = 200 - (66.66 + (minute * 1.111)); blue = 66.66 + (minute * 1.111); }
if(hour == 5) { green = 200 - (133.33 + (minute * 1.111)); blue = 133.33 + (minute * 1.111); }
}
//6-7-8-9 Blue on bottom to Red on left
if(hour >= 6 && hour < 9)
{
printf("6-7-8-9 Blue on bottom to Red on left");
red = 0;
green = 0;
blue = 200;
if(hour == 6) { blue = 200 - (minute * 1.111); red = (minute * 1.111); }
if(hour == 7) { blue = 200 - (66.66 + (minute * 1.111)); red = 66.66 + (minute * 1.111); }
if(hour == 8) { blue = 200 - (133.33 + (minute * 1.111)); red = 133.33 + (minute * 1.111); }
}
//9-10-11-12 Red on left to White on top
if(hour >=9 && hour < 12)
{
printf("9-10-11-12 Red on left to White on top");
red = 200;
green = 0;
blue = 0;
if(hour == 9) { blue = minute * 1.111; green = blue ; }
if(hour == 10) { blue = 66.66 + (minute * 1.111); green = blue; }
if(hour == 11) { blue = 133.33 + (minute * 1.111); green = blue; }
}
RGB_LED( red, green, blue);
}
//4bit binary setting register
//void setting_mech(void) //This is sort of an Altairian setting device where you flip up to four switches
//on top of the box then press load (cram).
{
int setloop = 1;
hour = 0;
minute = 0;
while(setloop)
{
if(cram)//hour
{
if(sw8){hour +=8;}
if(sw4){hour +=4;}
if(sw2){hour +=2;}
if(sw1){hour +=1;}
if(hour == 0 || hour > 12){hour = 12;}
led1 = 1; led2 = 1; led3 = 1; led4 = 1;
wait(.25);
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
while(setloop)
{
if(cram)//quartile
{
if(sw8){minute += 45;}
if(sw4){minute += 30;}
if(sw2){minute += 15;}
if(sw1){minute += 0;}
if(minute > 59){minute = 0;}
led1 = 1; led2 = 1; led3 = 1; led4 = 1;
wait(.25);
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
while(setloop)
{
if(cram)//remainder of minutes
{
if(sw8){minute += 8;}
if(sw4){minute += 4;}
if(sw2){minute += 2;}
if(sw1){minute += 1;}
if(minute > 59){minute = 0;}
led1 = 1; led2 = 1; led3 = 1; led4 = 1;
wait(.25);
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
setloop = 0; //KILL ALL LOOPS
}//end remainder if
}//end remainder while
}//end quarter if
}//end while for quarter if
}//end hour if
}//end of big while
}//end of setting function
//Start of main() definition
int main() {
set_time(3600); // Set RTC time to one full hour for calculation purposes-> 60 minutes * 60
//seconds We don't need this now, just good practice.
spi.format(16,0); //Set up SPI
spi.frequency(500000);
enable=0;
latch=0;
//Expedient setting mechanism via serial, commented out because of external
//setting mechanism setting_mech()
/*
int geth, getm;
printf("Hour: ");
scanf("%d",&geth);
printf("Minute: \n");
scanf("%d",&getm);
hour = geth;
if(getm == 0) {minute = getm;}
else {minute = getm -1;}
*/
setting_mech();
if(minute != 0){minute = minute -1; second = (hour * 3600) + (minute * 60); }
if(minute == 0){second = (hour * 3600);}
set_time(second);
while(1) {
wait(.1);
//Start of time loop
time_t seconds = time(NULL);
timeprocessor(seconds); //get seconds counted
color_calculator();
if(cram) //This test is here to set the clock on the fly, without resetting the mbed or unplugging
//the clock
{
led1 = 1; led2 = 1; led3 = 1; led4 = 1;
wait(.25);
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
wait(.2);
led1 = 1; led2 = 1; led3 = 1; led4 = 1;
wait(.25);
led1 = 0; led2 = 0; led3 = 0; led4 = 0;
setting_mech();
}
} //end of while loop
} //end of main()