Skip to content

Commit

Permalink
examples: add sdl2 and pigpio
Browse files Browse the repository at this point in the history
  • Loading branch information
danctorres committed Jan 24, 2025
1 parent 099bb22 commit 47390d5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/gpio_sdl/BUILD
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",
],
)
13 changes: 13 additions & 0 deletions examples/gpio_sdl/README
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
```
44 changes: 44 additions & 0 deletions examples/gpio_sdl/gpio_sdl.cpp
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();
}
12 changes: 12 additions & 0 deletions examples/gpio_sdl/gpio_sdl.h
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
52 changes: 52 additions & 0 deletions examples/gpio_sdl/main.cpp
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;
}

0 comments on commit 47390d5

Please sign in to comment.