forked from SEAME-pt/SEAME-Course-24-25
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
099bb22
commit 47390d5
Showing
5 changed files
with
146 additions
and
0 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,25 @@ | ||
cc_library( | ||
name = "lib", | ||
srcs = [ | ||
"gpio_sdl.cpp", | ||
], | ||
hdrs = [ | ||
"gpio_sdl.h", | ||
], | ||
features = ["warnings_critical_code_gcc"], # Suppress warnings from pigpio.h | ||
linkopts = [ | ||
"-L/lib/x86_64-linux-gnu/include", | ||
"-lpigpio", | ||
"-lrt", | ||
"-lSDL2", | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = "bin", | ||
srcs = ["main.cpp"], | ||
features = ["warnings_critical_code_gcc"], | ||
deps = [ | ||
":lib", | ||
], | ||
) |
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,13 @@ | ||
# How to Run | ||
|
||
Run the following command to compile for x86: | ||
|
||
```bash | ||
bazel build //examples/gpio_sdl:bin | ||
``` | ||
|
||
Run the following command to compile for aarch64: | ||
|
||
```bash | ||
bazel build //examples/gpio_sdl:bin --platforms=//bazel/platforms:aarch64_linux | ||
``` |
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,44 @@ | ||
#include "gpio_sdl.h" | ||
#include <pigpio.h> | ||
#include <iostream> | ||
|
||
bool initGPIO() { | ||
if (gpioInitialise() < 0) { | ||
std::cerr << "Failed to initialize pigpio." << std::endl; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int readButtonState(int gpioPin) { | ||
return gpioRead(static_cast<unsigned int>(gpioPin)); | ||
} | ||
|
||
bool initSDL(SDL_Window*& window, SDL_Renderer*& renderer) { | ||
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | ||
std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl; | ||
return false; | ||
} | ||
window = SDL_CreateWindow("GPIO + SDL2 Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); | ||
if (!window) { | ||
std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl; | ||
return false; | ||
} | ||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | ||
if (!renderer) { | ||
std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << std::endl; | ||
SDL_DestroyWindow(window); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void cleanupSDL(SDL_Window* window, SDL_Renderer* renderer) { | ||
SDL_DestroyRenderer(renderer); | ||
SDL_DestroyWindow(window); | ||
SDL_Quit(); | ||
} | ||
|
||
void cleanupGPIO() { | ||
gpioTerminate(); | ||
} |
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,12 @@ | ||
#ifndef GPIO_SDL_H | ||
#define GPIO_SDL_H | ||
|
||
#include <SDL2/SDL.h> | ||
|
||
bool initGPIO(); | ||
int readButtonState(int gpioPin); | ||
bool initSDL(SDL_Window*& window, SDL_Renderer*& renderer); | ||
void cleanupSDL(SDL_Window* window, SDL_Renderer* renderer); | ||
void cleanupGPIO(); | ||
|
||
#endif // GPIO_SDL_H |
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,52 @@ | ||
#include <iostream> | ||
|
||
#include <pigpio.h> | ||
#include "gpio_sdl.h" | ||
|
||
#define GPIO_BUTTON 17 | ||
|
||
int main() { | ||
if (!initGPIO()) { | ||
return -1; | ||
} | ||
gpioSetMode(GPIO_BUTTON, PI_INPUT); | ||
gpioSetPullUpDown(GPIO_BUTTON, PI_PUD_UP); | ||
|
||
SDL_Window* window = nullptr; | ||
SDL_Renderer* renderer = nullptr; | ||
if (!initSDL(window, renderer)) { | ||
cleanupGPIO(); | ||
return -1; | ||
} | ||
|
||
bool running = true; | ||
while (running) { | ||
SDL_Event event; | ||
while (SDL_PollEvent(&event)) { | ||
if (event.type == SDL_QUIT) { | ||
running = false; | ||
} | ||
} | ||
|
||
int buttonState = readButtonState(GPIO_BUTTON); | ||
|
||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
SDL_RenderClear(renderer); | ||
|
||
if (buttonState == 0) { | ||
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); | ||
} else { | ||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); | ||
} | ||
SDL_Rect rect = {220, 140, 200, 200}; | ||
SDL_RenderFillRect(renderer, &rect); | ||
|
||
SDL_RenderPresent(renderer); | ||
|
||
SDL_Delay(16); | ||
} | ||
|
||
cleanupSDL(window, renderer); | ||
cleanupGPIO(); | ||
return 0; | ||
} |