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

📄 dispdrv.c

📁 《OSEK/VDX汽车电子嵌入式软件编程技术》中光盘的资料
💻 C
字号:
/************************************************
*
*	$Copyright    2001 Joseph J. Lemieux  ALL RIGHTS RESERVED. $
*
*	$Filename: C:\OSEKBook\src\CH09\src\dispdrv.c $
*
*	Description: Routines to drive the LCD display	
*
************************************************/
#ifndef DISPDRVC
#define DISPDRVC

/************************************************
*
*	Include files
*
************************************************/

#include "typedefs.h"
#include "os.h"
#include "init.h"
#include "dispdrv.h"

/************************************************
*
*	Local macros
*
************************************************/

/************************************************
*
*	Local type definitions
*
************************************************/
/***
*
* Type definition for cursor position
*
***/
typedef struct CURSOR_POSITION_TYPEtag {
   UINT8 row;
   UINT8 column;
   }CURSOR_POSITION_TYPE;

/************************************************
*
*	Local Function Prototypes
*
************************************************/
void wait(UINT32 a);
void PackDisplay(char *string);
void OutputNewDisplay(void);
void OutputNewLine(void);
void SetDisplayPosition(UINT8 row, UINT8 column);

/************************************************
*
*	Local Variables
*
************************************************/
/* Actual string to be sent to display */

/* Current Cursor Position */
static CURSOR_POSITION_TYPE cursorPosition = {0,0};

/************************************************
*
*	Global Variables - only used until messaging
*    is enabled.
*
************************************************/
/***
* Global variable into which the application places
* the requested display, and the pointer to the buffer
* location which is updated by the display. The buffer
* accepts \n for newline and \f to clear display before
* entering the info. Ends with a NULL character which
* must also be put in by the application.
***/
typedef struct DISPLAY_BUFFER_TYPEtag {
   UINT8 row;
   UINT8 column;
   char buffer[100];
   }DISPLAY_BUFFER_TYPE;
static DISPLAY_BUFFER_TYPE displayBuffer[DISPLAY_BUFFER_SIZE];
static UINT8 displayBufferStart,displayBufferEnd;
static BOOLEAN displayBufferError;

/************************************************
*
*	Local Constants
*
************************************************/
/* Initialization Message */
char const InitMessage[] = "\f  OSEK/VDX Example\n  Cardgame \360 \361 \362 \363   Press D to Shuffle ";

/* Setup for card suit special characters */
char const special[] = {	0x0a,0x1b,0x1f,0x0e,0x0e,0x04,0x04,0x00,
					0x04,0x0e,0x0e,0x1f,0x0e,0x0e,0x04,0x00,
					0x04,0x0e,0x0e,0x1f,0x1f,0x15,0x04,0x00,
					0x04,0x0e,0x04,0x0a,0x1f,0x0a,0x00,0x00,
					0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,
               0x1b,0x11,0x11,0x15,0x11,0x11,0x1b,0x00
};

/* Row Translation Matrix */
UINT8 const RowTranslation[MAX_DISPLAY_ROWS] = {ROW_TRANSLATION};
/************************************************
*
*	Functions
*
************************************************/
/************************************************
*
*   Function:     InitDisplay
*
*   Inputs:       type - type of initialization occurring.
*
*   Outputs:      none
*
*   Returns:      none
*
*   Description:  Initializes the display and prepares for 
*                 output. Sends startup message InitMessage.
*
************************************************/
void InitDisplay(InitType type)
{

	UINT8 *displayControl = DISPLAY_CONTROL_LOCATION;
	UINT8 *display = DISPLAY_BUFFER_LOCATION;

	UINT8 i;
   displayBufferStart = displayBufferEnd = 0;
   displayBufferError = FALSE;

/***
*
* Perform display initialization sequence
*
***/
   if(type==INIT_RESET)
   {
      wait(100000);
   }
	*displayControl = 0x38;
	wait(30000);
	*displayControl = 0x38;
	wait(1000);
	*displayControl = 0x38;
	wait(1000);
	*displayControl = 0x38;
	wait(1000);
	*displayControl = 0x10;
	wait(1000);
	*displayControl = 0x01;
	wait(30000);
	*displayControl = 0x06;
	wait(1000);
	*displayControl = 0x0E;
	wait(1000);
	*displayControl = 0x40;
	wait(1000);
/***
*
* Program Card Suit special characters
*
***/
	for(i=0;i<sizeof(special);i++)
	{
		*display = special[i];
	wait(1000);
	}
	*displayControl = 0x80;
	wait(1000);
	*displayControl = 0x01;
	wait(30000);

/***
*
* Pack display initialization message and output
*
***/
	
   cursorPosition.row = cursorPosition.column = 0;
}

/************************************************
*
*   Function:     wait
*
*   Inputs:       time - number of loops to wait
*
*   Outputs:      none
*
*   Returns:      none
*
*   Description:  Small routine that inserts a small delay
*                 into the display writes.
*
************************************************/
void wait(UINT32 time)
{
	while(time-- != 0);
}

/************************************************
*
*   Function:     PackDisplay
*
*   Inputs:       string - Pointer to string that contains
*                 the data to be output - null terminated.
*
*   Outputs:      none
*
*   Returns:      none
*
*   Description:  Adds the data from the string to the display
*                 at the current cursor position. If the
*                 string has \f, the display is cleared. If \n,
*                 the cursor is moved to the next line. If 0xF0
*                 or greater, then a special character is output.
*
************************************************/
void PackDisplay(char *string)
{
UINT8 i,j;

   while(*string != 0)
   {
      switch(*string)
      {
/***
*
* If line feed, then output new line to screen.
*
***/
         case 0x0a:
            OutputNewLine();
            break;
         case 0x0c:
/***
*
* If form feed, clear screen
*
***/
            for(i=0;i<MAX_DISPLAY_ROWS;i++)
            {
               for(j=0;j<MAX_DISPLAY_LINE_LENGTH;j++)
               {
                  displayMirror->message[i*MAX_DISPLAY_LINE_LENGTH + j] = ' ';
               }
            }
            cursorPosition.row = cursorPosition.column = 0;
            break;
/***
*
* Place character, then increment cursor. If at end of line, go to new line
*
***/
         default:
            if(*string >= 240)
            {
               displayMirror->message[cursorPosition.row*MAX_DISPLAY_LINE_LENGTH + cursorPosition.column++] 
                  = *string - (char)240;
            }
            else
            {
               displayMirror->message[cursorPosition.row*MAX_DISPLAY_LINE_LENGTH + cursorPosition.column++] 
                  = *string;
            }
            if(cursorPosition.column == MAX_DISPLAY_LINE_LENGTH)
            {
               OutputNewLine();
            }
      }
      ++string;
   }
}

/************************************************
*
*   Function:     OutputNewDisplay
*
*   Inputs:       none
*
*   Outputs:      none
*
*   Returns:      none
*
*   Description:  Outputs the value in displayMessage
*                 buffer to the display, switching
*                 the lines as necessary.
*
************************************************/
TASK(OutputDisplayBuffer)
{
   UINT8 *displayControl = DISPLAY_CONTROL_LOCATION;
   UINT8 *display = DISPLAY_BUFFER_LOCATION;
   UINT8 i,outputRow;
   UINT8 translateRow;

/***
*
* First clear the display, then output message.
*
***/
   WriteDisplay(InitMessage);
   while(1){
      translateRow = 0;
      WaitEvent(BUFFER_CHANGED);
      ClearEvent(BUFFER_CHANGED);
      ReceiveMessage(DisplayMessage,displayMirrorTemp);
      *displayControl = 0x01;
      SetRelAlarm(DisplayWaitAlarm,10,0);
      WaitEvent(DISPLAY_READY);
      ClearEvent(DISPLAY_READY);
      DisableAllInterrupts();
      while(translateRow < MAX_DISPLAY_ROWS){
      outputRow = RowTranslation[translateRow++];
         for(i=0;i<MAX_DISPLAY_LINE_LENGTH;i++){
            *display = displayMirrorTemp->message[outputRow*MAX_DISPLAY_LINE_LENGTH + i];
         wait(1000);
         }
      }
      i=((cursorPosition.row&0x01)*0x40) + 
          ((cursorPosition.row&0x02)/2*MAX_DISPLAY_LINE_LENGTH) +
             cursorPosition.column;
      *displayControl = 0x80+i;
      wait(1000);
      EnableAllInterrupts();
   }
}

/************************************************
*
*   Function:     OutputNewLine
*
*   Inputs:       none
*
*   Outputs:      none
*
*   Returns:      none
*
*   Description:  Moves the cursor to the new line.
*                 If at the last line, scroll up.
*
************************************************/
void OutputNewLine(void)
{

UINT8 i,j;

   if(++cursorPosition.row == MAX_DISPLAY_ROWS)
   {
       --cursorPosition.row;
       for(i=0;i<MAX_DISPLAY_ROWS-1;i++)
       {
          for(j=0;j<MAX_DISPLAY_LINE_LENGTH;j++)
          {
             displayMirror->message[i*MAX_DISPLAY_LINE_LENGTH + j] 
                = displayMirror->message[((i+1)*MAX_DISPLAY_LINE_LENGTH) + j];
          }
       }
       for(j=0;j<MAX_DISPLAY_LINE_LENGTH;j++)
       {
          displayMirror->message[i*MAX_DISPLAY_LINE_LENGTH + j] = ' ';
       }
   }
   cursorPosition.column = 0;
}

/************************************************
*
*   Function:     OutputDisplay
*
*   Inputs:       none
*
*   Outputs:      none
*
*   Returns:      none
*
*   Description:  Task which performs the display
*                 output.
*
************************************************/
TASK(OutputDisplay)
{
   while((displayBufferError == TRUE) || 
      (displayBufferStart != displayBufferEnd)){
      if(displayBuffer[displayBufferStart].row != 0xff){
         SetDisplayPosition(displayBuffer[displayBufferStart].row,
            displayBuffer[displayBufferStart].column);
      }
      PackDisplay(displayBuffer[displayBufferStart].buffer);
      if((++displayBufferStart) == DISPLAY_BUFFER_SIZE){
         displayBufferStart = 0;
      }
      Schedule();
   }
   SendMessage(DisplayMessage,displayMirror);
   TerminateTask();
}

/************************************************
*
*   Function:     SetDisplayPosition
*
*   Inputs:       row,column - zero based position
*                 of the row and column.
*
*   Outputs:      Sets the cursorPosition variable
*                 and moves the cursor.
*
*   Returns:      none
*
*   Description:  Moves the cursor to the 
*                 position described by row:column
*                 and sets the proper variables.
*
************************************************/
void SetDisplayPosition(UINT8 row, UINT8 column)
{
   UINT8 *displayControl = DISPLAY_CONTROL_LOCATION;
   UINT8 i;

   cursorPosition.row = row;
   cursorPosition.column = column;
   i=((cursorPosition.row&0x01)*0x40) + 
       ((cursorPosition.row&0x02)/2*MAX_DISPLAY_LINE_LENGTH) +
         cursorPosition.column;
   *displayControl = 0x80+i;
   wait(1000);
}

/************************************************
*
*   Function:     WriteDisplay
*
*   Inputs:       text - reference to null terminated
*                 string of text to be output.
*
*   Outputs:      Adds text string to buffer.
*
*   Returns:      TRUE if successful, FALSE otherwise
*
*   Description:  Calls WriteDisplayAt with row and
*                 column set to 0xFF, which indicates
*                 current row.
*
************************************************/
BOOLEAN WriteDisplay(const char * text)
{
   WriteDisplayAt(0xff,0xff,text);
}

/************************************************
*
*   Function:     WriteDisplayAt
*
*   Inputs:       row, column - row/column to which to
*                 begin writing the text.
*                 text - reference to null terminated
*                 string of text to be output.
*
*   Outputs:      Adds text string to buffer.
*
*   Returns:      TRUE if successful, FALSE otherwise
*
*   Description:  Checks buffer for room for message.
*                 If room exists, copies row and column
*                 to buffer, increments buffer, and
*                 activates task OutputDisplay to actually
*                 output the data to the display.
*
************************************************/
BOOLEAN WriteDisplayAt(UINT8 row, UINT8 column, const char * text)
{
BOOLEAN result=TRUE;

   if(displayBufferError == FALSE){
      SuspendOSInterrupts();
      displayBuffer[displayBufferEnd].row = row;
      displayBuffer[displayBufferEnd].column = column;
      strcpy(&displayBuffer[displayBufferEnd].buffer,text);
      if(++displayBufferEnd == DISPLAY_BUFFER_SIZE){
         displayBufferEnd = 0;
      }
      if(displayBufferEnd == displayBufferStart){
         displayBufferError = TRUE;
      }
      ResumeOSInterrupts();
      ActivateTask(OutputDisplay);
   }
   else{
      result = FALSE;
   }
   return result;
}

#endif /* DISPDRVC */

⌨️ 快捷键说明

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