2013-05-24 18:00:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/clcd/slcd_main.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-05-25 21:46:43 +02:00
|
|
|
#include <nuttx/lcd/slcd_ioctl.h>
|
2013-05-24 18:00:54 +02:00
|
|
|
#include <nuttx/lcd/slcd_codec.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_SLCD_DEVNAME
|
2018-11-25 21:01:37 +01:00
|
|
|
# define CONFIG_EXAMPLES_SLCD_DEVNAME "/dev/slcd0"
|
2013-05-24 18:00:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Typs
|
|
|
|
****************************************************************************/
|
2020-03-18 18:03:30 +01:00
|
|
|
|
2013-05-24 18:00:54 +02:00
|
|
|
/* All state information for this test is kept within the following structure
|
|
|
|
* in order create a namespace and to minimize the possibility of name
|
|
|
|
* collisions.
|
2013-05-26 17:28:57 +02:00
|
|
|
*
|
|
|
|
* NOTE: stream must be the first element of struct slcd_test_s to support
|
|
|
|
* casting between these two types.
|
2013-05-24 18:00:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct slcd_test_s
|
|
|
|
{
|
2013-05-27 15:26:59 +02:00
|
|
|
struct lib_outstream_s stream; /* Stream to use for all output */
|
|
|
|
struct slcd_attributes_s attr; /* Size of the SLCD (rows x columns) */
|
|
|
|
int fd; /* File descriptor or the open SLCD device */
|
|
|
|
bool initialized; /* TRUE: Initialized */
|
|
|
|
uint8_t currow; /* Next row to display */
|
2013-05-24 18:00:54 +02:00
|
|
|
|
|
|
|
/* The I/O buffer */
|
|
|
|
|
2013-05-24 19:11:48 +02:00
|
|
|
uint8_t buffer[CONFIG_EXAMPLES_SLCD_BUFSIZE];
|
2013-05-24 18:00:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct slcd_test_s g_slcdtest;
|
2013-05-24 19:11:48 +02:00
|
|
|
static const char g_slcdhello[] = "Hello";
|
2013-05-24 18:00:54 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-05-24 19:11:48 +02:00
|
|
|
* Name: slcd_dumpbuffer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Do a pretty buffer dump
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-18 18:03:30 +01:00
|
|
|
void slcd_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
|
|
|
|
unsigned int buflen)
|
2013-05-24 19:11:48 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
printf("%s:\n", msg);
|
2013-05-24 19:11:48 +02:00
|
|
|
for (i = 0; i < buflen; i += 32)
|
|
|
|
{
|
|
|
|
printf("%04x: ", i);
|
|
|
|
for (j = 0; j < 32; j++)
|
|
|
|
{
|
|
|
|
k = i + j;
|
|
|
|
|
|
|
|
if (j == 16)
|
|
|
|
{
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k < buflen)
|
|
|
|
{
|
|
|
|
printf("%02x", buffer[k]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" ");
|
|
|
|
for (j = 0; j < 32; j++)
|
|
|
|
{
|
|
|
|
k = i + j;
|
|
|
|
|
|
|
|
if (j == 16)
|
|
|
|
{
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k < buflen)
|
|
|
|
{
|
|
|
|
if (buffer[k] >= 0x20 && buffer[k] < 0x7f)
|
|
|
|
{
|
|
|
|
printf("%c", buffer[k]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2020-03-18 18:03:30 +01:00
|
|
|
}
|
2013-05-24 19:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: slcd_flush
|
2013-05-24 18:00:54 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int slcd_flush(FAR struct lib_outstream_s *stream)
|
|
|
|
{
|
|
|
|
FAR struct slcd_test_s *priv = (FAR struct slcd_test_s *)stream;
|
2013-05-24 19:11:48 +02:00
|
|
|
FAR const uint8_t *buffer;
|
2013-05-24 18:00:54 +02:00
|
|
|
ssize_t nwritten;
|
|
|
|
ssize_t remaining;
|
|
|
|
|
|
|
|
/* Loop until all bytes were written (handling both partial and interrupted
|
|
|
|
* writes).
|
|
|
|
*/
|
|
|
|
|
|
|
|
remaining = stream->nput;
|
2013-05-24 19:11:48 +02:00
|
|
|
buffer = priv->buffer;
|
|
|
|
|
|
|
|
slcd_dumpbuffer("WRITING", buffer, remaining);
|
|
|
|
|
|
|
|
while (remaining > 0)
|
2013-05-24 18:00:54 +02:00
|
|
|
{
|
2013-05-24 19:11:48 +02:00
|
|
|
nwritten = write(priv->fd, buffer, remaining);
|
2013-05-24 18:00:54 +02:00
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
printf("write failed: %d\n", errcode);
|
|
|
|
|
|
|
|
if (errcode != EINTR)
|
|
|
|
{
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remaining -= nwritten;
|
2013-05-24 19:11:48 +02:00
|
|
|
buffer += nwritten;
|
2013-05-24 18:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the stream */
|
|
|
|
|
|
|
|
stream->nput = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: slcd_putc
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void slcd_putc(FAR struct lib_outstream_s *stream, int ch)
|
|
|
|
{
|
|
|
|
FAR struct slcd_test_s *priv = (FAR struct slcd_test_s *)stream;
|
|
|
|
|
|
|
|
/* Write the character to the buffer */
|
|
|
|
|
|
|
|
priv->buffer[stream->nput] = ch;
|
|
|
|
stream->nput++;
|
|
|
|
priv->buffer[stream->nput] = '\0';
|
|
|
|
|
|
|
|
/* If the buffer is full, flush it */
|
|
|
|
|
2013-05-24 19:11:48 +02:00
|
|
|
if (stream->nput >= CONFIG_EXAMPLES_SLCD_BUFSIZE)
|
2013-05-24 18:00:54 +02:00
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
slcd_flush(stream);
|
2013-05-24 18:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: slcd_puts
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void slcd_puts(FAR struct lib_outstream_s *outstream,
|
|
|
|
FAR const char *str)
|
|
|
|
{
|
|
|
|
for (; *str; str++)
|
|
|
|
{
|
|
|
|
slcd_put((int)*str, outstream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* slcd_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2013-05-24 18:00:54 +02:00
|
|
|
{
|
|
|
|
FAR struct slcd_test_s *priv = &g_slcdtest;
|
2013-05-24 20:00:15 +02:00
|
|
|
FAR const char *str = g_slcdhello;
|
2013-05-26 19:26:34 +02:00
|
|
|
int fd;
|
2013-05-24 18:00:54 +02:00
|
|
|
int ret;
|
|
|
|
|
2013-05-24 20:00:15 +02:00
|
|
|
/* Parse the command line. For now, only a single optional string argument
|
|
|
|
* is supported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
str = argv[1];
|
|
|
|
}
|
2013-05-26 19:26:34 +02:00
|
|
|
|
|
|
|
/* Open the SLCD device */
|
|
|
|
|
|
|
|
printf("Opening %s for read/write access\n", CONFIG_EXAMPLES_SLCD_DEVNAME);
|
|
|
|
|
|
|
|
fd = open(CONFIG_EXAMPLES_SLCD_DEVNAME, O_RDWR);
|
2020-03-18 14:56:27 +01:00
|
|
|
if (fd < 0)
|
2013-05-26 19:26:34 +02:00
|
|
|
{
|
|
|
|
printf("Failed to open %s: %d\n", CONFIG_EXAMPLES_SLCD_DEVNAME, errno);
|
|
|
|
goto errout;
|
|
|
|
}
|
2013-05-24 20:00:15 +02:00
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
/* Are we already initialized? */
|
|
|
|
|
|
|
|
if (!priv->initialized)
|
|
|
|
{
|
2013-05-27 19:39:13 +02:00
|
|
|
unsigned long brightness;
|
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
/* Initialize the output stream */
|
2013-05-24 18:00:54 +02:00
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
memset(priv, 0, sizeof(struct slcd_test_s));
|
|
|
|
priv->stream.put = slcd_putc;
|
2013-05-24 18:00:54 +02:00
|
|
|
#ifdef CONFIG_STDIO_LINEBUFFER
|
2013-05-26 17:28:57 +02:00
|
|
|
priv->stream.flush = slcd_flush;
|
2013-05-24 18:00:54 +02:00
|
|
|
#endif
|
2020-03-18 14:56:27 +01:00
|
|
|
priv->fd = fd;
|
2013-05-24 18:00:54 +02:00
|
|
|
|
2013-05-27 15:26:59 +02:00
|
|
|
/* Get the attributes of the SCLD device */
|
2013-05-24 18:00:54 +02:00
|
|
|
|
2013-05-27 15:26:59 +02:00
|
|
|
ret = ioctl(fd, SLCDIOC_GETATTRIBUTES, (unsigned long)&priv->attr);
|
2013-05-26 17:28:57 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-05-27 15:26:59 +02:00
|
|
|
printf("ioctl(SLCDIOC_GETATTRIBUTES) failed: %d\n", errno);
|
2013-05-26 17:28:57 +02:00
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:39:13 +02:00
|
|
|
printf("Attributes:\n");
|
|
|
|
printf(" rows: %d columns: %d nbars: %d\n",
|
2013-05-27 15:26:59 +02:00
|
|
|
priv->attr.nrows, priv->attr.ncolumns, priv->attr.nbars);
|
2013-05-27 19:39:13 +02:00
|
|
|
printf(" max contrast: %d max brightness: %d\n",
|
|
|
|
priv->attr.maxcontrast, priv->attr.maxbrightness);
|
2013-05-26 17:28:57 +02:00
|
|
|
|
|
|
|
/* Home the cursor and clear the display */
|
|
|
|
|
|
|
|
printf("Clear screen\n");
|
|
|
|
slcd_encode(SLCDCODE_CLEAR, 0, &priv->stream);
|
|
|
|
slcd_flush(&priv->stream);
|
|
|
|
|
2013-05-27 19:39:13 +02:00
|
|
|
/* Set the brightness to the mid value */
|
|
|
|
|
|
|
|
brightness = ((unsigned long)priv->attr.maxbrightness + 1) >> 1;
|
2014-11-25 21:51:10 +01:00
|
|
|
printf("Set brightness to %lu\n", brightness);
|
2013-05-27 19:39:13 +02:00
|
|
|
|
|
|
|
ret = ioctl(fd, SLCDIOC_SETBRIGHTNESS, brightness);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Report the ioctl failure, but do not error out. Some SLCDs
|
|
|
|
* do not support brightness settings.
|
|
|
|
*/
|
|
|
|
|
|
|
|
printf("ioctl(SLCDIOC_GETATTRIBUTES) failed: %d\n", errno);
|
|
|
|
}
|
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
priv->initialized = true;
|
2013-05-24 18:00:54 +02:00
|
|
|
}
|
|
|
|
|
2013-05-26 19:26:34 +02:00
|
|
|
/* Save the file descriptor in a place where slcd_flush can find it */
|
|
|
|
|
|
|
|
priv->fd = fd;
|
|
|
|
|
|
|
|
/* Set the cursor to the beginning of the current row by homing the cursor
|
|
|
|
* then going down as necessary, and erase to the end of the line.
|
2013-05-26 17:28:57 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
slcd_encode(SLCDCODE_HOME, 0, &priv->stream);
|
|
|
|
slcd_encode(SLCDCODE_DOWN, priv->currow, &priv->stream);
|
|
|
|
slcd_encode(SLCDCODE_ERASEEOL, 0, &priv->stream);
|
2013-05-24 18:00:54 +02:00
|
|
|
|
2013-05-26 17:28:57 +02:00
|
|
|
/* Increment to the next row, wrapping back to first if necessary. */
|
2013-05-24 18:00:54 +02:00
|
|
|
|
2013-05-27 15:26:59 +02:00
|
|
|
if (++priv->currow >= priv->attr.nrows)
|
2013-05-26 17:28:57 +02:00
|
|
|
{
|
|
|
|
priv->currow = 0;
|
|
|
|
}
|
2013-05-24 18:00:54 +02:00
|
|
|
|
|
|
|
/* Say hello */
|
|
|
|
|
2013-05-24 20:00:15 +02:00
|
|
|
printf("Print [%s]\n", str);
|
|
|
|
slcd_puts(&priv->stream, str);
|
2013-05-24 19:11:48 +02:00
|
|
|
slcd_flush(&priv->stream);
|
2013-05-24 18:00:54 +02:00
|
|
|
|
|
|
|
/* Normal exit */
|
|
|
|
|
2013-05-24 19:11:48 +02:00
|
|
|
printf("Test complete\n");
|
2013-05-26 19:26:34 +02:00
|
|
|
close(fd);
|
2013-05-24 18:00:54 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
errout_with_fd:
|
2020-03-18 18:03:30 +01:00
|
|
|
close(priv->fd);
|
|
|
|
|
2013-05-24 18:00:54 +02:00
|
|
|
errout:
|
2020-03-18 18:03:30 +01:00
|
|
|
priv->initialized = false;
|
|
|
|
exit(EXIT_FAILURE);
|
2013-05-24 18:00:54 +02:00
|
|
|
}
|