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

📄 guidemo_adc.c

📁 ARM_CORTEX-M3应用实例开发详解光盘
💻 C
字号:
                                    /*
*********************************************************************************************************
*                                                uC/GUI
*                        Universal graphic software for embedded applications
*
*                       (c) Copyright 2002, Micrium Inc., Weston, FL
*                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
*              礐/GUI is protected by international copyright laws. Knowledge of the
*              source code may not be used to write a similar product. This file may
*              only be used in accordance with a license and should not be redistributed
*              in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File        : GUIDEMO_Graph.c
Purpose     : Several GUIDEMO routines
----------------------------------------------------------------------
*/

#include "GUI.h"
#include "LCD_ConfDefaults.h"      /* valid LCD configuration */
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "adc.h"
#include "keys.h"
#include "aux_lib.h"

#define     GUIDEMO_LARGE   1

#if GUI_SUPPORT_MEMDEV

/*********************************************************************
*
*       Structure containing information for drawing routine
*
**********************************************************************
*/

typedef struct {
  I16 disoff;
  I16 * aY;
} PARAM;

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

#if GUIDEMO_LARGE
  #define YSIZE   (LCD_YSIZE - 100)
#else
  #define YSIZE   (LCD_YSIZE - 30)
#endif
/*
#if LCD_YSIZE > 120
  #define YSIZE   (LCD_YSIZE - 100)
#else
  #define YSIZE   20
#endif
*/

#define DEG2RAD (3.1415926f / 180)

#if LCD_BITSPERPIXEL == 1
  #define COLOR_GRAPH0 GUI_WHITE
  #define COLOR_GRAPH1 GUI_WHITE
#else
  #define COLOR_GRAPH0 GUI_GREEN
  #define COLOR_GRAPH1 GUI_YELLOW
#endif


#define uDelay(x) sleep_us(x)
#define mDelay(x) sleep_us((x)*900)

/*********************************************************************
*
*       Draws the graph
*
**********************************************************************
*/

/* 需要显示数据窗口大小 */
#define DATWIN  (LCD_XSIZE - 20)
/* 用于保存ADC采集数据的缓冲,此处定义为两倍窗口大小 */
#define MEMWIN  (DATWIN * 2)

/* 采样触发阀值 */
#define TRIGGERNUM  10
/* 采样间隔 */
static unsigned int _Mul_T = 4;
/* adc 采样后缩小倍数 */
static unsigned int _Mul_V = 40;

/* 停止模式时 时基调整用*/
static unsigned int _SMul_T = 0;

/*  示波器运行标志 */
static char Running = 1;

/* 当前选择菜单索引 */
static char Select = 0;

/* 显示坐标 x ,y 偏移 */
static unsigned int movx = 0;
static unsigned int movy = YSIZE / 2;


/* x 轴每一格表示的时间 (us) */
static unsigned int _GetX(void)
{
    return 40 * _Mul_T;
}

/* y 轴每一格表示的电压,单位(mv)*/
static unsigned int _GetY(void)
{
    return 16 * _Mul_V;
}

static void _RunStp(int key)
{
    if (key != KEY_SEL)
        return;
    Running = !Running;
    if (Running == 0)
        _SMul_T = _Mul_T;
    else
        _SMul_T = 0;
}
static void _Tim(int key)
{
    switch (key) {
    case KEY_UP:
        if (_Mul_T < 128)
            _Mul_T *= 2;
        break;
    case KEY_DOWN:
         
        _Mul_T /= 2;
        if (_Mul_T == 0)
            _Mul_T = 1;
        break;
    case KEY_SEL:
        _Mul_T = 4;

    }
    
}
static void _Vol(int key)
{
    switch (key) {
    case KEY_UP:
        if (_Mul_V != 10)
            _Mul_V /= 2; 
        break;
    case KEY_DOWN:
        if (_Mul_V != 80)
            _Mul_V *= 2;  
        break;
    case KEY_SEL:       
        _Mul_V = 40;
    }
}
static void _Mvx(int key)
{
    switch (key) {
    case KEY_UP:
        movx += 2;
        break;
    case KEY_DOWN:
        movx -= 2;  
        break;
    case KEY_SEL:
        movx  = 0;
    }
}
static void _Mvy(int key)
{
    switch (key) {
    case KEY_UP:
        movy -= 1;
        break;
    case KEY_DOWN:
        movy += 1;  
        break;
    case KEY_SEL:
        movy  = YSIZE / 2;

    }
}


static void _Draw(void * p) {
  int i;
  PARAM * pParam = (PARAM *)p;
  int off = pParam->disoff;

  GUI_SetBkColor(GUI_BLACK);
  GUI_SetColor(GUI_DARKGRAY);
  GUI_ClearRect(19, (LCD_YSIZE - 20) - YSIZE, (LCD_XSIZE - 2), (LCD_YSIZE - 21));
  for (i = 0; i < (YSIZE / 2); i += 20) {
    GUI_DrawHLine((LCD_YSIZE - 20) - (YSIZE / 2) + i, 19, (LCD_XSIZE - 2));
    if (i) {
      GUI_DrawHLine((LCD_YSIZE - 20) - (YSIZE / 2) - i, 19, (LCD_XSIZE - 2));
    }
  }
  for (i = 40; i < (LCD_XSIZE - 20); i += 40) {
    GUI_DrawVLine(18 + i, (LCD_YSIZE - 20) - YSIZE, (LCD_YSIZE - 21));
  }
 
  off += movx;

  if (off < 0)
    off = 0;
  if (off > (MEMWIN-DATWIN)-1)
    off = (MEMWIN-DATWIN)-1;

  GUI_SetColor(COLOR_GRAPH0);
  GUI_DrawGraph(pParam->aY + off, (LCD_XSIZE - 20), 19, (LCD_YSIZE - 20) - YSIZE);
}
/*
static void _Draw2(void * p) {
  PARAM * pParam = (PARAM *)p;
  _Draw(p);
  GUI_SetColor(COLOR_GRAPH1);
  GUI_DrawGraph(pParam->aY+15, (LCD_XSIZE - 20), 19, (LCD_YSIZE - 20) - YSIZE);
}
*/
/*********************************************************************
*
*       Labels the x & y-axis
*
**********************************************************************
*/

static void _Label(void) {
  int x, y;
  GUI_SetPenSize(1);
  GUI_ClearRect(0, (LCD_YSIZE - 21) - YSIZE, (LCD_XSIZE - 1), (LCD_YSIZE - 1));
  GUI_DrawRect(18, (LCD_YSIZE - 21) - YSIZE, (LCD_XSIZE - 1), (LCD_YSIZE - 20));
  GUI_SetFont(&GUI_Font6x8);
  for (x = 0; x < (LCD_XSIZE - 20); x += 40) {
    int xPos = x + 18;
    GUI_DrawVLine(xPos, (LCD_YSIZE - 20), (LCD_YSIZE - 14));
    GUI_DispDecAt(x / 40, xPos - 2, (LCD_YSIZE - 9), 1);
  }
  for (y = 0; y < YSIZE / 2; y += 20) {
    int yPos = (LCD_YSIZE - 20) - YSIZE / 2 + y;
    GUI_DrawHLine(yPos, 13, 18);
    if (y) {
      GUI_GotoXY(1, yPos - 4);
      GUI_DispSDec(-y / 20, 2);
      yPos = (LCD_YSIZE - 20) - YSIZE / 2 - y;
      GUI_DrawHLine(yPos, 13, 18);
      GUI_GotoXY(1, yPos - 4);
      GUI_DispSDec(y / 20, 2);
    } else {
      GUI_DispCharAt('0', 7, yPos - 4);
    }
  }
}


static void _ShowText(const char * sText) {
  GUI_SetColor(GUI_WHITE);
  GUI_SetBkColor(GUI_RED);
  #if GUIDEMO_LARGE
    GUI_ClearRect(0, 0, LCD_XSIZE, 60);
    GUI_SetFont(&GUI_FontComic18B_ASCII);
    GUI_DispStringAt(sText, 18, 5);
  #else
    sText = sText;  /* Avoid warning */
  #endif
}

static void _LabelMS(void) {
  GUI_SetFont(&GUI_Font6x8);
  #if GUIDEMO_LARGE
    GUI_DispStringAt("msec/graph:", LCD_XSIZE-((14+1)*6), 15);
  
  #endif
}

static void _DisplayTime(int tDiff) {
  #if GUIDEMO_LARGE
    GUI_GotoXY(212, 15);
    GUI_SetFont(&GUI_Font6x8);
    GUI_SetColor(GUI_WHITE);
    GUI_SetBkColor(GUI_RED);
    GUI_DispDecSpace(tDiff, 3);
  #else
    tDiff = tDiff; /* Avoid warning */
  #endif
}

#define _FUNX   18
#define _FUNY   42
#define _FUNW   (8*3+2)
#define _FUNN   5
static void _DispFunText(void)
{
    static char *funs[] = {"", "Tim", "Vol", "Mvx", "Mvy"};
    static char *ost[] = {"Stp", "Run"};
    char *pSlt;

    int i;

    GUI_SetColor(GUI_WHITE);
    GUI_SetFont(&GUI_Font8x16);
    /* 高亮菜单 */
    GUI_SetBkColor(GUI_BLUE);          
    if (Select == 0) 
        pSlt = ost[Running];
    else {
        pSlt = funs[Select];
    }
    GUI_DispStringAt(pSlt, _FUNX + Select * _FUNW, _FUNY);
    
    GUI_SetBkColor(GUI_BROWN);
    if (Select != 0)
        GUI_DispStringAt(ost[Running], _FUNX, _FUNY);

    for (i=1; i<_FUNN; i++) {
        if (i != Select)        
            GUI_DispStringAt(funs[i], _FUNX + i * _FUNW, _FUNY);
    }
}
/* 显示单位信息 */
static void _DispStat(void)
{
    char buf[15];
    unsigned int t;

    GUI_SetColor(GUI_WHITE);
    GUI_SetBkColor(GUI_RED);
    GUI_SetFont(&GUI_Font6x8);
    t = _GetX();
    sprintf(buf, "x:%4d(us)", t);
    GUI_DispStringAt(buf, LCD_XSIZE-(10+1)*6, 42);
    t = _GetY();
    sprintf(buf, "y:%4d(mv)", t);
    GUI_DispStringAt(buf, LCD_XSIZE-(10+1)*6, 50);
}
/*********************************************************************
*
*       Draws random data
*
**********************************************************************
*/

static void _GetADCData(I16 * paY) 
{
    int i;
    int us, ms;
    us = (_Mul_T-1) % 1000;
    ms = (_Mul_T-1) / 1000;
    if (Running != 0) { 
        for (i = 0; i < MEMWIN; i++) {
            paY[i] =  adc_get_value();
            uDelay(us);
            if (ms>1)
                OSTimeDlyHMSM(0, 0, 0, ms-1);
            else
                mDelay(ms);
        }
    }
}
static void _DataTrans(PARAM * param, I16 *pDat)
{
    I16 * paY = param->aY;
    int i;
    int off = 0;
    I16 min = 4095;
    for (i=0; i<MEMWIN; i++) {
        paY[i] = pDat[i];
        if (min > pDat[i])
            min = pDat[i];    
    }
    if (_SMul_T > 0 && _SMul_T != _Mul_T) {
        int prop;
        /* 停止模式,且时基经过调整 */
        off = param->disoff;
        if (_SMul_T < _Mul_T) {
            /* 压缩 */
            int k;
            prop = (_Mul_T / _SMul_T);
            
            k = off;
            for (i=off; i<MEMWIN; i+=prop) 
                paY[k++] = pDat[i];
            for (; k<MEMWIN; k++)
                paY[k] = min;
            k = off;
            for (i=off; i>=0; i-=prop)
                paY[k--] = pDat[i];
            for (; k>=0; k--)    
                    paY[k] = min;
                    
        } else {
           
            int k, m = 0;
            I16 tmp;
            prop = _SMul_T / _Mul_T;
            
            
            for (; (prop & 1) == 0; prop >>= 1) 
                m++;
            
            i = off + 1;
            paY[off] = pDat[off];
            for (k=off+1; k<MEMWIN; k+=(m+1), i++) {
                int j;
                tmp = (pDat[i] - pDat[i-1]) / (m+1);
                for (j=0; j<m && k+j<MEMWIN; j++)
                    paY[k+j] = paY[k+j-1] + tmp;
                if (j==m)
                    paY[k+j] = pDat[i];
            }
            i = off - 1;
            for (k=off-1; k>=0; k-=(m+1), i--) {
                int j;
                tmp = (pDat[i+1] - pDat[i]) / (m+1);
                for (j=0; j<m && k-j>=0; j++)
                    paY[k-j] = paY[k-j+1] - tmp;
                if (j==m)
                    paY[k-j] = pDat[i];
            }
        }  
    }
 //   if ((_SMul_T > 0 && _SMul_T != _Mul_T) {
 //       off = 0; 
    for (i=MEMWIN-1; i>=0; i--) { 
        paY[i] =  movy - (paY[i] - min) / _Mul_V;
                      
        if (!off && i<(MEMWIN-DATWIN)/2) {
                if ((paY[i] - paY[i+1]) > TRIGGERNUM && paY[i] < movy)
                    off = i;
        }
        
    }  
    
    param->disoff = off;
}

static int proc_key(void)
{
    static void (*fun[])(int) = 
    {_RunStp, _Tim, _Vol, _Mvx, _Mvy};
    int key;
    key = key_accept();
    switch (key) {
    case KEY_USER:
        return 0;
       
    case KEY_RIGHT:
        Select = ++Select % _FUNN;
        _DispFunText();
        break;
    case KEY_LEFT:
        Select = --Select % _FUNN;
        _DispFunText();
        break;
    case KEY_DOWN:
    case KEY_UP:
    case KEY_SEL:
        fun[Select](key);
        _DispStat();
        _DispFunText();
        break;
    }
    
    return 1;
}

static void _DemoRandomGraph(void) {
  PARAM Param;
  int Cnt = 0;
  GUI_RECT Rect = {19, (LCD_YSIZE - 20) - YSIZE, (LCD_XSIZE - 2), (LCD_YSIZE - 21)};

  GUI_HMEM hMem = GUI_ALLOC_Alloc /*Zero*/(MEMWIN * sizeof(I16));
  GUI_HMEM hDat = GUI_ALLOC_Alloc /*Zero*/(MEMWIN * sizeof(I16));
  I16 *pDat;

  if (hMem == 0 || hDat == 0) {
    GUI_ALLOC_Free(hMem);
    GUI_ALLOC_Free(hDat);
    _ShowText("Err: Not enough memory!");
    return;
  }
  Param.aY = (I16*)GUI_ALLOC_h2p(hMem);
  pDat = (I16*)GUI_ALLOC_h2p(hDat);

  _ShowText("Oscillograph");
  _LabelMS();
  _DispFunText();
  _DispStat();

  while(proc_key()) {
    int t1, tDiff2;
    t1 = GUI_GetTime();
    _GetADCData(pDat);
    _DataTrans(&Param, pDat);
    GUI_MEMDEV_Draw(&Rect, _Draw, &Param, 0, GUI_MEMDEV_NOTRANS);
    tDiff2 = GUI_GetTime() - t1;
    if (tDiff2 < 100) {
      GUI_Delay(100 - tDiff2);
    }
    if(!((++Cnt)%10)) {
      _DisplayTime(tDiff2);
    }
    
  }
  GUI_ALLOC_Free(hMem);
  GUI_ALLOC_Free(hDat);
}



/*********************************************************************
*
*       GUIDEMO_Graph
*
**********************************************************************
*/

void GUIDEMO_Adc(void) {
//  GUIDEMO_ShowIntro("Drawing a graph",
//                    "\nOptimized drawing routine for"
//                    "\ndrawing graph data");
  GUI_SetBkColor(GUI_BLACK);
  GUI_SetColor(GUI_WHITE);
  GUI_Clear();
  _Label();
  _DemoRandomGraph();
//  GUIDEMO_NotifyStartNext();
//  _DemoSineWave();
//  GUIDEMO_NotifyStartNext();
//  _DemoOrData();
}

#elif defined(NC30) || defined(NC308)

void GUIDEMO_Adc(void) {}

#endif

⌨️ 快捷键说明

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