优化这段代码 void Controller::onShowDeviceTask() { for(auto it = ClientObjects.begin(); it != ClientObjects.end() ; it++) { if(it.value()->Item != sender()) continue; emit SendCmd2Client(it.key(),"GetTaskList"); break; } }
时间: 2023-05-16 08:07:39 浏览: 48
这段代码可以进行如下优化:
void Controller::onShowDeviceTask() {
for(auto it = ClientObjects.begin(); it != ClientObjects.end() ; ++it) {
if(it.value()->Item == sender()) {
emit SendCmd2Client(it.key(),"GetTaskList");
break;
}
}
}
优化后的代码中,我们将自增操作it++改为了++it,这样可以避免产生临时变量,提高代码效率。另外,我们将if语句中的条件判断改为了等于判断,这样可以避免不必要的continue语句,提高代码可读性。
相关问题
使用STM32cubemx建立工程文件,用于读取ADC128S芯片的八个通道,请补全main.c代码
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "spi.h"
#include "gpio.h"
#define ADC128S_CS_GPIO_Port GPIOA
#define ADC128S_CS_Pin GPIO_PIN_4
#define ADC128S_NUM_CHANNELS 8
uint16_t adc128s_values[ADC128S_NUM_CHANNELS];
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SPI1_Init();
MX_ADC_Init();
/* Infinite loop */
while (1)
{
uint8_t channel;
uint8_t tx_data[2] = {0};
uint8_t rx_data[2] = {0};
GPIO_PinState cs_state;
for (channel = 0; channel < ADC128S_NUM_CHANNELS; channel++)
{
/* Set ADC128S CS low */
HAL_GPIO_WritePin(ADC128S_CS_GPIO_Port, ADC128S_CS_Pin, GPIO_PIN_RESET);
/* Build SPI transaction */
tx_data[0] = (0x80 | (channel << 4));
HAL_SPI_TransmitReceive(&hspi1, tx_data, rx_data, sizeof(tx_data), 100);
/* Set ADC128S CS high */
HAL_GPIO_WritePin(ADC128S_CS_GPIO_Port, ADC128S_CS_Pin, GPIO_PIN_SET);
/* Store ADC value */
adc128s_values[channel] = ((rx_data[0] & 0x0F) << 8) | rx_data[1];
}
/* Wait for DMA transfer to complete */
HAL_ADC_Start_DMA(&hadc, (uint32_t*)adc128s_values, ADC128S_NUM_CHANNELS);
HAL_Delay(1);
while (HAL_ADC_PollForConversion(&hadc, 100) != HAL_OK);
/* Process ADC values */
/* ... */
}
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* Enable DMA controller clock
*/
void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure GPIO pin Output Level */
HAL_GPIO_WritePin(ADC128S_CS_GPIO_Port, ADC128S_CS_Pin, GPIO_PIN_SET);
/* Configure GPIO pin : ADC128S_CS_Pin */
GPIO_InitStruct.Pin = ADC128S_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(ADC128S_CS_GPIO_Port, &GPIO_InitStruct);
}
/* SPI1 init function */
void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC_Init 0 */
/* USER CODE END ADC_Init 0 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.NbrOfConversion = ADC128S_NUM_CHANNELS;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_4;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = ADC_REGULAR_RANK_6;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = ADC_REGULAR_RANK_7;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_7;
sConfig.Rank = ADC_REGULAR_RANK_8;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
/* USER CODE END ADC_Init 2 */
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
使用Unity实现在pico中在黑板上写字代码
以下是使用Unity在Pico中实现在黑板上写字的代码:
1. 创建一个Plane对象,并将其旋转90度,使其成为一个平面。
```
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.transform.Rotate(new Vector3(90, 0, 0));
```
2. 创建一个材质,并将其贴到Plane上。
```
Material material = new Material(Shader.Find("Standard"));
plane.GetComponent<Renderer>().material = material;
```
3. 创建一个RenderTexture,用于渲染黑板上的内容。
```
RenderTexture renderTexture = new RenderTexture(1024, 1024, 24);
```
4. 将RenderTexture设置为材质的主纹理。
```
material.mainTexture = renderTexture;
```
5. 创建一个Camera,并将其渲染目标设置为RenderTexture。
```
GameObject cameraObject = new GameObject("Camera");
Camera camera = cameraObject.AddComponent<Camera>();
camera.targetTexture = renderTexture;
```
6. 将Camera的位置设置为黑板的位置,并使其朝向黑板。
```
cameraObject.transform.position = new Vector3(0, 1, -2);
cameraObject.transform.LookAt(plane.transform);
```
7. 创建一个画笔对象,并将其放在Pico的手柄上。
```
GameObject brush = GameObject.CreatePrimitive(PrimitiveType.Sphere);
brush.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
brush.GetComponent<Renderer>().material.color = Color.black;
brush.transform.parent = PicoUnityActivity.instance.ControllerRight.transform;
brush.transform.localPosition = new Vector3(0, 0, 0.1f);
```
8. 在Update函数中,检测手柄的触发键是否按下,并在黑板上绘制画笔的位置。
```
void Update()
{
if (PicoInputManager.Controller.Pico_ControllerRight.GetPress(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))
{
Ray ray = new Ray(brush.transform.position, brush.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector2 uv = hit.textureCoord;
uv.x *= renderTexture.width;
uv.y *= renderTexture.height;
RenderTexture.active = renderTexture;
GL.PushMatrix();
GL.LoadPixelMatrix(0, renderTexture.width, 0, renderTexture.height);
GL.Begin(GL.TRIANGLES);
GL.Color(Color.black);
GL.Vertex3(uv.x, uv.y, 0);
GL.Vertex3(uv.x + 10, uv.y, 0);
GL.Vertex3(uv.x, uv.y + 10, 0);
GL.End();
GL.PopMatrix();
RenderTexture.active = null;
}
}
}
```
这样,就可以在Pico中实现在黑板上写字了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)