In: Computer Science
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi?
1 #include <stdio.h>
2 #include <unistd.h>
3 #include "sense.h"
4
5 #define WHITE 0xFFFF
6
7 int main(void) {
8 // getFrameBuffer should only get called
once/program
9 pi_framebuffer_t
*fb=getFrameBuffer();
10 sense_fb_bitmap_t
*bm=fb->bitmap;
11
12 bm->pixel[0][0]=WHITE;
13 bm->pixel[0][1]=WHITE;
14 bm->pixel[0][2]=WHITE;
15 bm->pixel[0][3]=WHITE;
16 bm->pixel[0][4]=WHITE;
17 bm->pixel[0][5]=WHITE;
18 bm->pixel[1][5]=WHITE;
19 bm->pixel[2][5]=WHITE;
20 bm->pixel[3][5]=WHITE;
21 bm->pixel[3][4]=WHITE;
22 bm->pixel[3][3]=WHITE;
23 bm->pixel[2][3]=WHITE;
24 bm->pixel[1][3]=WHITE;
25
26
27 sleep(1);
28
29
30 bm->pixel[0][0]=WHITE;
31 bm->pixel[0][1]=WHITE;
32 bm->pixel[0][2]=WHITE;
33 bm->pixel[0][3]=WHITE;
34 bm->pixel[0][4]=WHITE;
35 bm->pixel[0][5]=WHITE;
36 bm->pixel[1][0]=WHITE;
37 bm->pixel[2][0]=WHITE;
38 bm->pixel[3][0]=WHITE;
39
40
41 /*
42 void drawCFB(void);
43 drawCFB();
44 */
45
46 sleep(1);
47 freeFrameBuffer(fb);
48 return 0;
49 }
50
The sleep() function you used is correct but since it may be sometimes the correct delay is not produced. In that case you can use the following delay function in your code.
I was not able to test your code since I do not have "sense.h" header file (as it may be your own custom header file) and also that rasberry pi apparatus too, at my home. So, I cannot see the ouput of given code.
Let me know of the issues if any in the comments below.
I hope the given delay function serves your purpose of adding one second delay.
// C function to implement the time delay functionality
#include <stdio.h>
// To use time library of C
#include <time.h>
void delay_func(int s)
{
// To convert given time in seconds to milliseconds
int m_s = 1000 * s;
// To note the start time i.e. the current time of the system clock
clock_t sys_t = clock();
// looping until the required time is not achieved
while (clock() < sys_t + m_s);
}
// Main code to test the above functionality
int main()
{
int n
for (n = 0; n < 100; n++)
{
// delay of one second
delay_func(1);
printf("%d seconds have passed\n", n + 1);
}
return 0;
}
Output Screenshot
The required code with the delay functionality added. Also I have changed some of the pixel arrangement for L to make L look better. If you want you can keep the arrangement or otherwise revert to your old one.
#include <stdio.h>
#include <unistd.h>
// To use time library of C
#include <time.h>
#include "sense.h"
#define WHITE 0xFFFF
void delay_func(int s)
{
// To convert given time in seconds to milliseconds
int m_s = 1000 * s;
// To note the start time i.e. the current time of the system clock
clock_t sys_t = clock();
// looping until the required time is not achieved
while (clock() < sys_t + m_s);
}
int main(void) {
// getFrameBuffer should only get called once/program
pi_framebuffer_t *fb=getFrameBuffer();
sense_fb_bitmap_t *bm=fb->bitmap;
// New Pixel arrangement for P
bm->pixel[0][0]=WHITE;
bm->pixel[0][1]=WHITE;
bm->pixel[0][2]=WHITE;
bm->pixel[0][3]=WHITE;
bm->pixel[0][4]=WHITE;
bm->pixel[0][5]=WHITE;
bm->pixel[1][5]=WHITE;
bm->pixel[2][5]=WHITE;
// bm->pixel[3][5]=WHITE;
bm->pixel[3][4]=WHITE;
//bm->pixel[3][3]=WHITE;
bm->pixel[2][3]=WHITE;
//bm->pixel[1][3]=WHITE;
// Added a new pixel value
bm->pixel[1][2]=WHITE;
//sleep(1);
//using delay() function instead of sleep()
delay_func(1);
// Pixel arrangement for L
bm->pixel[0][0]=WHITE;
bm->pixel[0][1]=WHITE;
bm->pixel[0][2]=WHITE;
bm->pixel[0][3]=WHITE;
bm->pixel[0][4]=WHITE;
bm->pixel[0][5]=WHITE;
bm->pixel[1][0]=WHITE;
bm->pixel[2][0]=WHITE;
bm->pixel[3][0]=WHITE;
/*
void drawCFB(void);
drawCFB();
*/
//sleep(1);
//I think delay is not needed here
//but adding it since your in code sleep is inserted here
delay_func(1);
freeFrameBuffer(fb);
return 0;
}