I am completely stuck on this can anyone help me with a plac
I am completely stuck on this, can anyone help me with a place to start or advice? I am in a C programingcourse.
You are working on using the DualShock 4 as a data source for a game, but you are having trouble visualizing the readings from the DualShock 4 as a function of time. It would be nice to plot a bar graph in real time showing either the roll or pitch of the DualShock 4, but you realize that you are in a hurry and don\'t want to deal with getting the graphics code right. Suddenly, you realize that you can graph these variables as a horizontal bar chart using characters on the screen and then let the lines scroll down the screen to form a moving bar graph.
For our purposes, roll is defined as the angle the DualShock 4 is tipped left or right while holding it normally.
Pitch is the angle from level that the DualShock 4 is tipped forward or backward.
In this lab, you will use skeleton code as your starting point to design and implement a modular DualShock 4 graphing application. Download the skeleton code lab7.c. You must use the skeleton code\'s function and basic structure to implement your program. Carefully read the skeleton program before beginning. It may help you focus your strategy. Take your time and carefully write and test individual functions in lab.
Because both roll and pitch may be positive or negative, you will need a bar graph where 0 is halfway across the screen at column 40. In the sample output below, if the value is 0 (or not enough to justify printing a character) output a 0 at column 40. If the value is greater than 0, output r\'s at column 40 and to the right. The number should be proportional to the value being graphed and such that pi/2 maps to 39 characters. Similarly, the l\'s should be used when the value is less than 0.
0
rrrrrrr
rrrr
r
llllllllll
llllllllllllllll
llllllllllllllllllllllllllllll
The above output would show a graph of 0, a slight positive trend of 3 positive readings, and 3 increasingly negative readings. When run, the program should graph roll. To switch to roll, the user should press the TRIANGLE button. To switch to pitch, the user should press the X button.
This is the provided code:
Solution
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589
#define TRUE 0
#define FALSE 1
//NO GLOBAL VARIABLES ALLOWED
int read_acc(double* a_x, double* a_y, double* a_z, int* time, int* Button_UP, int* Button_DOWN, int* Button_LEFT, int* Button_RIGHT);
double roll(double x_mag);
double pitch(double y_mag);
int scaleRadsForScreen(double rad);
void print_chars(int num, char use);
void graph_line(int number);
int main()
{
double x, y, z; // magnitude values of x, y, and z accelerations
int b_Up, b_Down, b_Left, b_Right; // variables to hold the button statuses
double roll_rad, pitch_rad; // value of the roll measured in radians
int scaled_value; // value of the roll adjusted to fit screen display
int t, q = 0, f;
//insert any beginning needed code here
//for( q = -25; q < 30; q+=5){
//graph_line( q );
//}
do
{
read_acc(&x , &y, &z, &t, &b_Up, &b_Down, &b_Left, &b_Right); // check for button input
if ( f-b_Down != 0 && b_Down == 1){
q = q + b_Down;
}
pitch_rad = pitch(y);
roll_rad = roll(x);
// calculate roll and pitch
// switch between roll and pitch(up vs. down button)
if (q % 2 == 1){ // Scale your output value
scaled_value = scaleRadsForScreen( roll_rad);
}
else {
scaled_value = scaleRadsForScreen( pitch_rad);
}
graph_line(scaled_value); // Output your graph line
f = b_Down;
fflush(stdout);
} while(b_Left != 1); // Modify to stop when left button is pressed
return 0;
}
//Functions
int read_acc(double* a_x, double* a_y, double* a_z, int* time, int* Button_UP, int* Button_DOWN, int* Button_LEFT, int* Button_RIGHT){
int j, s;
scanf(\"%d, %lf, %lf, %lf, %d, %d, %d, %d, %d, %d\", time, a_x, a_y, a_z, Button_UP, Button_DOWN, Button_LEFT, Button_RIGHT, &j, &s); //add joystick and slider if outputs only zeros
if (*Button_LEFT == 0){
return 1;
}
else{
return 0;
}
}
double roll(double x_mag){
if(x_mag > 1.0){
x_mag = 1.0;
}
if(x_mag < -1.0){
x_mag = -1.0;
}
return asin(x_mag);
}
double pitch(double y_mag){
if(y_mag > 1.0){
y_mag = 1.0;
}
if(y_mag < -1.0){
y_mag = -1.0;
}
return asin(y_mag);
}
int scaleRadsForScreen(double rad){
return (int) (rad * (39 * 2)/PI);
}
void print_chars(int num, char use){
int x;
for(x = 0; x < num; x++ ){
printf(\"%c\", use);
}
}
void graph_line(int number){
int x = 0;
if (number < 0){
x = number + 40;
print_chars( x, \' \');
print_chars( -number, \'l\');
}
else if ( number > 0) {
x = 40 - number;
print_chars( 40, \' \');
print_chars( number, \'r\');
}
else{
print_chars( 40, \' \');
print_chars ( 1, \'0\');
}
print_chars(1, \'\ \');
}



