Question

In: Computer Science

How do I make sure that this C code shows the letter P for one second...

How do I make sure that this C code shows the letter P for one second then L a second later on a raspberry pi sense hat?

How do I make sure that this C code shows the letter P for one second then L a second later on a raspberry pi sense hat?


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
13     // Letter P
14     bm->pixel[0][0]=WHITE;
15     bm->pixel[0][1]=WHITE;
16     bm->pixel[0][2]=WHITE;
17     bm->pixel[0][3]=WHITE;
18     bm->pixel[0][4]=WHITE;
19     bm->pixel[0][5]=WHITE;
20     bm->pixel[1][5]=WHITE;
21     bm->pixel[2][5]=WHITE;
22     bm->pixel[3][5]=WHITE;
23     bm->pixel[3][4]=WHITE;
24     bm->pixel[3][3]=WHITE;
25     bm->pixel[2][3]=WHITE;
26     bm->pixel[1][3]=WHITE;
27
28     /*
29     void drawCFB(void);
30     drawCFB();
31     */
32
33     sleep(1);
34
35     bm->pixel[0][0]=WHITE;                       
36     bm->pixel[0][1]=WHITE;
37     bm->pixel[0][2]=WHITE;
38     bm->pixel[0][3]=WHITE;
39     bm->pixel[0][4]=WHITE;
40     bm->pixel[0][5]=WHITE;
41     bm->pixel[1][0]=WHITE;
42     bm->pixel[2][0]=WHITE;
43     bm->pixel[3][0]=WHITE;
44
45     sleep(1);                                                                                           
46                                                                                                             
47     return 0;                                                                                               
48 }

Solutions

Expert Solution

I think the sleep() function you used is correct but since it may be sometimes that correct delay is not produced by it. 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;
 }


Related Solutions

How can I edit this C code to make sure that the letter P and L...
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;...
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
How do I add a method to make sure that the user inputs in uppercase only...
How do I add a method to make sure that the user inputs in uppercase only and anything entered in lower case throws an error as well as to make sure when they are halving the questioned number they don't enter any decimals? import java.util.*; public class TestCode { public static void main(String[] args) { String choice = "YES"; Random random = new Random(); Scanner scanner = new Scanner(System.in); ArrayList data = new ArrayList(); int count = 0,correct=0; while (!choice.equals("NO"))...
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
How do i make a point for line segment from p to infinite in java? in...
How do i make a point for line segment from p to infinite in java? in C++, it's Point p; Point extreme = {INF, p.y}; --------------------------------------------------------------------------------------------- boolean isInside(Point polygon[], int n, Point p) { // There must be at least 3 vertices in polygon[] if (n < 3) return false;    // Create a point for line segment from p to infinite //Point extreme = {INF, p.y};    p= Double.POSITIVE_INFINITY;    Point extreme = p.y; // Count intersections of the...
How do I make a template functions for these 2 functions to prevent code duplication These...
How do I make a template functions for these 2 functions to prevent code duplication These are the 2 functions 1st: virtual Card lead_card(const std::string &trump) {              int numOfTrump = 0;         for (int i = 0; i < int(hand.size()); ++i) {             if (hand[i].is_trump(trump) == true) {                 numOfTrump += 1;             }         }              if (numOfTrump != int(hand.size())) {             Card c = highestCardNotIncludingTrump(hand, trump);             int indexOfHighest = 0;             for (int i...
I was not sure how to utilize this line because I made code that works but...
I was not sure how to utilize this line because I made code that works but not with this line specifically. C++ Function 2: bool exists_trio_within_distance(int*,int,int); //Input:    //an integer array (param 1), its size (param 2), and    //a distance (param 3) //Output:    //True or false //Behavior:    //Returns true is there exists    //a sequence of 3 values in the array    //such that sum of the first two elements    //is equal to the third element...
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT