|
| 1 | +/**************************************************************************** |
| 2 | + * apps/system/vncviewer/lcd_render.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | + |
| 29 | +#include <stdio.h> |
| 30 | +#include <string.h> |
| 31 | +#include <errno.h> |
| 32 | +#include <fcntl.h> |
| 33 | +#include <stdlib.h> |
| 34 | +#include <unistd.h> |
| 35 | +#include <sys/ioctl.h> |
| 36 | + |
| 37 | +#include <nuttx/video/fb.h> |
| 38 | +#include <nuttx/lcd/lcd_dev.h> |
| 39 | + |
| 40 | +#include "lcd_render.h" |
| 41 | + |
| 42 | +/**************************************************************************** |
| 43 | + * Public Functions |
| 44 | + ****************************************************************************/ |
| 45 | + |
| 46 | +/**************************************************************************** |
| 47 | + * Name: lcd_init |
| 48 | + ****************************************************************************/ |
| 49 | + |
| 50 | +int lcd_init(struct lcd_ctx_s *ctx, int devno) |
| 51 | +{ |
| 52 | + char devpath[16]; |
| 53 | + int ret; |
| 54 | + |
| 55 | + snprintf(devpath, sizeof(devpath), "/dev/lcd%d", devno); |
| 56 | + |
| 57 | + ctx->fd = open(devpath, O_RDWR); |
| 58 | + if (ctx->fd < 0) |
| 59 | + { |
| 60 | + printf("vncviewer: failed to open %s: %d\n", devpath, errno); |
| 61 | + return -errno; |
| 62 | + } |
| 63 | + |
| 64 | + /* Query LCD resolution */ |
| 65 | + |
| 66 | + struct fb_videoinfo_s vinfo; |
| 67 | + ret = ioctl(ctx->fd, LCDDEVIO_GETVIDEOINFO, |
| 68 | + (unsigned long)(uintptr_t)&vinfo); |
| 69 | + if (ret < 0) |
| 70 | + { |
| 71 | + printf("vncviewer: GETVIDEOINFO failed: %d\n", errno); |
| 72 | + close(ctx->fd); |
| 73 | + ctx->fd = -1; |
| 74 | + return -errno; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + ctx->xres = vinfo.xres; |
| 79 | + ctx->yres = vinfo.yres; |
| 80 | + ctx->fmt = vinfo.fmt; |
| 81 | + } |
| 82 | + |
| 83 | + printf("vncviewer: LCD %s opened (%ux%u)\n", |
| 84 | + devpath, ctx->xres, ctx->yres); |
| 85 | + |
| 86 | + /* Set power on */ |
| 87 | + |
| 88 | + ret = ioctl(ctx->fd, LCDDEVIO_SETPOWER, 1); |
| 89 | + if (ret < 0) |
| 90 | + { |
| 91 | + printf("vncviewer: WARNING: SETPOWER failed: %d\n", errno); |
| 92 | + } |
| 93 | + |
| 94 | + return OK; |
| 95 | +} |
| 96 | + |
| 97 | +/**************************************************************************** |
| 98 | + * Name: lcd_put_row |
| 99 | + ****************************************************************************/ |
| 100 | + |
| 101 | +int lcd_put_row(struct lcd_ctx_s *ctx, uint16_t x, uint16_t y, |
| 102 | + uint16_t w, const uint16_t *pixels) |
| 103 | +{ |
| 104 | + struct lcddev_area_s area; |
| 105 | + int ret; |
| 106 | + |
| 107 | + /* Clip to LCD bounds */ |
| 108 | + |
| 109 | + if (x >= ctx->xres || y >= ctx->yres) |
| 110 | + { |
| 111 | + return OK; |
| 112 | + } |
| 113 | + |
| 114 | + if (x + w > ctx->xres) |
| 115 | + { |
| 116 | + w = ctx->xres - x; |
| 117 | + } |
| 118 | + |
| 119 | + area.row_start = y; |
| 120 | + area.row_end = y; |
| 121 | + area.col_start = x; |
| 122 | + area.col_end = x + w - 1; |
| 123 | + area.data = (uint8_t *)pixels; |
| 124 | + |
| 125 | + ret = ioctl(ctx->fd, LCDDEVIO_PUTAREA, (unsigned long)(uintptr_t)&area); |
| 126 | + if (ret < 0) |
| 127 | + { |
| 128 | + return -errno; |
| 129 | + } |
| 130 | + |
| 131 | + return OK; |
| 132 | +} |
| 133 | + |
| 134 | +/**************************************************************************** |
| 135 | + * Name: lcd_fill |
| 136 | + ****************************************************************************/ |
| 137 | + |
| 138 | +int lcd_fill(struct lcd_ctx_s *ctx, uint16_t color) |
| 139 | +{ |
| 140 | + uint16_t y; |
| 141 | + uint16_t i; |
| 142 | + uint16_t w = ctx->xres; |
| 143 | + FAR uint16_t *rowbuf; |
| 144 | + |
| 145 | + rowbuf = (FAR uint16_t *)malloc(w * sizeof(uint16_t)); |
| 146 | + if (rowbuf == NULL) |
| 147 | + { |
| 148 | + return -ENOMEM; |
| 149 | + } |
| 150 | + |
| 151 | + for (i = 0; i < w; i++) |
| 152 | + { |
| 153 | + rowbuf[i] = color; |
| 154 | + } |
| 155 | + |
| 156 | + for (y = 0; y < ctx->yres; y++) |
| 157 | + { |
| 158 | + lcd_put_row(ctx, 0, y, w, rowbuf); |
| 159 | + } |
| 160 | + |
| 161 | + free(rowbuf); |
| 162 | + |
| 163 | + return OK; |
| 164 | +} |
| 165 | + |
| 166 | +/**************************************************************************** |
| 167 | + * Name: lcd_uninit |
| 168 | + ****************************************************************************/ |
| 169 | + |
| 170 | +void lcd_uninit(struct lcd_ctx_s *ctx) |
| 171 | +{ |
| 172 | + if (ctx->fd >= 0) |
| 173 | + { |
| 174 | + close(ctx->fd); |
| 175 | + ctx->fd = -1; |
| 176 | + } |
| 177 | +} |
0 commit comments