add button test at the board startup, part 3
This commit is contained in:
		
							parent
							
								
									0b8a55a9e2
								
							
						
					
					
						commit
						1eb0c2c318
					
				
							
								
								
									
										162
									
								
								Core/Src/main.c
									
									
									
									
									
								
							
							
						
						
									
										162
									
								
								Core/Src/main.c
									
									
									
									
									
								
							@ -68,6 +68,88 @@ static void MX_ADC1_Init(void);
 | 
				
			|||||||
/* Private user code ---------------------------------------------------------*/
 | 
					/* Private user code ---------------------------------------------------------*/
 | 
				
			||||||
/* USER CODE BEGIN 0 */
 | 
					/* USER CODE BEGIN 0 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void button_init_and_test(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						// letting the buttons be tested
 | 
				
			||||||
 | 
						display_write_data_seq("Fill any bar:");
 | 
				
			||||||
 | 
						DISPLAY_SET_CURSOR(1, 5);
 | 
				
			||||||
 | 
						display_write_data_seq("[-] [-----]");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						size_t pressed_elements;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						uint32_t sw_button_locations[5][2] = {
 | 
				
			||||||
 | 
								{GPIOC, GPIO_PIN_11},
 | 
				
			||||||
 | 
								{GPIOA, GPIO_PIN_15},
 | 
				
			||||||
 | 
								{GPIOC, GPIO_PIN_9},
 | 
				
			||||||
 | 
								{GPIOC, GPIO_PIN_6},
 | 
				
			||||||
 | 
								{GPIOC, GPIO_PIN_8}
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						do {
 | 
				
			||||||
 | 
							HAL_Delay(100);
 | 
				
			||||||
 | 
							pressed_elements = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							DISPLAY_SET_CURSOR(1, 6);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) {
 | 
				
			||||||
 | 
								pressed_elements |= 0x20;
 | 
				
			||||||
 | 
								display_write_data_byte('*');
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								display_write_data_byte('-');
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							DISPLAY_SET_CURSOR(1, 10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for (size_t i = 0; i < 5; i++) {
 | 
				
			||||||
 | 
								pressed_elements >>= 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								size_t input = !HAL_GPIO_ReadPin(sw_button_locations[i][0], sw_button_locations[i][1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (input) {
 | 
				
			||||||
 | 
									pressed_elements |= 0x20;
 | 
				
			||||||
 | 
									display_write_data_byte('*');
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									display_write_data_byte('-');
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} while (pressed_elements != 0x1 && pressed_elements != 0x3E);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// visual reaction to bar fill
 | 
				
			||||||
 | 
						GPIOD->ODR = 0x1000;
 | 
				
			||||||
 | 
						HAL_Delay(500);
 | 
				
			||||||
 | 
						GPIOD->ODR = 0x2000;
 | 
				
			||||||
 | 
						HAL_Delay(500);
 | 
				
			||||||
 | 
						GPIOD->ODR = 0x4000;
 | 
				
			||||||
 | 
						HAL_Delay(500);
 | 
				
			||||||
 | 
						GPIOD->ODR = 0x8000;
 | 
				
			||||||
 | 
						HAL_Delay(500);
 | 
				
			||||||
 | 
						GPIOD->ODR = 0x0000;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// waiting for control buttons to be released
 | 
				
			||||||
 | 
						while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) | !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8));
 | 
				
			||||||
 | 
						HAL_Delay(500);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// switch mode for user button
 | 
				
			||||||
 | 
						GPIO_InitTypeDef GPIO_InitStruct = {0};
 | 
				
			||||||
 | 
						GPIO_InitStruct.Pin = GPIO_PIN_0;
 | 
				
			||||||
 | 
						GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
 | 
				
			||||||
 | 
						GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
				
			||||||
 | 
						HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
 | 
				
			||||||
 | 
						HAL_NVIC_EnableIRQ(EXTI0_IRQn);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// switch mode for SW5 (alternative advancing method)
 | 
				
			||||||
 | 
						GPIO_InitStruct.Pin = GPIO_PIN_8;
 | 
				
			||||||
 | 
						GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
 | 
				
			||||||
 | 
						GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
				
			||||||
 | 
						HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
 | 
				
			||||||
 | 
						HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* USER CODE END 0 */
 | 
					/* USER CODE END 0 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@ -104,85 +186,9 @@ int main(void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  GPIOD->ODR = 0x1000;
 | 
					  GPIOD->ODR = 0x1000;
 | 
				
			||||||
  display_init();
 | 
					  display_init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  GPIOD->ODR = 0xF000;
 | 
					  GPIOD->ODR = 0xF000;
 | 
				
			||||||
 | 
					  button_init_and_test();
 | 
				
			||||||
  // init showcase
 | 
					 | 
				
			||||||
  display_write_data_seq("Fill any bar:");
 | 
					 | 
				
			||||||
  DISPLAY_SET_CURSOR(1, 5);
 | 
					 | 
				
			||||||
  display_write_data_seq("[-] [-----]");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  size_t pressed_elements;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  uint32_t sw_button_locations[5][2] = {
 | 
					 | 
				
			||||||
		  {GPIOC, GPIO_PIN_11},
 | 
					 | 
				
			||||||
		  {GPIOA, GPIO_PIN_15},
 | 
					 | 
				
			||||||
		  {GPIOC, GPIO_PIN_9},
 | 
					 | 
				
			||||||
		  {GPIOC, GPIO_PIN_6},
 | 
					 | 
				
			||||||
		  {GPIOC, GPIO_PIN_8}
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  do {
 | 
					 | 
				
			||||||
	  HAL_Delay(100);
 | 
					 | 
				
			||||||
	  pressed_elements = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	  DISPLAY_SET_CURSOR(1, 6);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	  if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) {
 | 
					 | 
				
			||||||
		  pressed_elements |= 0x20;
 | 
					 | 
				
			||||||
		  display_write_data_byte('*');
 | 
					 | 
				
			||||||
	  } else {
 | 
					 | 
				
			||||||
		  display_write_data_byte('-');
 | 
					 | 
				
			||||||
	  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	  DISPLAY_SET_CURSOR(1, 10);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	  for (size_t i = 0; i < 5; i++) {
 | 
					 | 
				
			||||||
		  pressed_elements >>= 1;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		  size_t input = !HAL_GPIO_ReadPin(sw_button_locations[i][0], sw_button_locations[i][1]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		  if (input) {
 | 
					 | 
				
			||||||
			  pressed_elements |= 0x20;
 | 
					 | 
				
			||||||
			  display_write_data_byte('*');
 | 
					 | 
				
			||||||
		  } else {
 | 
					 | 
				
			||||||
			  display_write_data_byte('-');
 | 
					 | 
				
			||||||
		  }
 | 
					 | 
				
			||||||
	  }
 | 
					 | 
				
			||||||
  } while (pressed_elements != 0x1 && pressed_elements != 0x3E);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  GPIOD->ODR = 0x1000;
 | 
					 | 
				
			||||||
  HAL_Delay(500);
 | 
					 | 
				
			||||||
  GPIOD->ODR = 0x2000;
 | 
					 | 
				
			||||||
  HAL_Delay(500);
 | 
					 | 
				
			||||||
  GPIOD->ODR = 0x4000;
 | 
					 | 
				
			||||||
  HAL_Delay(500);
 | 
					 | 
				
			||||||
  GPIOD->ODR = 0x8000;
 | 
					 | 
				
			||||||
  HAL_Delay(500);
 | 
					 | 
				
			||||||
  GPIOD->ODR = 0x0000;
 | 
					 | 
				
			||||||
  while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0));
 | 
					 | 
				
			||||||
  HAL_Delay(500);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // switch mode for user button
 | 
					 | 
				
			||||||
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 | 
					 | 
				
			||||||
  GPIO_InitStruct.Pin = GPIO_PIN_0;
 | 
					 | 
				
			||||||
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
 | 
					 | 
				
			||||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
					 | 
				
			||||||
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
 | 
					 | 
				
			||||||
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // switch mode for SW5 (alternative advancing method)
 | 
					 | 
				
			||||||
  GPIO_InitStruct.Pin = GPIO_PIN_8;
 | 
					 | 
				
			||||||
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
 | 
					 | 
				
			||||||
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
					 | 
				
			||||||
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
 | 
					 | 
				
			||||||
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* USER CODE END 2 */
 | 
					  /* USER CODE END 2 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user