Print data just once - c

i need to make somekind of "on-off" button with my project.
when the button not pressed, it supposed to print
stopped
if pressed
started
the problem is, when im not pressin it, it keep printin 'stopped', same when i keep pushin the button. i want it to only print the data once.
more detail, what i need is, the button hold the stay in 'STARTED' position until i press it again.
here is my code
{
int main (void)
int TestM4;
while(1)
{
if (!(PORTJ_IN&PIN1_bm)) //test m4
{
testM4 = 1;
printf("%d\n", testM4);
}
else
{
testM4 = 0;
printf("%d\n", testM4);
}

while(1)
This runs the while loop continuously.
So, it checks the buttons, prints out the statements, and then goes back straightaway to check them again.
To run it only once, remove the while loop altogether, but then the program will end pretty quickly.
Perhaps, look at ways to pause the program.

I don't know all the code, so while(1) could be good or bad code, but you can test as a flag to change it and print once only.
while(1)
{
if (!(PORTJ_IN&PIN1_bm)) //test m4
{
if(testM4 != 1) // status as button up
{
testM4 = 1;
printf("%d\n", testM4);
}
}
else
{
if(testM4 != 0)
{
testM4 = 0;
printf("%d\n", testM4);
}
}
}

Related

C: Bus Error between function returns and execution goes back to parent function

To simplify the problem as much as possible, I have two functions, a parent that calls the child. Everything executes okay till it gets to the return of the child function. After that I get a Bus Error.
int main () {
game();
// this doesn't get executed and program fails with bus error
printf("Execute 2");
return 1;
}
int game () {
game_t GameInfo = {.level = 1, .score = 0, .playerCh = 0, .playerX = 1, .playerY = 1};
gameLevel(&GameInfo);
mvprintw(1,1, "Executed");
// code works up to here and get's executed properly
return 1;
};
void gameLevel (game_t *GameInfo) {
// determine the size of the game field
int cellCols = COLS / 3;
int cellRows = (LINES / 3) - 2;
GameInfo -> playerX = 1;
GameInfo -> playerY = 1;
generateMaze(0);
int solved = 0;
int level = GameInfo -> level;
// default player position
getPlayerDefault(GameInfo);
pthread_t enemies_th;
pthread_create(&enemies_th, NULL, enemies, (void *)GameInfo);
// enemies(&level);
while (solved == 0 && GameInfo -> collision != 1) {
printGameInfo(GameInfo);
noecho();
char move = getch();
echo();
if (GameInfo -> collision != 1) {
if (checkMoveValidity(move, GameInfo) == 1) {
solved = movePlayer(move, GameInfo);
if (solved == 1) {
break;
}
}
} else {
break;
}
}
if (solved == 1) {
pthread_cancel(enemies_th);
GameInfo->level++;
gameLevel(GameInfo);
} else {
// game over
pthread_cancel(enemies_th);
return;
}
}
Now, the code is much more complicated than here, but I think that shouldn't have any influence on this (?) as it executes properly, until the return statement. There is also ncurses and multithreading, quite complex custom structures, but it all works, up until that point. Any ideas ?
Tried putting print statements after each segment of code, everything worked up until this.
pthread_cancel() doesn't terminate the requested thread immediately. The only way to know that a cancelled thread has terminated is to call pthread_join(). If the thread is left running, it will interfere with use of the GameInfo variable in the next level of the game if the current level is solved, or may use the GameInfo variable beyond its lifetime if the current level was not solved and the main thread returns back to the main() function.
To make sure the old enemies thread has terminated, add calls to pthread_join() to the gameLevel() function as shown below:
if (solved == 1) {
pthread_cancel(enemies_th);
pthread_join(enemies_th);
GameInfo->level++;
gameLevel(GameInfo);
} else {
// game over
pthread_cancel(enemies_th);
pthread_join(enemies_th);
return;
}
The use of tail recursion in gameLevel() seems unnecessary. I recommend returning the solved value and letting the game() function start the next level:
In game():
while (gameLevel(&GameInfo)) {
GameInfo.level++;
}
In gameLevel():
int gameLevel(game_t *GameInfo) {
/* ... */
pthread_cancel(enemies_th);
pthread_join(enemies_th);
return solved;
}

C clock program

I was trying to create a clock program with c using the eclipse ide.
When I run the program it keeps executing every single count.
I used system("cls"). Also tried "clear"
It shows no error but it is not working. I want the screen cleared after every count. It is showing a ⍰ symbol sometimes.
https://i.stack.imgur.com/0ElDL.png ---This is what I got.
int main(void)
{
int hour,minute,second;
hour=minute=second=0;
setbuf(stdout,NULL);
while(1)
{
system("cls");
printf("%02d:%02d:%02d\n",hour,minute,second);
fflush(stdout);
second++;
if(second==60)
{
minute += 1;
second=0;
}
if(minute==60)
{
hour += 1;
minute=0;
}
if(hour==24)
{
hour=0;
minute=0;
second=0;
}
sleep(1);
}
return 0;
.
try using system("clear");
According to example published above you need only one line to display clock. It's easier to add carriage return:
while(1)
{
printf("%02d:%02d:%02d\r",hour,minute,second);
fflush(stdout);
...
}

Priority Queue synchronization with pthreads

I'm working on a college assignment where we are to implement parallelized A* search for a 15 puzzle. For this part, we are to use only one priority queue (I suppose to see that the contention by multiple threads would limit speedup). A problem I am facing is properly synchronizing popping the next "candidate" from the priority queue.
I tried the following:
while(1) {
// The board I'm trying to pop.
Board current_board;
pthread_mutex_lock(&priority_queue_lock);
// If the heap is empty, wait till another thread adds new candidates.
if (pq->heap_size == 0)
{
printf("Waiting...\n");
pthread_mutex_unlock(&priority_queue_lock);
continue;
}
current_board = top(pq);
pthread_mutex_unlock(&priority_queue_lock);
// Generate the new boards from the current one and add to the heap...
}
I've tried different variants of the same idea, but for some reason there are occasions where the threads get stuck on "Waiting". The code works fine serially (or with two threads), so that leads me to believe this is the offending part of the code. I can post the entire thing if necessary. I feel like it's an issue with my understanding of the mutex lock though. Thanks in advance for help.
Edit:
I've added the full code for the parallel thread below:
// h and p are global pointers initialized in main()
void* parallelThread(void* arg)
{
int thread_id = (int)(long long)(arg);
while(1)
{
Board current_board;
pthread_mutex_lock(&priority_queue_lock);
current_board = top(p);
pthread_mutex_unlock(&priority_queue_lock);
// Move blank up.
if (current_board.blank_x > 0)
{
int newpos = current_board.blank_x - 1;
Board new_board = current_board;
new_board.board[current_board.blank_x][current_board.blank_y] = new_board.board[newpos][current_board.blank_y];
new_board.board[newpos][current_board.blank_y] = BLANK;
new_board.blank_x = newpos;
new_board.goodness = get_goodness(new_board.board);
new_board.turncount++;
if (check_solved(new_board))
{
printf("Solved in %d turns",new_board.turncount);
exit(0);
}
if (!exists(h,new_board))
{
insert(h,new_board);
push(p,new_board);
}
}
// Move blank down.
if (current_board.blank_x < 3)
{
int newpos = current_board.blank_x + 1;
Board new_board = current_board;
new_board.board[current_board.blank_x][current_board.blank_y] = new_board.board[newpos][current_board.blank_y];
new_board.board[newpos][current_board.blank_y] = BLANK;
new_board.blank_x = newpos;
new_board.goodness = get_goodness(new_board.board);
new_board.turncount++;
if (check_solved(new_board))
{
printf("Solved in %d turns",new_board.turncount);
exit(0);
}
if (!exists(h,new_board))
{
insert(h,new_board);
push(p,new_board);
}
}
// Move blank right.
if (current_board.blank_y < 3)
{
int newpos = current_board.blank_y + 1;
Board new_board = current_board;
new_board.board[current_board.blank_x][current_board.blank_y] = new_board.board[current_board.blank_x][newpos];
new_board.board[current_board.blank_x][newpos] = BLANK;
new_board.blank_y = newpos;
new_board.goodness = get_goodness(new_board.board);
new_board.turncount++;
if (check_solved(new_board))
{
printf("Solved in %d turns",new_board.turncount);
exit(0);
}
if (!exists(h,new_board))
{
insert(h,new_board);
push(p,new_board);
}
}
// Move blank left.
if (current_board.blank_y > 0)
{
int newpos = current_board.blank_y - 1;
Board new_board = current_board;
new_board.board[current_board.blank_x][current_board.blank_y] = new_board.board[current_board.blank_x][newpos];
new_board.board[current_board.blank_x][newpos] = BLANK;
new_board.blank_y = newpos;
new_board.goodness = get_goodness(new_board.board);
new_board.turncount++;
if (check_solved(new_board))
{
printf("Solved in %d turns",new_board.turncount);
exit(0);
}
if (!exists(h,new_board))
{
insert(h,new_board);
push(p,new_board);
}
}
}
return NULL;
}
I tried the following:
I don't see anything wrong with the code that follows, assuming that top also removes the board from the queue. It's wasteful (if the queue is empty, it will spin locking and unlocking the mutex), but not wrong.
I've added the full code
This is useless without the code for exists, insert and push.
One general observation:
pthread_mutex_lock(&priority_queue_lock);
current_board = top(p);
pthread_mutex_unlock(&priority_queue_lock);
In the code above, your locking is "ouside" of the top function. But here:
if (!exists(h,new_board))
{
insert(h,new_board);
push(p,new_board);
}
you either do no locking at all (in which case that's a bug), or you do locking "inside" exists, insert and push.
You should not mix "inside" and "outside" locking. Pick one or the other and stick with it.
If you in fact do not lock the queue inside exists, insert, etc. then you have a data race and are thinking of mutexes incorrectly: they protect invariants, and you can't check whether the queue is empty in parallel with another thread executing "remove top element" -- these operations require serialization, and thus must both be done under a lock.

SDL_GetKeyboardState not working

I am trying to make a controller for a game with SDL 2(didn't want to ask on gamedev since it is not a game issue directly) I use SDL_GetKeyboardEvent to see if the navigation arrows are being pressed but it apparently doesn't work, it is supposed to print a value 1 or -1 if one of those keys is pressed but it doesn't it just prints 0 even if I hold the key down for several seconds, it is like it doesn't detect that the key is being pressed. I searched all over the internet and this is how they do it, but it doesn't work for me.
#define SDL_MAIN_HANDLED
#include <stdio.h>
#include "SDL.h"
/* I'll add some code later
so something that isn't used
might be initialized
*/
int main (void)
{
int a = 1;
int x;
int z;
SDL_Event quit;
const Uint8 *keys = SDL_GetKeyboardState(NULL);
SDL_Init(SDL_INIT_VIDEO);
while(a)
{
SDL_PollEvent(&quit);
if(quit.type == SDL_QUIT)
a = 0;
if(keys[SDL_SCANCODE_UP])
z = 1;
else if(keys[SDL_SCANCODE_DOWN])
z = -1;
else
z = 0;
if(keys[SDL_SCANCODE_LEFT])
x = -1;
else if(keys[SDL_SCANCODE_RIGHT])
x = 1;
else
x = 0;
printf("%d, %d\n", x, z);
//This is supposed to print
//x, z values so if up arrow is pressed it will print 0, 1 and if
//down arrow is pressed it will print 0, -1: the same with horizontal ones.
//1 or -1, 1 or -1
}
SDL_Quit();
return 0;
}
Read the documentation: wiki
Note: This function gives you the current state after all events have been processed, so if a key or button has been pressed and released before you process events, then the pressed state will never show up in the SDL_GetKeyboardState() calls
What it means is?
You need to process all events. How? Looping the PollEvent, after the loop (or if you want to check in the loop, check at the end), the SDL_GetKeyboardState is usable.
So, go through the loop, check for keyboards states. Do not forget to always go through the loop before checking for keys
e.g.
while (game)
{
/*! updates the array of keystates */
while ((SDL_PollEvent(&e)) != 0)
{
/*! request quit */
if (e.type == SDL_QUIT)
{
game = false;
}
}
if (keys[SDL_SCANCODE_RIGHT])
std::cout << "Right key";
}
You'll need to create a window with SDL_SetVideoMode to get mouse and keyboard events.
Here is my InputManager update function that updates the user input from the keyboard and mouse. Notice the const_cast needed to be able to update the class variable Uint8* keysArray;. This way, more than one event can be processed at once.
void update() {
SDL_PumpEvents();
// update keyboard state
keysArray = const_cast <Uint8*> (SDL_GetKeyboardState(NULL));
if (keysArray[SDL_SCANCODE_RETURN])
printf("MESSAGE: <RETURN> is pressed...\n");
if (keysArray[SDL_SCANCODE_RIGHT] && keysArray[SDL_SCANCODE_UP])
printf("MESSAGE: Right and Up arrows are pressed...\n");
// update mouse location and button states
SDL_GetMouseState(&x, &y);
getMouseButtonStates();
}

Lag using Xlib/X11 event handling in C

Beginner user of C here.
I'm trying to build a library in C using X11/Xlib so I can use it just for little projects and I'm running into a problem when trying to handle events to get input(button presses and key presses) from the user. It works fine for a while and then it starts to build up a significant lag over time.
Right now what I have is my program checking if there is an event waiting and if there is, retrieving it.
I think that my problem right now is that the events are getting stored in memory and its bogging down the program. But that's just a total guess.
Any help will be appreciated. Thank you.
EDIT: Forgot code (I knew I forgot something)
The two functions in question are:
int event_waiting()
{
XEvent event;
if(XCheckMaskEvent(dspy,-1,&event)) {
if(event.type==KeyPress) {
XPutBackEvent(dspy,&event);
return 1;
} else if (event.type==ButtonPress) {
XPutBackEvent(dspy,&event);
return 1;
}
} /* <<=== added missing close-curly here */
return 0;
}
char wait()
{
XEvent event;
XNextEvent(dspy,&event);
if(event.type==KeyPress) {
saved_x = event.xkey.x;
saved_y = event.xkey.y;
return XLookupKeysym(&event.xkey,0);
} else if(event.type==ButtonPress) {
saved_x = event.xkey.x;
saved_y = event.xkey.y;
return event.xbutton.button;
}
}
And then they are called in the main like so,
if (event_waiting()){
char c = wait();
//Switch case goes here
}
EDIT 2: UPDATED CODE
XEvent event;
if(XCheckMaskEvent(display,-1,&event))
{
if(event.type==KeyPress) {
XPutBackEvent(display,&event);
return 1;
} else if (event.type==ButtonPress) {
XPutBackEvent(display,&event);
return 1;
}
}
XFlush(display);
return 0;
`
The lag, which gets worse over time, means that you have many untouched events in your event queue, which slows down XCheckMaskEvent().
Try specifying events using XSelectInput(... ButtonPressMask | KeyPressMask), and try flushing the event queue using XFlush() if there is no event in which you are interested:
if(event.type==KeyPress) {
XPutBackEvent(dspy,&event);
return 1;
} else if (event.type==ButtonPress) {
XPutBackEvent(dspy,&event);
return 1;
} else {
XFlush(dspy); // this
}

Resources