thermferm/lcd-buffer.c

Thu, 25 Apr 2024 14:26:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 25 Apr 2024 14:26:47 +0200
changeset 708
13555c27b592
parent 667
bba6ca418e09
permissions
-rw-r--r--

Version 0.9.19a6. Fixes after a short trial on the production controller. Fixed json for alternate beer termperature sensor. Fixed division by 1000 for the room temperature and humidity values. The dropdown list for devices shows the address instead of description in the list.

/*****************************************************************************
 * Copyright (C) 2014-2024
 *   
 * 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) {

	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;
	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);
	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