Untitled

 avatar
unknown
plain_text
2 years ago
1.5 kB
4
Indexable
// intensity of color
#define INTENSITY 40
int binaryArray[8];

void numberToBinary(int num)
{
 // reset binary, just in case
 for (int i = 0; i <= 7; i++)
 {
 binaryArray[i] = 0;
 }
 // calculate binary
 int i = 0; 
 while (num != 0){
 binaryArray[i] = ( num % 2 == 0 ? 0 : 1 );
 num /= 2;
 i++;

}
}

void display(int num, int line, int r, int g, int b)
{ 
 // converts the number into the binary array 
 numberToBinary(num);
 // draw the binary 
 for(int pos = 0; pos <= 7; pos++){
 if (binaryArray[7-pos] == 1)
 {
 led_set_pixel(pos, line, r, g, b); 
 }
}
}
int main(void)
{
 time_t t;
 struct tm *currentTime;
 struct timespec delay;
 // lines of clock, 3. and 7. row are blank
 int year = 0;
 int month = 1;
 int day = 2;
 int hour = 4;
 int minute = 5;
 int second = 6;
printf("Clock is running\n");
 while(1)
 {
 // causing a delay
 int millis = 1000;
 delay.tv_sec = millis / 1000;
 delay.tv_nsec = (millis % 1000) * 1000000L;
 nanosleep(&delay, (struct timespec*) NULL);
 
 // getting the current time
 time(&t);
 currentTime = localtime(&t);
 
 led_clear(0, 0, 0);
 // tm_year = the number of years since 1900
 display(currentTime->tm_year - 100, year, 0, INTENSITY, 0);
 display(currentTime->tm_mon + 1, month, 0, 0, INTENSITY);
 display(currentTime->tm_mday, day, INTENSITY, 0, 0);
 display(currentTime->tm_hour, hour, 0, 0, INTENSITY);
 display(currentTime->tm_min, minute, INTENSITY, 0, 0);
 display(currentTime->tm_sec, second, 0, INTENSITY, 0);
 
 }
}
Editor is loading...