thermferm/lcd-buffer.c

Sat, 25 Apr 2020 20:31:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 25 Apr 2020 20:31:31 +0200
changeset 605
e00f8ff4de9a
parent 484
7362ebd40f26
child 614
389097dc665d
permissions
-rw-r--r--

Version 0.9.8. Added extra path to the fonts for Debian buster. Changed the PID to work on Proportional on Measurement. Added loops so that it looks like the PID is running at 100 mSec intervals.

/*****************************************************************************
 * Copyright (C) 2014-2015
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * mbsePi-apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with mbsePi-apps; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "thermferm.h"
#include "lcd-buffer.h"
#include "lcd-pcf8574.h"
#include "slcd.h"
#include "panel.h"


int			current_lines = 0;
int			current_offset = 0;
lcd_rows		*my_lcd_rows = NULL;

extern int		lcdHandle;
extern int		slcdHandle;
extern sys_config	Config;



/*
 * Reset the LCD screens chain, it will automatic rebuild.
 */
void lcd_buf_reset(void)
{
    lcd_rows	*tmp, *old;

    for (tmp = my_lcd_rows; tmp; tmp = old) {
	old = tmp->next;
	tmp->next = NULL;
	free(tmp);
    }
    current_lines = current_offset = 0;
    my_lcd_rows = NULL;
}



/*
 * Write to buffer array. Allocate more lines if needed.
 */
void lcd_buf_write(int row, const char *format, ...)
{
    char	buf[81 * sizeof(char)];
    va_list	va_ptr;
    lcd_rows	*tmp, *newrow;
    int		i = 0;

    va_start(va_ptr, format);
    vsnprintf(buf, (Config.lcd_cols + 1) * sizeof(char), format, va_ptr);
    va_end(va_ptr);

    /*
     * Check if the line in row is allocated. If not, do it
     * and make sure the chain is valid.
     */
    while (row > current_lines) {

	syslog(LOG_NOTICE, "lcd_buf_write(%d, ...) allocate new row", row);
	newrow = (lcd_rows *)malloc(sizeof(lcd_rows));
	newrow->next = NULL;
	snprintf(newrow->row, (Config.lcd_cols + 1) * sizeof(char), "                    ");

	if (my_lcd_rows == NULL) {
	    my_lcd_rows = newrow;
	} else {
	    for (tmp = my_lcd_rows; tmp; tmp = tmp->next) {
		if (tmp->next == NULL) {
		    tmp->next = newrow;
		    current_lines++;
		    break;
		}
	    }
	}
    }

    /*
     * Now update the text
     */
    for (tmp = my_lcd_rows; tmp; tmp = tmp->next) {
	i++;
	if (i == row) {
	    snprintf(tmp->row, Config.lcd_cols + 1, "%s", buf);
	    return;
	}
    }
    syslog(LOG_NOTICE, "lcd_buf_write(%d, ...) could not find row, i=%d", row, i);
}



void lcd_buf_step(int key)
{
    if (key == KEY_DOWN) {
	if (current_offset < (current_lines - Config.lcd_rows))
	    current_offset += Config.lcd_rows;
	else
	    current_offset = 0;
	syslog(LOG_NOTICE, "lcd_buf_step(KEY_DOWN) current_lines=%d current_offset=%d", current_lines, current_offset);
	lcd_buf_show();
    }
    if (key == KEY_UP) {
	if (current_offset >= Config.lcd_rows)
	    current_offset -= Config.lcd_rows;
	else
	    current_offset = (current_lines - Config.lcd_rows);
	syslog(LOG_NOTICE, "lcd_buf_step(KEY_UP) current_lines=%d current_offset=%d", current_lines, current_offset);
	lcd_buf_show();
    }
}



/*
 * This will be called from the main thread every second.
 */
void lcd_buf_show(void)
{
    int		i = 0, r = 0;
    lcd_rows	*tmp;

    for (tmp = my_lcd_rows; tmp; tmp = tmp->next) {
	if (i == current_offset)
	    break;
	i++;
    }

    if (tmp == NULL) {
	syslog(LOG_NOTICE, "lcd_buf_show: search after last record, current_offset=%d current_lines=%d i=%d", current_offset, current_lines, i);
	return;
    }

    for (r = 0; r < Config.lcd_rows; r++) {
#ifdef HAVE_WIRINGPI_H
	lcdPosition(lcdHandle, 0, r);
	lcdPuts(lcdHandle, tmp->row);
#endif
	slcdPosition(slcdHandle, 0, r);
	slcdPuts(slcdHandle, tmp->row);

	if (tmp->next != NULL)
	    tmp = tmp->next;
	else
	    tmp = my_lcd_rows;
    }
}

mercurial