Skip to content

Commit

Permalink
Update GPIO functionality and fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
XQuestCode committed Jan 24, 2024
1 parent 7b675f4 commit c586b8f
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 192 deletions.
214 changes: 91 additions & 123 deletions coresdk/src/backend/gpio_driver.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/***********************************************
* XQuestCode || Aditya Parmar
* © 2024 Aditya Parmar. All Rights Reserved.
* This file is part of the SplashKit Core Library.
* Use https://abyz.me.uk/rpi/pigpio/pdif2.html for reference
***********************************************/
// gpio_driver.cpp
// This file is part of the SplashKit Core Library.
// Copyright (©) 2024 Aditya Parmar. All Rights Reserved.

#include "gpio_driver.h"

Expand All @@ -13,128 +10,99 @@

#ifdef RASPBERRY_PI
#include "pigpiod_if2.h"
#endif

using namespace std;

using namespace std;
// Use https://abyz.me.uk/rpi/pigpio/pdif2.html for reference
namespace splashkit_lib
{
int pi;
int pi;

// Check if pigpio_init() has been called before any other GPIO functions
void check_pi()
{
#ifdef RASPBERRY_PI
if (pi < 0)
// Check if pigpio_init() has been called before any other GPIO functions
bool check_pi()
{
cout << "pigpio_init() must be called before any other GPIO functions" << endl;
exit(1);

if (pi < 0)
{
cout << "gpio_init() must be called before any other GPIO functions" << endl;
return true;
}
else
return false;
}
#endif
}

// Initialize the GPIO library
int sk_gpio_init()
{
#ifdef RASPBERRY_PI
pi = pigpio_start(0, 0);
return pi;
#else
return -1;
#endif
}

// Read the value of a GPIO pin
int sk_gpio_read(int pin)
{
#ifdef RASPBERRY_PI
check_pi();
return gpio_read(pi, pin);
#else
cout << "gpio_read() called, but not on a Raspberry Pi" << endl;
return -1;
#endif
}

// Write a value to a GPIO pin
void sk_gpio_write(int pin, int value)
{
#ifdef RASPBERRY_PI
check_pi();
gpio_write(pi, pin, value);
#else
cout << "gpio_write() called, but not on a Raspberry Pi" << endl;
#endif
}

// Set the mode of a GPIO pin
void sk_gpio_set_mode(int pin, int mode)
{
#ifdef RASPBERRY_PI
check_pi();
set_mode(pi, pin, mode);
#else
cout << "gpio_set_mode() called, but not on a Raspberry Pi" << endl;
#endif
}
int sk_gpio_get_mode(int pin)
{
#ifdef RASPBERRY_PI
check_pi();
return get_mode(pi, pin);
#else
cout << "gpio_get_mode() called, but not on a Raspberry Pi" << endl;
return -1;
#endif
}
void sk_gpio_set_pull_up_down(int pin, int pud)
{
#ifdef RASPBERRY_PI
check_pi();
set_pull_up_down(pi, pin, pud);
#else

cout << "gpio_set_pull_up_down() called, but not on a Raspberry Pi" << endl;
#endif
}
void sk_set_pwm_range(int pin, int range)
{
#ifdef RASPBERRY_PI
check_pi();
set_PWM_range(pi, pin, range);
#else
cout << "set_PWM_range() called, but not on a Raspberry Pi" << endl;
#endif
}
void sk_set_pwm_frequency(int pin, int frequency)
{
#ifdef RASPBERRY_PI
check_pi();
set_PWM_frequency(pi, pin, frequency);
#else
cout << "set_PWM_frequency() called, but not on a Raspberry Pi" << endl;
#endif
}
void sk_set_pwm_dutycycle(int pin, int dutycycle)
{
#ifdef RASPBERRY_PI
check_pi();
set_PWM_dutycycle(pi, pin, dutycycle);
#else
cout << "set_PWM_dutycycle() called, but not on a Raspberry Pi" << endl;
#endif
}


// Cleanup the GPIO library
void sk_gpio_cleanup()
{
#ifdef RASPBERRY_PI
check_pi();

pigpio_stop(pi);
#else
cout << "pigpio_cleanup() called, but not on a Raspberry Pi" << endl;
#endif
}
}
// Initialize the GPIO library
int sk_gpio_init()
{

pi = pigpio_start(0, 0);
return pi;
}

// Read the value of a GPIO pin
int sk_gpio_read(int pin)
{

if (check_pi())
{
return gpio_read(pi, pin);
}
}

// Write a value to a GPIO pin
void sk_gpio_write(int pin, int value)
{

check_pi();
gpio_write(pi, pin, value);
}

// Set the mode of a GPIO pin
void sk_gpio_set_mode(int pin, int mode)
{

check_pi();
set_mode(pi, pin, mode);
}

int sk_gpio_get_mode(int pin)
{

check_pi();
return get_mode(pi, pin);
}
void sk_gpio_set_pull_up_down(int pin, int pud)
{

check_pi();
set_pull_up_down(pi, pin, pud);
}
void sk_set_pwm_range(int pin, int range)
{

check_pi();
set_PWM_range(pi, pin, range);
}
void sk_set_pwm_frequency(int pin, int frequency)
{

check_pi();
set_PWM_frequency(pi, pin, frequency);
}
void sk_set_pwm_dutycycle(int pin, int dutycycle)
{

check_pi();
set_PWM_dutycycle(pi, pin, dutycycle);
}

// Cleanup the GPIO library
void sk_gpio_cleanup()
{

check_pi();

pigpio_stop(pi);
}
}
#endif
12 changes: 6 additions & 6 deletions coresdk/src/backend/gpio_driver.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/***********************************************
* XQuestCode || Aditya Parmar
* © 2024 Aditya Parmar. All Rights Reserved.
* This file is part of the SplashKit Core Library.
* Use https://abyz.me.uk/rpi/pigpio/pdif2.html for reference
***********************************************/
// gpio_driver.h
// This file is part of the SplashKit Core Library.
// Copyright (©) 2024 Aditya Parmar. All Rights Reserved.

#ifndef SPLASHKIT_GPIO_H
#define SPLASHKIT_GPIO_H

#include <stdint.h> // Include the appropriate header file for stdint.h
#ifdef RASPBERRY_PI
namespace splashkit_lib
{
int sk_gpio_init();
Expand All @@ -21,4 +20,5 @@ namespace splashkit_lib
void sk_set_pwm_dutycycle(int pin, int dutycycle);
void sk_gpio_cleanup();
}
#endif
#endif /* defined(gpio_driver) */
Loading

0 comments on commit c586b8f

Please sign in to comment.