thermferm/lcd-buffer.c

Mon, 18 May 2015 21:19:06 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 18 May 2015 21:19:06 +0200
changeset 365
df0261bb3feb
parent 245
b01b6238eb67
child 418
0bfe08c7ba6e
permissions
-rw-r--r--

Version 0.3.3, still not for production. Fixed warnings when the simulator code is compiled. Slowed the simulator air temperature change 60 times. More realistic temperature changes for the heater and cooler elements. Improved logic in the simulator.

/*****************************************************************************
 * Copyright (C) 2014
 *   
 * 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 "panel.h"


#ifdef HAVE_WIRINGPI_H

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

extern int		lcdHandle;
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;
    }

    lcdPosition(lcdHandle, 0, r);
    lcdPuts(lcdHandle, tmp->row);

    r++;
    if (r < Config.lcd_rows) {
	if (tmp->next != NULL)
	    tmp = tmp->next;
	else
	    tmp = my_lcd_rows;
	lcdPosition(lcdHandle, 0, r);
	lcdPuts(lcdHandle, tmp->row);
    }
}



#endif

mercurial