pleasedontcode

# GPIO Management rev_05

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