⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctrl.c

📁 基于嵌入式linux的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * ctrl.c * * ============================================================================ * Copyright (c) Texas Instruments Inc 2005 * * Use of this software is controlled by the terms and conditions found in the * license agreement under which this software has been supplied or provided. * ============================================================================ *//* Standard Linux headers */#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <pthread.h>#include <linux/fb.h>#include <sys/mman.h>#include <sys/time.h>#include <sys/ioctl.h>/* Davinci specific kernel headers */#include <video/davincifb.h>/* Codec Engine headers */#include <xdc/std.h>#include <ti/sdo/ce/Engine.h>/* Demo headers */#include <rendezvous.h>#include <msp430lib.h>#include "encode.h"#include "ctrl.h"#include "ui.h"/* Keyboard command prompt */#define COMMAND_PROMPT       "Command [ 'help' for usage ] > "/* The 0-7 transparency value to use for the OSD */#define OSD_TRANSPARENCY     0x55#define MAX_TRANSPARENCY     0x77#define MIN_TRANSPARENCY     0x11#define INC_TRANSPARENCY     0x11#define NO_TRANSPARENCY      0x0/* The levels of initialization */#define OSDINITIALIZED       0x1#define ENGINEOPENED         0x2#define MSP430LIBINITIALIZED 0x4#define UICREATED            0x8/* Local function prototypes */static int  setOsdBuffer(int osdFd, int idx);static int  osdInit(char *displays[]);static int  osdExit(int osdFd);static int  setOsdTransparency(unsigned char trans);static int  waitForVsync(int fd);static int  getArmCpuLoad(int *procLoad, int *cpuLoad);static int  keyAction(enum msp430lib_keycode key, int displayIdx,                      int *osdTransPtr);static void drawDynamicData(Engine_Handle hEngine, int osdFd, int *time,                            int *displayIdx, int *workingIdx);/****************************************************************************** * setOsdBuffer ******************************************************************************/static int setOsdBuffer(int osdFd, int idx){    struct fb_var_screeninfo vInfo;    if (ioctl(osdFd, FBIOGET_VSCREENINFO, &vInfo) == -1) {        ERR("Failed FBIOGET_VSCREENINFO (%s)\n", strerror(errno));        return FAILURE;    }    vInfo.yoffset = vInfo.yres * idx;    if (ioctl(osdFd, FBIOPAN_DISPLAY, &vInfo) == -1) {        ERR("Failed FBIOPAN_DISPLAY (%s)\n", strerror(errno));        return FAILURE;    }    return SUCCESS;}/****************************************************************************** * osdInit ******************************************************************************/static int osdInit(char *displays[]){    struct fb_var_screeninfo varInfo;    int                      fd;    int                      size;    /* Open the OSD device */    fd = open(OSD_DEVICE, O_RDWR);    if (fd == -1) {        ERR("Failed to open fb device %s\n", OSD_DEVICE);        return FAILURE;    }    if (ioctl(fd, FBIOGET_VSCREENINFO, &varInfo) == -1) {        ERR("Failed ioctl FBIOGET_VSCREENINFO on %s\n", OSD_DEVICE);        return FAILURE;    }    /* Try the requested size */    varInfo.xres = D1_WIDTH;    varInfo.yres = D1_HEIGHT;    varInfo.bits_per_pixel = SCREEN_BPP;    if (ioctl(fd, FBIOPUT_VSCREENINFO, &varInfo) == -1) {        ERR("Failed ioctl FBIOPUT_VSCREENINFO on %s\n", OSD_DEVICE);        return FAILURE;    }    if (varInfo.xres != D1_WIDTH ||        varInfo.yres != D1_HEIGHT ||        varInfo.bits_per_pixel != SCREEN_BPP) {        ERR("Failed to get the requested screen size: %dx%d at %d bpp\n",            D1_WIDTH, D1_HEIGHT, SCREEN_BPP);        return FAILURE;    }    size = varInfo.xres * varInfo.yres * varInfo.bits_per_pixel / 8;    /* Map the frame buffers to user space */    displays[0] = (char *) mmap(NULL, size * NUM_BUFS,                                   PROT_READ | PROT_WRITE,                                   MAP_SHARED, fd, 0);    if (displays[0] == MAP_FAILED) {        ERR("Failed mmap on %s\n", OSD_DEVICE);        return FAILURE;    }    displays[1] = displays[0] + size;    setOsdBuffer(fd, 0);    return fd;}/****************************************************************************** * osdExit ******************************************************************************/static int osdExit(int osdFd){    setOsdBuffer(osdFd, 0);    close(osdFd);    return SUCCESS;}/****************************************************************************** * setOsdTransparency ******************************************************************************/static int setOsdTransparency(unsigned char trans){    struct fb_var_screeninfo vInfo;    unsigned short          *attrDisplay;    int                      attrSize;    int                      fd;    /* Open the attribute device */    fd = open(ATTR_DEVICE, O_RDWR);    if (fd == -1) {        ERR("Failed to open attribute window %s\n", ATTR_DEVICE);        return FAILURE;    }    if (ioctl(fd, FBIOGET_VSCREENINFO, &vInfo) == -1) {        ERR("Error reading variable information.\n");        return FAILURE;    }    /* One nibble per pixel */    attrSize = vInfo.xres_virtual * vInfo.yres / 2;     attrDisplay = (unsigned short *) mmap(NULL, attrSize,                                          PROT_READ | PROT_WRITE,                                          MAP_SHARED, fd, 0);    if (attrDisplay == MAP_FAILED) {        ERR("Failed mmap on %s\n", ATTR_DEVICE);        return FAILURE;    }    /* Fill the window with the new attribute value */    memset(attrDisplay, trans, attrSize);    munmap(attrDisplay, attrSize);    close(fd);    return SUCCESS;}/****************************************************************************** * waitForVsync ******************************************************************************/static int waitForVsync(int fd){    int dummy;    /* Wait for vertical sync */    if (ioctl(fd, FBIO_WAITFORVSYNC, &dummy) == -1) {        ERR("Failed FBIO_WAITFORVSYNC (%s)\n", strerror(errno));        return FAILURE;    }    return SUCCESS;}/****************************************************************************** * getArmCpuLoad ******************************************************************************/static int getArmCpuLoad(int *procLoad, int *cpuLoad){    static unsigned long prevIdle     = 0;    static unsigned long prevTotal    = 0;    static unsigned long prevProc     = 0;    int                  cpuLoadFound = FALSE;    unsigned long        user, nice, sys, idle, total, proc;    unsigned long        uTime, sTime, cuTime, csTime;    unsigned long        deltaTotal, deltaIdle, deltaProc;    char                 textBuf[4];    FILE                *fptr;    /* Read the overall system information */    fptr = fopen("/proc/stat", "r");    if (fptr == NULL) {        ERR("/proc/stat not found. Is the /proc filesystem mounted?\n");        return FAILURE;    }    /* Scan the file line by line */    while (fscanf(fptr, "%4s %lu %lu %lu %lu %*[^\n]", textBuf,                  &user, &nice, &sys, &idle) != EOF) {        if (strcmp(textBuf, "cpu") == 0) {            cpuLoadFound = TRUE;            break;        }    }    if (fclose(fptr) != 0) {        return FAILURE;    }    if (!cpuLoadFound) {        return FAILURE;    }    /* Read the current process information */    fptr = fopen("/proc/self/stat", "r");    if (fptr == NULL) {        ERR("/proc/self/stat not found. Is the /proc filesystem mounted?\n");        return FAILURE;    }    if (fscanf(fptr, "%*d %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %lu "                     "%lu %lu %lu", &uTime, &sTime, &cuTime, &csTime) != 4) {        ERR("Failed to get process load information.\n");        fclose(fptr);        return FAILURE;    }    if (fclose(fptr) != 0) {        return FAILURE;    }    total = user + nice + sys + idle;    proc = uTime + sTime + cuTime + csTime;     /* Check if this is the first time, if so init the prev values */    if (prevIdle == 0 && prevTotal == 0 && prevProc == 0) {        prevIdle = idle;        prevTotal = total;        prevProc = proc;         return SUCCESS;    }    deltaIdle = idle - prevIdle;    deltaTotal = total - prevTotal;    deltaProc = proc - prevProc;     prevIdle = idle;    prevTotal = total;    prevProc = proc;    *cpuLoad = 100 - deltaIdle * 100 / deltaTotal;    *procLoad = deltaProc * 100 / deltaTotal;    return SUCCESS;}/****************************************************************************** * drawDynamicData ******************************************************************************/static void drawDynamicData(Engine_Handle hEngine, int osdFd, int *time,                            int *displayIdx, int *workingIdx){    static unsigned long  firstTime = 0;    static unsigned long  prevTime;    unsigned long         newTime;    unsigned long         deltaTime;    struct timeval        tv;    struct tm            *timePassed;    time_t                spentTime;    char                  tempString[9];    int                   procLoad;    int                   armLoad;    int                   dspLoad;    int                   fps;    int                   videoKbps;    int                   soundKbps;    float                 fpsf;    float                 videoKbpsf;    float                 soundKbpsf;    *time = -1;    if (gettimeofday(&tv, NULL) == -1) {        ERR("Failed to get os time\n");        return;    }    newTime = tv.tv_sec * 1000 + tv.tv_usec / 1000;    if (!firstTime) {        firstTime = newTime;        prevTime = newTime;        return;    }    /* Only update OSD every second */    deltaTime = newTime - prevTime;    if (deltaTime <= 1000) {        return;    }    prevTime = newTime;    spentTime = (newTime - firstTime) / 1000;    if (spentTime <= 0) {        return;    }    *time = spentTime;    fpsf         = gblGetAndResetFrames() * 1000.0 / deltaTime;    fps          = fpsf + 0.5;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -