CTS_ctrl.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:11k
源码类别:

MTK

开发平台:

C++ Builder

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *   CTS_ctrl.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  Clima temperature system (CTS) control function source
  48.  *
  49.  * Author:
  50.  * -------
  51.  *  Andy Ueng (mtk00490)
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * $Revision$
  58.  * $Modtime$
  59.  * $Log$
  60.  *
  61.  *------------------------------------------------------------------------------
  62.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  63.  *============================================================================
  64.  ****************************************************************************/
  65. #include <vcl.h>
  66. #pragma hdrstop
  67. #ifndef  _CTS_CTRL_H_
  68. #include "cts_ctrl.h"
  69. #endif
  70. #ifndef  _FT_UTILS_H_
  71. #include "ft_utils.h"
  72. #endif
  73. /*---------------------------------------------------------------------------*/
  74. static const char  CTS_CMD_BEGIN            =  0x02;
  75. static const char  CTS_CMD_ADR              =  0x81;
  76. static const char  CTS_CMD_START            =  0xF0;
  77. static const char  CTS_CMD_STOP             =  0xF0;
  78. static const char  CTS_CMD_TEMP_CHANNEL     =  0xB0;
  79. static const char  CTS_CMD_SET_ANALOG       =  0xE1;
  80. static const char  CTS_CMD_GET_ANALOG       =  0xC1;
  81. static const char  CTS_CHANNEL_TEMP         =  0xB0;
  82. static const char  CTS_CMD_END              =  0x03;
  83. static const char  CTS_BLANK                =  0xA0;
  84. #define  CTS_CMD_BUF_SIZE  256
  85. //===========================================================================
  86. CCTSCTRL::CCTSCTRL( void )
  87. {
  88. }
  89. //---------------------------------------------------------------------------
  90. CCTSCTRL::~CCTSCTRL()
  91. {
  92. }
  93. //=============================================================================
  94. bool CCTSCTRL::CTS_Init(int com_port)
  95. {
  96.     sUART  uart;
  97.     uart.com_port  = com_port;
  98.     uart.baud_rate = CBR_19200;
  99.     uart.byte_size = 8;
  100.     uart.parity    = ODDPARITY;
  101.     uart.stop_bits = ONESTOPBIT;
  102.     if(! UART_Obj.UART_Open( hCOM, uart ) )   return false;
  103.     return true;
  104. }
  105. //--------------------------------------------------------------------------
  106. bool CCTSCTRL::CTS_Close(void)
  107. {
  108.    UART_Obj.UART_Close( hCOM );
  109.    return true;
  110. }
  111. //--------------------------------------------------------------------------
  112. bool CCTSCTRL::CTS_Start( void )
  113. {
  114.     char cmd_data[CTS_CMD_BUF_SIZE];
  115.     char cmd[CTS_CMD_BUF_SIZE];
  116.     cmd_data[0] = CTS_CMD_ADR;
  117.     cmd_data[1] = CTS_CMD_START;
  118.     cmd_data[2] = 0xB0;
  119.     cmd_data[3] = 0xB0;
  120.     cmd_data[4] = 0xB1;
  121.     cmd_data[5] = '';
  122.     CTS_ComposeCmd( cmd_data, cmd );
  123.     UART_Obj.UART_SendData( hCOM, cmd );
  124.     return true;
  125. }
  126. //--------------------------------------------------------------------------
  127. bool CCTSCTRL::CTS_Stop( void )
  128. {
  129.     char cmd_data[CTS_CMD_BUF_SIZE];
  130.     char cmd[CTS_CMD_BUF_SIZE];
  131.     cmd_data[0] = CTS_CMD_ADR;
  132.     cmd_data[1] = CTS_CMD_STOP;
  133.     cmd_data[2] = 0xB0;
  134.     cmd_data[3] = 0xB0;
  135.     cmd_data[4] = 0xB0;
  136.     cmd_data[5] = '';
  137.     CTS_ComposeCmd( cmd_data, cmd );
  138.     UART_Obj.UART_SendData( hCOM, cmd );
  139.     return true;
  140. }
  141. //--------------------------------------------------------------------------
  142. bool CCTSCTRL::CTS_SetTemp( double temp )
  143. {
  144.     char cmd_temp[6];
  145.     char cmd_data[CTS_CMD_BUF_SIZE];
  146.     char cmd[CTS_CMD_BUF_SIZE];
  147.     int  i=0;
  148.     double_To_Array_ASCII_char( temp, cmd_temp );
  149.     cmd_temp[5] = '';
  150.     while( i<sizeof(cmd_temp) && cmd_temp[i]!='' )
  151.     {
  152.         cmd_temp[i] |= 0x80;
  153.         i++;
  154.     }
  155.     cmd_data[0] = CTS_CMD_ADR;
  156.     cmd_data[1] = CTS_CMD_SET_ANALOG;
  157.     cmd_data[2] = CTS_CMD_TEMP_CHANNEL;
  158.     cmd_data[3] = CTS_BLANK;
  159.     cmd_data[4] = '';
  160.     strcat(cmd_data, cmd_temp);
  161.     CTS_ComposeCmd( cmd_data, cmd );
  162.     UART_Obj.UART_SendData( hCOM, cmd );
  163.     return true;
  164. }
  165. //--------------------------------------------------------------------------
  166. bool CCTSCTRL::CTS_GetTemp( double &effective_temp, double &index_temp )
  167. {
  168.     char cmd_data[CTS_CMD_BUF_SIZE];
  169.     char cmd[CTS_CMD_BUF_SIZE];
  170.     char received_data[CTS_CMD_BUF_SIZE];
  171.     DWORD dw_byte_read;
  172.     cmd_data[0] = CTS_CMD_ADR;
  173.     cmd_data[1] = CTS_CMD_GET_ANALOG;
  174.     cmd_data[2] = CTS_CMD_TEMP_CHANNEL;
  175.     cmd_data[3] = '';
  176.     CTS_ComposeCmd( cmd_data, cmd );
  177.     UART_Obj.UART_SendData( hCOM, cmd );
  178.     for(int i=0; i<5; i++)
  179.     {
  180.         Sleep(500);
  181.         if( UART_Obj.UART_ReceiveData( hCOM, received_data, dw_byte_read ) ) break;
  182.     }
  183.     #if 0
  184.     received_data[0]  = 0x02;
  185.     received_data[1]  = 0x81;
  186.     received_data[2]  = 0xC1;
  187.     received_data[3]  = 0xB0;
  188.     received_data[4]  = 0xA0;
  189.     received_data[5]  = 0xAD;
  190.     received_data[6]  = 0xB1;
  191.     received_data[7]  = 0xB4;
  192.     received_data[8]  = 0xAE;
  193.     received_data[9]  = 0xB5;
  194.     received_data[10] = 0xA0;
  195.     received_data[11] = 0xAD;
  196.     received_data[12] = 0xB1;
  197.     received_data[13] = 0xB3;
  198.     received_data[14] = 0xAE;
  199.     received_data[15] = 0xB8;
  200.     received_data[16] = 0xFA;
  201.     received_data[17] = 0x03;
  202.     dw_byte_read = 18;
  203.     #endif
  204.     if(! CTS_ExtractTemp( received_data, dw_byte_read, effective_temp, index_temp ) )  return false;
  205.     return true;
  206. }
  207. //--------------------------------------------------------------------------
  208. bool CCTSCTRL::CTS_ExtractTemp( char* received_data, DWORD dw_byte_read, double &effective_temp, double &index_temp )
  209. {
  210.     char Temp_Header[5];
  211.     char effective_temp_array[6];
  212.     char index_temp_array[6];
  213.     int i;
  214.     if( ! CTS_TempHeaderBytesCheck(received_data)                                   ||
  215.         ! CTS_TempChkByteCheck( received_data, dw_byte_read, *(received_data+16) ) ||
  216.         ! CTS_TempTailByteCheck( received_data )
  217.       )
  218.       return false;
  219.     for(i=0;i<sizeof(effective_temp_array)-1; i++)
  220.     {
  221.         effective_temp_array[i] = *(received_data+5+i);
  222.     }
  223.     effective_temp_array[i]='';
  224.     ASCII_char_To_Array_double( effective_temp_array, effective_temp );
  225.     for(i=0;i<sizeof(index_temp_array)-1; i++)
  226.     {
  227.         index_temp_array[i] = *(received_data+11+i);
  228.     }
  229.     index_temp_array[i] = '';
  230.     ASCII_char_To_Array_double( index_temp_array, index_temp );
  231.     return true;
  232. }
  233. //--------------------------------------------------------------------------
  234. bool CCTSCTRL::CTS_TempHeaderBytesCheck( char* received_data )
  235. {
  236.     char Temp_Header[2];
  237.     Temp_Header[0] = 0x02;
  238.     Temp_Header[1] = 0x81;
  239.    // Temp_Header[2] = 0xC1;
  240.    // Temp_Header[3] = 0xB0;
  241.    // Temp_Header[4] = 0xA0;
  242.     for(int i=0; i<sizeof(Temp_Header); i++)
  243.     {
  244.        if( *(received_data+i) != Temp_Header[i] )  return false;
  245.     }
  246.     return true;
  247. }
  248. //--------------------------------------------------------------------------
  249. bool CCTSCTRL::CTS_TempChkByteCheck( char* received_data, DWORD dw_byte_read, char &r_chk )
  250. {
  251.     char c_chk=0;
  252.     for (unsigned int i=1; i<dw_byte_read-2; i++)
  253.     {
  254.         c_chk ^= *(received_data+i);
  255.     }
  256.     if(c_chk != r_chk)  return false;
  257.     return true;
  258. }
  259. //--------------------------------------------------------------------------
  260. bool CCTSCTRL::CTS_TempTailByteCheck( char* received_data )
  261. {
  262.     if( *(received_data+17) != CTS_CMD_END )  return false;
  263.     return true;
  264. }
  265. //--------------------------------------------------------------------------
  266. bool CCTSCTRL::CTS_ComposeCmd( char *cmd_data, char* cmd )
  267. {
  268.     char  cmd_chk=0;
  269.     int   i=0;
  270.     int   len;
  271.     while( cmd_data[i]!=0 && i<CTS_CMD_BUF_SIZE )
  272.     {
  273.         cmd_chk ^= cmd_data[i];
  274.         i++;
  275.     }
  276.     cmd_chk |= 0x80;
  277.     cmd[0] = CTS_CMD_BEGIN;
  278.     cmd[1] = '';
  279.     strcat(cmd, cmd_data);
  280.     len = strlen(cmd);
  281.     cmd[len]= cmd_chk;
  282.     cmd[len+1] = CTS_CMD_END;
  283.     cmd[len+2] = '';
  284.     return true;
  285. }
  286. //===========================================================================
  287. ////////////////////////////  Global information  ///////////////////////////
  288. //============================================================================
  289. HANDLE CCTSCTRL::CTS_GetComHandle( void )
  290. {
  291.     return hCOM;
  292. }
  293. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  294. DCB CCTSCTRL::Get_OriginalDCB( void )
  295. {
  296.      DCB dcb = UART_Obj.Get_OriginalDCB( );
  297.      return dcb;
  298. }
  299. //---------------------------------------------------------------------------
  300. bool CCTSCTRL::Set_OriginalDCB( DCB &dcb )
  301. {
  302.      UART_Obj.Set_OriginalDCB( dcb );
  303.      return true;
  304. }