/* 获取BMP文件的位图数据(打开1bit/像素) */ int get_bmp_data(bmp_t *pstBmp, const char *pcFileName) { FILE *fp; BITMAPFILEHEADER stBmpFileHeader; BITMAPINFOHEADER stBmpInfoHeader; /* 输入参数检查 */ if(INVALID_POINTER(pstBmp) || INVALID_POINTER(pcFileName)) { DEBUG_MSG("E:input param error.\r\n"); return -1; } /* 打开bmp文件 */ if((fp = fopen(pcFileName, "rb")) == NULL) { DEBUG_MSG("E:Error opening file %s.\r\n", pcFileName); return -1; } /* 读BMP文件头信息(分开读取) */ fread(&(stBmpFileHeader.bfType), sizeof(stBmpFileHeader.bfType), 1, fp); fread(&(stBmpFileHeader.bfSize), sizeof(stBmpFileHeader.bfSize), 1, fp); fread(&(stBmpFileHeader.bfReserved1), sizeof(stBmpFileHeader.bfReserved1), 1, fp); fread(&(stBmpFileHeader.bfReserved2), sizeof(stBmpFileHeader.bfReserved2), 1, fp); fread(&(stBmpFileHeader.bfOffBits), sizeof(stBmpFileHeader.bfOffBits), 1, fp); /* 打印文件头信息 */ print_bmp_file_header(&stBmpFileHeader); if(0x4d42 != stBmpFileHeader.bfType) { DEBUG_MSG("E:is not bmp file.\r\n"); return -1; } /* 读位图信息 */ if(0 > fread(&stBmpInfoHeader, sizeof(stBmpInfoHeader), 1, fp)) { ERROR_MSG("E:fread bmiHeader error!\r\n"); } /* 打印位图信息头 */ print_bmp_info_header(&stBmpInfoHeader); /* 保存信息 */ pstBmp->usBitCount = stBmpInfoHeader.biBitCount; pstBmp->ulWidth = stBmpInfoHeader.biWidth; pstBmp->ulHeight = stBmpInfoHeader.biHeight; pstBmp->ulRowBytes = (((pstBmp->ulWidth)*(pstBmp->usBitCount) + 31) >> 5) << 2; pstBmp->pucData = (unsigned char *)malloc((pstBmp->ulHeight)*(pstBmp->ulRowBytes)); if(NULL == pstBmp->pucData) { DEBUG_MSG("E:malloc error.\r\n"); return -1; } fseek(fp, stBmpFileHeader.bfOffBits, SEEK_SET); fread(pstBmp->pucData, (pstBmp->ulHeight)*(pstBmp->ulRowBytes), 1, fp); /* 关闭文件 */ fclose(fp); return 0; }
/* 阻塞读CAN */ int can_read(struct can_frame *pstCanFrame) { int ret = 0; if((canfd <= 0) || INVALID_POINTER(pstCanFrame)) { DEBUG_MSG("E:input param error!\r\n"); return -1; } ret = read(canfd, pstCanFrame, sizeof(*pstCanFrame)); if(ret < sizeof(*pstCanFrame)) { ERROR_MSG("E:read failed!\r\n"); return -1; } /* 判断设备是否错误 */ if(pstCanFrame->can_id & CAN_ERR_FLAG) { handle_err_frame(pstCanFrame); DEBUG_MSG("E:CAN device error!\r\n"); return -1; } /* 打印CAN帧 */ print_frame(pstCanFrame); return 0; }
/* 打印bmp文件头信息 */ static void print_bmp_file_header(BITMAPFILEHEADER *pstBmpFileHeader) { /* 输入参数检查 */ if(INVALID_POINTER(pstBmpFileHeader)) { DEBUG_MSG("E:input param error.\r\n"); return; } DEBUG_MSG("D:bmp file header > type=0x%04x, size=%d, offbits=%d\r\n", \ pstBmpFileHeader->bfType, \ pstBmpFileHeader->bfSize, \ pstBmpFileHeader->bfOffBits); }
/* can发送 */ int can_write(struct can_frame *pstCanFrame) { if((canfd <= 0) || INVALID_POINTER(pstCanFrame)) { DEBUG_MSG("E:input param error!\r\n"); return -1; } if(0 > write(canfd, pstCanFrame, sizeof(*pstCanFrame))) { DEBUG_MSG("E: can write failed!\r\n"); return -1; } else { return 0; } }
/* 打印GPS信息 */ static int print_gps_info(gps_info_t *pstGpsInfo) { if(INVALID_POINTER(pstGpsInfo)) { DEBUG_MSG("E:input param error!\r\n"); return -1; } printf("GPS INFO: "); printf("utc_date=%s ", pstGpsInfo->utc_data); printf("time=%s ", pstGpsInfo->utc_time); printf("status=%c ", pstGpsInfo->status); printf("latitude=%f ", pstGpsInfo->latitude_value); printf("N/S=%c ", pstGpsInfo->latitude); printf("longtitude=%f ", pstGpsInfo->longtitude_value); printf("E/W=%c ", pstGpsInfo->longtitude); printf("speed=%f ", pstGpsInfo->speed); printf("azimuth_angle=%f ", pstGpsInfo->azimuth_angle); printf("\r\n"); }
/* 打印位图信息头 */ static void print_bmp_info_header(BITMAPINFOHEADER *pstBmpInfoHeader) { /* 输入参数检查 */ if(INVALID_POINTER(pstBmpInfoHeader)) { DEBUG_MSG("E:input param error.\r\n"); return; } DEBUG_MSG("D:bmp info header > size=%d, width=%d, height=%d, bitcount=%d, \ biCompression=%d, biSizeImage=%d, biXPelsPerMeter=%d, biYPelsPerMeter=%d, \ biClrUsed=%d, biClrImportant=%d\r\n", \ pstBmpInfoHeader->biSize, \ pstBmpInfoHeader->biWidth, \ pstBmpInfoHeader->biHeight, \ pstBmpInfoHeader->biBitCount, \ pstBmpInfoHeader->biCompression, \ pstBmpInfoHeader->biSizeImage, \ pstBmpInfoHeader->biXPelsPerMeter, \ pstBmpInfoHeader->biYPelsPerMeter, \ pstBmpInfoHeader->biClrUsed, \ pstBmpInfoHeader->biClrImportant); }
/*! \fn int32 put_string(int8 *pPutStr) * * \brief 输出字符串(在调试串口) * * \param *pPutStr [in] 指向输出的字符串 * * \exception 无 * * \return EXIT_SUCCESS:成功,EXIT_FAILURE:失败 */ int32 put_string(int8 *pPutStr) { int32 lRet = EXIT_FAILURE; uint16 unOutStrLen = 0; do { /*! 输入参数检查 */ if(INVALID_POINTER(pPutStr)) { break; } /*! 计算输出字符串的长度 */ unOutStrLen = 0; while('\0' != *(pPutStr+unOutStrLen)) { unOutStrLen++; } /*! 串口输出 */ lRet = uart_send((uint8 *)pPutStr, unOutStrLen); }while(0); return lRet; }
Sprite::~Sprite(void) { m_hostWnd = NULL; Sprite *sp = m_firstChild; while(sp) { Sprite *tmp = sp->m_nextSibling; sp->Unref(); // FIXME 有没有想过这里其实如果数量太多 会不会爆栈呢? 事实上是一个递归调用. sp = tmp; } m_firstChild = INVALID_POINTER(Sprite); m_lastChild = INVALID_POINTER(Sprite); m_prevSibling = INVALID_POINTER(Sprite); m_nextSibling = INVALID_POINTER(Sprite); m_parent = INVALID_POINTER(Sprite); //ClearPointer(m_parent); delete m_luaSide; m_luaSide = INVALID_POINTER(SpriteLua); LOG(<<"sprite deleted"); // TODO 加个名字 }
/* 发送数据到CAN */ int send_data_to_can(int lBoardType, char *pcCmd, int lCmdLen) { struct can_frame stCanFrame; int i = 0; uint8 ucCnt = 0; uint16 unTemp = 0; if(INVALID_POINTER(pcCmd) || (1 > lCmdLen) || (1 > lBoardType)) { DEBUG_MSG("E:input param error!\r\n"); return -1; } /*! 计算数据需要几个报文帧 */ unTemp = lCmdLen / 8; if(0x0000 != (lCmdLen & 0x0007)) { unTemp = unTemp + 1; } /*! 数据不大于8字节,无需分段 */ if(1 == unTemp) { stCanFrame.can_id = 0x80000000 | (15 << 22) | (lBoardType << 16) |FRAME_NONE_SEG; stCanFrame.can_dlc = lCmdLen; for(i = 0; i < lCmdLen; i++) { stCanFrame.data[i] = pcCmd[i]; } if(0 > can_write(&stCanFrame)) { return -1; } } else { /*! 第一个分段报文 */ stCanFrame.can_id = 0x80000000 | (15 << 22) | (lBoardType << 16) | FRAME_FIRST_SEG; stCanFrame.can_dlc = 8; for(i = 0; i<8; i++) { stCanFrame.data[i] = *pcCmd; pcCmd++; } if(0 > can_write(&stCanFrame)) { return -1; } unTemp--; ucCnt = 1; /*! 中间分段报文 */ while(1 != unTemp) { stCanFrame.can_id = 0x80000000 | (15 << 22) | (lBoardType << 16) | FRAME_MIDDLE_SEG | (ucCnt << 8); stCanFrame.can_dlc = 8; for(i = 0; i<8; i++) { stCanFrame.data[i] = *pcCmd; pcCmd++; } if(0 > can_write(&stCanFrame)) { return -1; } unTemp--; ucCnt++; } /*! 最后分段报文 */ if(0x0000 == (lCmdLen & 0x0007)) { unTemp = 8; } else { unTemp = lCmdLen & 0x0007; } ucCnt = 0; stCanFrame.can_id = 0x80000000 | (15 << 22) | (lBoardType << 16) | FRAME_END_SEG | (ucCnt << 8); stCanFrame.can_dlc = unTemp; for(i = 0; i<unTemp; i++) { stCanFrame.data[i] = *pcCmd; pcCmd++; } if(0 > can_write(&stCanFrame)) { return -1; } } return 0; }
/*! \fn int32 put_file_line(int8 *pFile, uint16 ulLine) * * \brief 输出调用此函数的文件名及行号(debug_msg(__FILE__, __LINE__);) * * \param *pFile [in] 指向文件名字符串 * * \param ulLine [in] 行号(不能大于65535) * * \exception 无 * * \return EXIT_SUCCESS:成功,EXIT_FAILURE:失败 */ int32 put_file_line(int8 *pFile, uint16 ulLine) { uint16 unFileSize = 0; uint8 aLineBuf[16] = {0}; uint8 aLineString[16] = {0}; uint8 ucLineSize = 0; uint16 ulTemp = 0; uint8 i = 0; if(INVALID_POINTER(pFile)) { return EXIT_FAILURE; } /*! 计算__FILE__字符串长度 */ while('\0' != *(pFile+unFileSize)) { unFileSize++; } /*! 2013-05-22 只打印最后一个“\”后的内容 */ for(ulTemp=(unFileSize-1); ulTemp != 0; ulTemp--) { if(0x5C == *(pFile+ulTemp)) { break; } } /*! 串口输出文件名称 */ uart_send(" ", 1); uart_send((uint8 *)(pFile+ulTemp), (unFileSize-ulTemp)); /*! 将__LINE__转换为字符串 */ aLineString[0] = ' '; if(ulLine < 1) { ucLineSize = 1; aLineString[1] = '0'; aLineString[2] = '\r'; aLineString[3] = '\n'; } else { ucLineSize = 0; ulTemp = ulLine; while(ulTemp != 0) { aLineBuf[ucLineSize] = ulTemp % 10 + '0'; ulTemp = ulTemp / 10; ucLineSize++; } /*! 字符串反顺序 */ for(i=0; i<ucLineSize; i++) { aLineString[i+1] = aLineBuf[ucLineSize-i-1]; } aLineString[ucLineSize+1] = '\r'; /*!< 字符串结尾 */ aLineString[ucLineSize+2] = '\n'; } /*! 串口输出行号加换行 */ uart_send(aLineString, ucLineSize+3); return EXIT_SUCCESS; }
/*! \fn int32_t can_send(uint8_t *pucSendBuf, uint16_t unSendLen) * * \brief CAN发送(输入必须为完整的通信帧) * * \param pucSendBuf [in] 指向发送缓冲区 * * \param unSendLen [in] 需发送数据的字节数 * * \exception 无 * * \return EXIT_SUCCESS:成功; EXIT_FAILURE:失败 */ int32_t can_send(uint8_t *pucSendBuf, uint16_t unSendLen) { int32 lRet = EXIT_FAILURE; uint8 ucCnt = 0; uint16 i = 0, unDataCnt = 0, unTemp = 0; do { /*! 输入参数检查 */ if(INVALID_POINTER(pucSendBuf) \ || (1 > unSendLen) || (CAN_MSG_BUF_SIZE < unSendLen)) { break; } unTemp = unSendLen / 8; if(0x0000 != (unSendLen&0x0007)) { unTemp = unTemp + 1; } while(1) { /*! 计算报文发送缓冲区空余空间 */ if(s_unCanMsgTxWrIndex >= s_unCanMsgTxRdIndex) { unDataCnt = s_unCanMsgTxWrIndex - s_unCanMsgTxRdIndex; unDataCnt = CAN_MSG_BUF_SIZE - unDataCnt; } else { unDataCnt = s_unCanMsgTxRdIndex - s_unCanMsgTxWrIndex; } /*! 发送缓冲区 */ if(unDataCnt >= unTemp) { break; } /*! 添加超时,防止死循环 */ } /*! 无分段 */ if(1 == unTemp) { s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].ExtId = 0x00010000; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].IDE = CAN_Id_Extended; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].RTR = CAN_RTR_Data; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].DLC = (uint8)unSendLen; for(i=0; i<(uint8)unSendLen; i++) { (s_astCanMsgTxBuf[s_unCanMsgTxWrIndex]).Data[i] = *pucSendBuf; pucSendBuf++; } s_unCanMsgTxWrIndex++; if(CAN_MSG_BUF_SIZE <= s_unCanMsgTxWrIndex) { s_unCanMsgTxWrIndex = 0; } } else /*!< 帧分为多个报文段 */ { /*! 第一个分段 */ s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].ExtId = 0x00014000; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].IDE = CAN_Id_Extended; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].RTR = CAN_RTR_Data; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].DLC = 8; for(i=0; i<8; i++) { (s_astCanMsgTxBuf[s_unCanMsgTxWrIndex]).Data[i] = *pucSendBuf; pucSendBuf++; } s_unCanMsgTxWrIndex++; if(CAN_MSG_BUF_SIZE <= s_unCanMsgTxWrIndex) { s_unCanMsgTxWrIndex = 0; } unTemp--; ucCnt = 1; /*! 中间段 */ while(1 != unTemp) { s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].ExtId = 0x00018000 + (ucCnt<<8); s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].IDE = CAN_Id_Extended; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].RTR = CAN_RTR_Data; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].DLC = 8; for(i=0; i<8; i++) { (s_astCanMsgTxBuf[s_unCanMsgTxWrIndex]).Data[i] = *pucSendBuf; pucSendBuf++; } s_unCanMsgTxWrIndex++; if(CAN_MSG_BUF_SIZE <= s_unCanMsgTxWrIndex) { s_unCanMsgTxWrIndex = 0; } unTemp--; ucCnt++; } /*! 结束段 */ if(0x0000 == (unSendLen&0x0007)) { unTemp = 8; } else { unTemp = unSendLen & 0x0007; } ucCnt = 0; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].ExtId = 0x0001C000 + (ucCnt<<8);; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].IDE = CAN_Id_Extended; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].RTR = CAN_RTR_Data; s_astCanMsgTxBuf[s_unCanMsgTxWrIndex].DLC = unTemp; for(i=0; i<unTemp; i++) { (s_astCanMsgTxBuf[s_unCanMsgTxWrIndex]).Data[i] = *pucSendBuf; pucSendBuf++; } s_unCanMsgTxWrIndex++; if(CAN_MSG_BUF_SIZE <= s_unCanMsgTxWrIndex) { s_unCanMsgTxWrIndex = 0; } } /*! 发送消息 */ lRet = send_can_msg(); NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn); }while(0); return lRet; }