pleasedontcode

# GPIO Management rev_04

Feb 19th, 2026
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # GPIO Management
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-02-20 04:36:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Initialize ESP32 DevKit V1 with configurable GPIO */
  21.     /* pins for digital I/O operations, ensuring proper */
  22.     /* pin initialization and state management during */
  23.     /* system startup and runtime. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. // Include the GPIO configuration header file for type definitions and function declarations
  30. #include "GPIOConfig.h"
  31.  
  32. // System Requirements:
  33. // System Requirement 1: "Initialize ESP32 DevKit V1 with configurable GPIO pins for digital I/O operations,
  34. // ensuring proper pin initialization and state management during system startup and runtime."
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. /**
  41.  * setup()
  42.  * Initializes the ESP32 DevKit V1 with GPIO manager and configures pins.
  43.  * This function runs once at system startup and performs:
  44.  * 1. GPIO Manager initialization
  45.  * 2. Configuration of required GPIO pins
  46.  * 3. Verification of pin configuration
  47.  */
  48. void setup(void)
  49. {
  50.     // Initialize Serial communication for debugging (optional)
  51.     Serial.begin(115200);
  52.     delay(100);
  53.    
  54.     // Initialize the GPIO Manager
  55.     // This sets up the internal GPIO management structure and resets all pins to unconfigured state
  56.     if (GPIO_Manager_Init())
  57.     {
  58.         Serial.println("GPIO Manager initialized successfully");
  59.     }
  60.     else
  61.     {
  62.         Serial.println("ERROR: GPIO Manager initialization failed");
  63.     }
  64.    
  65.     // Example: Configure GPIO pin 2 as OUTPUT with initial state LOW
  66.     // GPIO pin 2 is commonly available on ESP32 DevKit V1
  67.     if (GPIO_Configure_Pin(2, APP_GPIO_MODE_OUTPUT, APP_GPIO_STATE_LOW))
  68.     {
  69.         Serial.println("GPIO pin 2 configured as OUTPUT");
  70.     }
  71.     else
  72.     {
  73.         Serial.println("ERROR: Failed to configure GPIO pin 2");
  74.     }
  75.    
  76.     // Example: Configure GPIO pin 4 as OUTPUT with initial state LOW
  77.     if (GPIO_Configure_Pin(4, APP_GPIO_MODE_OUTPUT, APP_GPIO_STATE_LOW))
  78.     {
  79.         Serial.println("GPIO pin 4 configured as OUTPUT");
  80.     }
  81.     else
  82.     {
  83.         Serial.println("ERROR: Failed to configure GPIO pin 4");
  84.     }
  85.    
  86.     // Example: Configure GPIO pin 5 as INPUT with no initial state
  87.     // Input pins don't have initial state but the parameter is required by the function
  88.     if (GPIO_Configure_Pin(5, APP_GPIO_MODE_INPUT, APP_GPIO_STATE_LOW))
  89.     {
  90.         Serial.println("GPIO pin 5 configured as INPUT");
  91.     }
  92.     else
  93.     {
  94.         Serial.println("ERROR: Failed to configure GPIO pin 5");
  95.     }
  96.    
  97.     // Example: Configure GPIO pin 18 as INPUT with internal PULLUP
  98.     if (GPIO_Configure_Pin(18, APP_GPIO_MODE_INPUT_PULLUP, APP_GPIO_STATE_LOW))
  99.     {
  100.         Serial.println("GPIO pin 18 configured as INPUT_PULLUP");
  101.     }
  102.     else
  103.     {
  104.         Serial.println("ERROR: Failed to configure GPIO pin 18");
  105.     }
  106.    
  107.     // Display total number of configured pins
  108.     uint8_t configured_count = GPIO_Get_Configured_Count();
  109.     Serial.print("Total configured GPIO pins: ");
  110.     Serial.println(configured_count);
  111.    
  112.     delay(500);
  113. }
  114.  
  115. /**
  116.  * loop()
  117.  * Main program loop that runs continuously after setup() completes.
  118.  * This function performs:
  119.  * 1. Runtime state management checks
  120.  * 2. GPIO state monitoring
  121.  * 3. GPIO control operations
  122.  */
  123. void loop(void)
  124. {
  125.     // Perform runtime state management check to validate all configured pins
  126.     // This checks that pin states are consistent between hardware and internal management
  127.     if (GPIO_Runtime_State_Check())
  128.     {
  129.         // All configured pins are in valid state
  130.     }
  131.     else
  132.     {
  133.         // At least one pin state is invalid
  134.         Serial.println("WARNING: GPIO runtime state check detected inconsistency");
  135.     }
  136.    
  137.     // Example: Read the state of GPIO pin 5 (configured as INPUT)
  138.     GPIOState_t pin5_state = GPIO_Read_State(5);
  139.     Serial.print("GPIO pin 5 state: ");
  140.     Serial.println((pin5_state == APP_GPIO_STATE_HIGH) ? "HIGH" : "LOW");
  141.    
  142.     // Example: Read the state of GPIO pin 18 (configured as INPUT_PULLUP)
  143.     GPIOState_t pin18_state = GPIO_Read_State(18);
  144.     Serial.print("GPIO pin 18 state: ");
  145.     Serial.println((pin18_state == APP_GPIO_STATE_HIGH) ? "HIGH" : "LOW");
  146.    
  147.     // Example: Toggle GPIO pin 2 (configured as OUTPUT) every loop iteration
  148.     GPIOState_t pin2_current_state = GPIO_Read_State(2);
  149.     GPIOState_t pin2_new_state = (pin2_current_state == APP_GPIO_STATE_HIGH) ? APP_GPIO_STATE_LOW : APP_GPIO_STATE_HIGH;
  150.     if (GPIO_Set_State(2, pin2_new_state))
  151.     {
  152.         Serial.print("GPIO pin 2 set to: ");
  153.         Serial.println((pin2_new_state == APP_GPIO_STATE_HIGH) ? "HIGH" : "LOW");
  154.     }
  155.     else
  156.     {
  157.         Serial.println("ERROR: Failed to set GPIO pin 2 state");
  158.     }
  159.    
  160.     // Example: Set GPIO pin 4 to HIGH
  161.     if (GPIO_Set_State(4, APP_GPIO_STATE_HIGH))
  162.     {
  163.         Serial.println("GPIO pin 4 set to HIGH");
  164.     }
  165.     else
  166.     {
  167.         Serial.println("ERROR: Failed to set GPIO pin 4");
  168.     }
  169.    
  170.     // Example: Verify pin configuration mode for pin 2
  171.     GPIOMode_t pin2_mode = GPIO_Get_Mode(2);
  172.     Serial.print("GPIO pin 2 mode: ");
  173.     switch (pin2_mode)
  174.     {
  175.         case APP_GPIO_MODE_INPUT:
  176.             Serial.println("INPUT");
  177.             break;
  178.         case APP_GPIO_MODE_OUTPUT:
  179.             Serial.println("OUTPUT");
  180.             break;
  181.         case APP_GPIO_MODE_INPUT_PULLUP:
  182.             Serial.println("INPUT_PULLUP");
  183.             break;
  184.         case APP_GPIO_MODE_INPUT_PULLDOWN:
  185.             Serial.println("INPUT_PULLDOWN");
  186.             break;
  187.         default:
  188.             Serial.println("UNKNOWN");
  189.             break;
  190.     }
  191.    
  192.     // Example: Check if GPIO pin 4 is configured
  193.     if (GPIO_Is_Configured(4))
  194.     {
  195.         Serial.println("GPIO pin 4 is configured");
  196.     }
  197.     else
  198.     {
  199.         Serial.println("GPIO pin 4 is NOT configured");
  200.     }
  201.    
  202.     // Delay to prevent rapid loop iterations and allow observable changes
  203.     delay(2000);
  204. }
  205.  
  206. /* END CODE */
  207.  
Advertisement
Add Comment
Please, Sign In to add comment