#define DEBOUNCE_INTERVAL 20
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_SPI1_Init(void);
void MX_USB_HOST_Process(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  MX_USB_HOST_Init();
  /* USER CODE BEGIN 2 */

  int debounce_counter = 0;
  enum debounceState {LOCKED, RELEASED};
  enum debounceState debounce_state = RELEASED;
  GPIO_PinState JoyStateA = GPIO_PIN_RESET; // Initially set to an arbitrary state
  GPIO_PinState JoyStateD = GPIO_PIN_RESET;
  GPIO_PinState JoyStateC = GPIO_PIN_RESET;
  GPIO_PinState JoyStateCTR = GPIO_PIN_RESET;
  GPIO_PinState previous_JoyStateA = GPIO_PIN_SET; // Initially set to an arbitrary state
  GPIO_PinState previous_JoyStateD = GPIO_PIN_SET;
  GPIO_PinState previous_JoyStateC = GPIO_PIN_SET;
  GPIO_PinState previous_JoyStateCTR = GPIO_PIN_SET;

  typedef enum {
      STATE_ALL_LED_OFF,
      STATE_LEFT_LED_ON,
      STATE_RIGHT_LED_ON
  } State;

  typedef enum {
      INPUT_CENTER,
      INPUT_LEFT,
      INPUT_RIGHT,
      INPUT_DOWN
  } Input;

  State currentState = STATE_ALL_LED_OFF;
  State nextState;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

	  Input input;
	          if (HAL_GPIO_ReadPin(JOY_CTR_GPIO_Port, JOY_CTR_Pin) == GPIO_PIN_RESET)
	              input = INPUT_CENTER;
	          else if (HAL_GPIO_ReadPin(JOY_A_GPIO_Port, JOY_A_Pin) == GPIO_PIN_RESET)
	              input = INPUT_LEFT;
	          else if (HAL_GPIO_ReadPin(JOY_D_GPIO_Port, JOY_D_Pin) == GPIO_PIN_RESET)
	              input = INPUT_RIGHT;
	          else if (HAL_GPIO_ReadPin(JOY_C_GPIO_Port, JOY_C_Pin) == GPIO_PIN_RESET)
	              input = INPUT_DOWN;
	          else
	              continue;

	          switch(currentState){
	                 case STATE_ALL_LED_OFF:
	                    if (input == INPUT_LEFT)
	                        currentState = STATE_LEFT_LED_ON;
	                    else if (input == INPUT_RIGHT)
	                        currentState = STATE_RIGHT_LED_ON;
	                    else if (input == INPUT_DOWN)
	                        currentState = STATE_LEFT_LED_ON; // Turn on left LED if both off
	                    break;

	                case STATE_LEFT_LED_ON:
	                    if (input == INPUT_CENTER)
	                        currentState = STATE_ALL_LED_OFF;
	                    else if (input == INPUT_RIGHT || input == INPUT_DOWN)
	                        currentState = STATE_RIGHT_LED_ON;
	                    break;

	                case STATE_RIGHT_LED_ON:
	                    if (input == INPUT_CENTER)
	                        currentState = STATE_ALL_LED_OFF;
	                    else if (input == INPUT_LEFT || input == INPUT_DOWN)
	                        currentState = STATE_LEFT_LED_ON;
	                    break;

	                default:
	                    break;


	                    }

	                switch(currentState)
	                {
	                 case STATE_ALL_LED_OFF:
	                    HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
	                    HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
	                    break;

	                case STATE_LEFT_LED_ON:
	                    HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
	                    HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
	                    break;

	                case STATE_RIGHT_LED_ON:
	                    HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
	                    HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
	                    break;

	                default:
	                    break;
	                }

	                switch(debounce_state) {
	                      case RELEASED:

	                      JoyStateA = HAL_GPIO_ReadPin (JOY_A_GPIO_Port, JOY_A_Pin);
	                      JoyStateD = HAL_GPIO_ReadPin (JOY_D_GPIO_Port, JOY_D_Pin);
	                      JoyStateC = HAL_GPIO_ReadPin (JOY_C_GPIO_Port, JOY_C_Pin);
	                      JoyStateCTR = HAL_GPIO_ReadPin (JOY_CTR_GPIO_Port, JOY_CTR_Pin);
	                      if ((JoyStateA == GPIO_PIN_RESET && previous_JoyStateA == GPIO_PIN_SET) ||
	                          (JoyStateD == GPIO_PIN_RESET && previous_JoyStateD == GPIO_PIN_SET) ||
							  (JoyStateC == GPIO_PIN_RESET && previous_JoyStateC == GPIO_PIN_SET)||
	                          (JoyStateCTR == GPIO_PIN_RESET && previous_JoyStateCTR == GPIO_PIN_SET)) {
	                                     debounce_state = LOCKED;
	                      }
	                                  break;
	                              case LOCKED:
	                                  JoyStateA = GPIO_PIN_SET; // Manually reset JoyStateA when LOCKED
	                                  JoyStateD = GPIO_PIN_SET; // Manually reset JoyStateD when LOCKED
	                                  JoyStateC = GPIO_PIN_SET;
	                                  JoyStateCTR = GPIO_PIN_SET; // Manually reset JoyStateCTR when LOCKED
	                                  if ( debounce_counter > DEBOUNCE_INTERVAL) {
	                                      debounce_state = RELEASED; // RELEASE after time-out
	                                      debounce_counter = 0;
	                                  } else {
	                                      ++debounce_counter; // still LOCKED, increase counter
	                                  }
	                                  break;
	                          }
	                // If any JoyState changes, reset debounce_counter and update previous JoyStates
	                    if (JoyStateA != previous_JoyStateA || JoyStateD != previous_JoyStateD || JoyStateC != previous_JoyStateC || JoyStateCTR != previous_JoyStateCTR) {
	                        debounce_counter = 0; // Reset debounce counter when button state changes
	                        previous_JoyStateA = JoyStateA;
	                        previous_JoyStateD = JoyStateD;
	                        previous_JoyStateC = JoyStateC;
	                        previous_JoyStateCTR = JoyStateCTR;
	                    }

	                    // Update currentState if nextState is set
	                        if (nextState != currentState) {
	                            currentState = nextState;
	                            nextState = currentState; // Reset nextState
	                        }
	                              }
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();

    /* USER CODE BEGIN 3 */
  } 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}