static void codec_send(rt_uint16_t s_data) { rt_sem_take(&spi1_lock, RT_WAITING_FOREVER); /* SPI1 configure */ rt_hw_spi1_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */ codec_reset_csb(); SPI_WriteByte((s_data >> 8) & 0xFF); SPI_WriteByte(s_data & 0xFF); codec_set_csb(); rt_sem_release(&spi1_lock); }
static void rtgui_touch_calculate() { if (touch != RT_NULL) { rt_sem_take(&spi1_lock, RT_WAITING_FOREVER); /* SPI1 configure */ rt_hw_spi1_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */ //读取触摸值 { rt_uint16_t tmpx[10]; rt_uint16_t tmpy[10]; unsigned int i; for(i=0; i<10; i++) { CS_0(); WriteDataTo7843(TOUCH_MSR_X); /* read X */ tmpx[i] = SPI_WriteByte(0x00)<<4; /* read MSB bit[11:8] */ tmpx[i] |= ((SPI_WriteByte(TOUCH_MSR_Y)>>4)&0x0F ); /* read LSB bit[7:0] */ tmpy[i] = SPI_WriteByte(0x00)<<4; /* read MSB bit[11:8] */ tmpy[i] |= ((SPI_WriteByte(0x00)>>4)&0x0F ); /* read LSB bit[7:0] */ WriteDataTo7843( 1<<7 ); /* 打开中断 */ CS_1(); } //去最高值与最低值,再取平均值 { rt_uint32_t min_x = 0xFFFF,min_y = 0xFFFF; rt_uint32_t max_x = 0,max_y = 0; rt_uint32_t total_x = 0; rt_uint32_t total_y = 0; unsigned int i; for(i=0;i<10;i++) { if( tmpx[i] < min_x ) { min_x = tmpx[i]; } if( tmpx[i] > max_x ) { max_x = tmpx[i]; } total_x += tmpx[i]; if( tmpy[i] < min_y ) { min_y = tmpy[i]; } if( tmpy[i] > max_y ) { max_y = tmpy[i]; } total_y += tmpy[i]; } total_x = total_x - min_x - max_x; total_y = total_y - min_y - max_y; touch->x = total_x / 8; touch->y = total_y / 8; }//去最高值与最低值,再取平均值 }//读取触摸值 rt_sem_release(&spi1_lock); /* if it's not in calibration status */ if (touch->calibrating != RT_TRUE) { if (touch->max_x > touch->min_x) { touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x); } else { touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x); } if (touch->max_y > touch->min_y) { touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y); } else { touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y); } } }
static void rtgui_touch_calculate() { if (touch != RT_NULL) { rt_sem_take(&spi1_lock, RT_WAITING_FOREVER); /* SPI1 configure */ rt_hw_spi1_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */ //读取触摸值 { rt_uint16_t tmpx[10]; rt_uint16_t tmpy[10]; unsigned int i; /* From the datasheet: * When the very first CLK after the control byte comes in, the * DOUT of ADS7843 is not valid. So we could only get 7bits from * the first SPI_WriteByte. And the got the following 5 bits from * another SPI_WriteByte.(aligned MSB) */ for(i=0; i<10; i++) { CS_0(); WriteDataTo7843(TOUCH_MSR_X); tmpx[i] = (SPI_WriteByte(0x00) & 0x7F) << 5; tmpx[i] |= (SPI_WriteByte(TOUCH_MSR_Y) >> 3) & 0x1F; tmpy[i] = (SPI_WriteByte(0x00) & 0x7F) << 5; tmpy[i] |= (SPI_WriteByte(0x00) >> 3) & 0x1F; WriteDataTo7843( 1<<7 ); /* 打开中断 */ CS_1(); } //去最高值与最低值,再取平均值 { rt_uint32_t min_x = 0xFFFF,min_y = 0xFFFF; rt_uint32_t max_x = 0,max_y = 0; rt_uint32_t total_x = 0; rt_uint32_t total_y = 0; unsigned int i; for(i=0;i<10;i++) { if( tmpx[i] < min_x ) { min_x = tmpx[i]; } if( tmpx[i] > max_x ) { max_x = tmpx[i]; } total_x += tmpx[i]; if( tmpy[i] < min_y ) { min_y = tmpy[i]; } if( tmpy[i] > max_y ) { max_y = tmpy[i]; } total_y += tmpy[i]; } total_x = total_x - min_x - max_x; total_y = total_y - min_y - max_y; touch->x = total_x / 8; touch->y = total_y / 8; }//去最高值与最低值,再取平均值 }//读取触摸值 rt_sem_release(&spi1_lock); /* if it's not in calibration status */ if (touch->calibrating != RT_TRUE) { if (touch->max_x > touch->min_x) { touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x); } else if (touch->max_x < touch->min_x) { touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x); } if (touch->max_y > touch->min_y) { touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y); } else if (touch->max_y < touch->min_y) { touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y); } // normalize the data if (touch->x & 0x8000) touch->x = 0; else if (touch->x > X_WIDTH) touch->x = X_WIDTH - 1; if (touch->y & 0x8000) touch->y = 0; else if (touch->y > Y_WIDTH) touch->y = Y_WIDTH - 1; } }