-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from XQuestCode/develop
Added raspberry Pi GPIO functionalites into splashkit.
- Loading branch information
Showing
32 changed files
with
6,347 additions
and
5,041 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// gpio_driver.cpp | ||
// This file is part of the SplashKit Core Library. | ||
// Copyright (©) 2024 Aditya Parmar. All Rights Reserved. | ||
|
||
#include "gpio_driver.h" | ||
|
||
#include <string> | ||
#include <iostream> | ||
#include <cstdlib> // Add this line to include the necessary header for the exit() function | ||
|
||
#ifdef RASPBERRY_PI | ||
#include "pigpiod_if2.h" | ||
|
||
|
||
using namespace std; | ||
// Use https://abyz.me.uk/rpi/pigpio/pdif2.html for reference | ||
namespace splashkit_lib | ||
{ | ||
int pi; | ||
|
||
// Check if pigpio_init() has been called before any other GPIO functions | ||
bool check_pi() | ||
{ | ||
|
||
if (pi < 0) | ||
{ | ||
cout << "gpio_init() must be called before any other GPIO functions" << endl; | ||
return true; | ||
} | ||
else | ||
return false; | ||
} | ||
|
||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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(); | ||
int sk_gpio_read(int pin); | ||
void sk_gpio_set_mode(int pin, int mode); | ||
int sk_gpio_get_mode(int pin); | ||
void sk_gpio_set_pull_up_down(int pin, int pud); | ||
void sk_gpio_write(int pin, int value); | ||
void sk_set_pwm_range(int pin, int range); | ||
void sk_set_pwm_frequency(int pin, int frequency); | ||
void sk_set_pwm_dutycycle(int pin, int dutycycle); | ||
void sk_gpio_cleanup(); | ||
} | ||
#endif | ||
#endif /* defined(gpio_driver) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
// raspi_gpio.cpp | ||
// splashkit | ||
// Created by Aditya Parmar on 20/01/2024. | ||
// Copyright © 2024 XQuestCode. All rights reserved. | ||
#include "raspi_gpio.h" | ||
#include "gpio_driver.h" | ||
#include <iostream> | ||
using namespace std; | ||
|
||
namespace splashkit_lib | ||
{ | ||
// Each index points to PIN_1, PIN_2, PIN_3, etc. | ||
int BCMpinData[] = { | ||
-1, -1, 2, -1, 3, -2, 4, 14, -2, 15, 17, 18, 27, -2, 22, 23, -1, 24, 10, -2, 9, 25, 11, 8, -2, 7, 0, 1, 5, -2, -6, 12, 13, -2, 19, 16, 26, 20, -2, 21}; | ||
|
||
int boardToBCM(pins pin) | ||
{ | ||
if (pin >= PIN_1 && pin <= PIN_40) | ||
{ | ||
return BCMpinData[static_cast<int>(pin) - static_cast<int>(PIN_1)]; | ||
} | ||
cout << "Invalid board pin" << endl; | ||
return -1; | ||
} | ||
bool has_gpio() | ||
{ | ||
#ifdef RASPBERRY_PI | ||
return true; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
// Initialize GPIO resources | ||
void raspi_init() | ||
{ | ||
#ifdef RASPBERRY_PI | ||
sk_gpio_init(); | ||
#else | ||
cout << "GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
|
||
// Set the mode of the given pin | ||
void raspi_set_mode(pins pin, pin_modes mode) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant modify a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant modify a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
sk_gpio_set_mode(bcmPin, static_cast<int>(mode)); | ||
} | ||
#else | ||
cout << "Unable to set mode - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
pin_modes raspi_get_mode(pins pin) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant modify a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant modify a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
return static_cast<pin_modes>(sk_gpio_get_mode(bcmPin)); | ||
} | ||
return GPIO_DEFAULT_MODE; | ||
#else | ||
return GPIO_DEFAULT_MODE; | ||
cout << "Unable to get mode - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
|
||
// Write a value to the given pin | ||
void raspi_write(pins pin, pin_values value) | ||
{ | ||
#ifdef RASPBERRY_PIv | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant write a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant write a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
sk_gpio_write(bcmPin, static_cast<int>(value)); | ||
} | ||
#else | ||
cout << "Unable to write pin - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
|
||
// Read the value of the given pin | ||
pin_values raspi_read(pins pin) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Reading of PIN: " << pin << " would always be HIGH" << endl; | ||
return GPIO_HIGH; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
|
||
cout << "Reading of PIN: " << pin << " would always be LOW" << endl; | ||
return GPIO_LOW; | ||
} | ||
return static_cast<pin_values>(sk_gpio_read(bcmPin)); | ||
#else | ||
cout << "Unable to read pin - GPIO not supported on this platform" << endl; | ||
return GPIO_DEFAULT_VALUE; | ||
#endif | ||
} | ||
void raspi_set_pull_up_down(pins pin, pull_up_down pud) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant modify a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant modify a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
sk_gpio_set_pull_up_down(bcmPin, static_cast<int>(pud)); | ||
} | ||
#else | ||
cout << "Unable to set pull up/down - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
void raspi_set_pwm_range(pins pin, int range) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant modify a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant modify a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
sk_set_pwm_range(bcmPin, range); | ||
} | ||
#else | ||
cout << "Unable to set pwm range - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
void raspi_set_pwm_frequency(pins pin, int frequency) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant modify a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant modify a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
sk_set_pwm_frequency(bcmPin, frequency); | ||
} | ||
#else | ||
cout << "Unable to set pwm frequency - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
void raspi_set_pwm_dutycycle(pins pin, int dutycycle) | ||
{ | ||
#ifdef RASPBERRY_PI | ||
int bcmPin = boardToBCM(pin); | ||
if (bcmPin == -1) | ||
{ | ||
cout << "Cant modify a HIGH Pin" << endl; | ||
} | ||
else if (bcmPin == -2) | ||
{ | ||
cout << "Cant modify a Ground pin" << endl; | ||
} | ||
else | ||
{ | ||
sk_set_pwm_dutycycle(bcmPin, dutycycle); | ||
} | ||
#else | ||
cout << "Unable to set pwm dutycycle - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
|
||
// Cleanup GPIO resources | ||
void raspi_cleanup() | ||
{ | ||
#ifdef RASPBERRY_PI | ||
cout << "Cleaning GPIO pins" << endl; | ||
for (int i = 1; i <= 40; i++) | ||
{ | ||
int bcmPin = boardToBCM(static_cast<pins>(i)); | ||
if (bcmPin > 0) | ||
{ | ||
raspi_set_mode(static_cast<pins>(bcmPin), GPIO_INPUT); | ||
raspi_write(static_cast<pins>(bcmPin), GPIO_LOW); | ||
} | ||
} | ||
sk_gpio_cleanup(); | ||
#else | ||
cout << "Unable to set cleanup - GPIO not supported on this platform" << endl; | ||
#endif | ||
} | ||
} |
Oops, something went wrong.