thermferm/lcd-buffer.c

Mon, 15 Apr 2024 17:04:57 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 15 Apr 2024 17:04:57 +0200
changeset 678
cc49115e769e
parent 667
bba6ca418e09
permissions
-rw-r--r--

Better websocket broadcast messages. Added GLOBAL JSON command to the server. Better logic to trigger websocket and mqtt data updates for the fermenter units. Websocket receive added fermenter mode, stage, setpoints, switches. Added more css styles for the fermenter screen. Added the fermenter screen php and javascript.

/*****************************************************************************
 * 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