I am trying to draw a box around some text that will be updated. In the place holder the text is static, but flickering is still observed. My code is below
/*
* @file main.cc
* @author Tom Eaton
* @date 09 Jan 2018
* @brief Command line interface to show Hawkcell PCB data.
*/
#include <iostream>
#include <string>
#include <cstring>
#include <ncurses.h>
using namespace std;
/*
* Prints string in the horizontal center of row specified.
* @param row - Row that text should be printed on
* @param mesg - Pointer to char array containing message
*/
void printHorizCenter(int row, char* mesg) {
mvprintw(row, ((COLS - strlen(mesg))/2)-1, mesg);
}
/*
* Reads version of PCB and updates char array with version
* @param outVersion - Pointer to char array where version should be stored.
* @param port - Reference to serial port
*/
void printHorizBlock(int row, int startCol, int endCol) {
for(int i = startCol; i<=endCol; i++) {
mvaddch(row, i, ACS_BLOCK);
}
}
int main(int argc, char** argv) {
//Initialise curses
initscr();
cbreak();
echo();
keypad(stdscr, 1);
//Make character capture non blocking
nodelay(stdscr, 1);
//Define char arrays for each cell
char cellOne[2] = {'1', '\n'};
char cellTwo[2] = {'2', '\n'};
char cellThree[2] = {'3', '\n'};
char cellFour[2] = {'4', '\n'};
//Setup curses colour schemes
start_color();
use_default_colors();
curs_set(0);
//Setup curses colour pairs
init_pair(1, COLOR_RED, -1);
init_pair(2, COLOR_WHITE, COLOR_RED);
WINDOW *win = newwin(10, 20, 1, 0);
//Main loop
while (1) {
if(getch() == 12) {
endwin();
cerr << "You pressed left and exited" << endl;
exit(1);
}
//Print top title bar
attron(COLOR_PAIR(1));
printHorizBlock(0, 0, (COLS/2) - (strlen("HawkCell")/2));
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
printHorizCenter(0, (char *)"HawkCell");
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(1));
printHorizBlock(0, (COLS/2) + (strlen("HawkCell")/2) - 1, COLS);
attroff(COLOR_PAIR(1));
//Print voltage data
mvprintw(3, 1, "Cell 1: %.3fV\n", 4.0f);
mvprintw(4, 1, "Cell 2: %.3fV\n", 4.0f);
mvprintw(5, 1, "Cell 3: %.3fV\n", 4.0f);
mvprintw(6, 1, "Cell 4: %.3fV\n", 4.0f);
mvprintw(7, 1, "Total: %.3fV\n", 4.0f);
//Print bottom info bar
attron(COLOR_PAIR(1));
printHorizBlock(LINES-1, 0, COLS);
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
printHorizCenter(LINES-1, (char *)"ctrl+c: Exit, ctrl+l: Toggle logging ");
attroff(COLOR_PAIR(2));
//Call curses refresh function to update screen
box(win, 0, 0);
//touchwin(win);
wrefresh(win);
wrefresh(stdscr);
}
getch();
endwin();
return 0;
}
The problem is that there is flickering when the screen is redrawn, shown in the gif below (Photo sensitivity warning)Gif showing problem. As you can see it only happens on the lines containing text, which makes me think that the whole line is being drawn when writing the text, not just the specific characters.
I have tried using a various combination of wrefresh()
, touchwin()
, erase()
and refresh()
but I couldn't find any that did not produce flickering.
How do I stop this flickering?