Last active
November 26, 2023 11:37
-
-
Save SimedruF/902ce55249f1a4eea85d181cb953e1c6 to your computer and use it in GitHub Desktop.
Raspberry Pi 4 SPI data read
This file contains hidden or 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
/* | |
Florin Simedru | |
Complete project details at https://blog.automatic-house.ro | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
#include <wiringPiSPI.h> | |
#include <wiringPi.h> | |
#include <iostream> | |
#include <ncurses.h> | |
#include <cstdlib> | |
// Funcție pentru tratarea semnalului de întrerupere | |
void handleInterrupt(int signal) { | |
endwin(); // Termină modul curses înainte de închiderea programului | |
std::cout << "Program întrerupt de la semnalul " << signal << ". Închidere..." << std::endl; | |
exit(0); | |
} | |
int main() { | |
// Deschide conexiunea SPI | |
int channel = 0; // Poate fi 0 sau 1, în funcție de canalul SPI folosit | |
int speed = 1000000; // Viteza de transfer în Hz | |
int spi = wiringPiSPISetup(channel, speed); | |
if (spi == -1) { | |
std::cerr << "Eroare la deschiderea conexiunii SPI. Verificați conexiunile hardware." << std::endl; | |
return 1; | |
} | |
// Buffer pentru date | |
unsigned char data[2]; | |
// Inițializare mod curses | |
initscr(); | |
timeout(0); // Timpul maxim de așteptare pentru citirea unei taste | |
// Configurare pentru tratarea semnalelor de întrerupere | |
//signal(SIGINT, handleInterrupt); | |
while (true) { | |
// Citeste datele de pe SPI | |
wiringPiSPIDataRW(channel, data, 2); | |
// Procesează datele aici | |
// data[0] conține primul byte, data[1] conține al doilea byte | |
// Modificați această parte în funcție de formatul datelor primite | |
// Exemplu de afișare a datelor citite | |
if((data[0] != 0) && (data[0]!=0)) | |
{ | |
//std::cout << "Date citite: " << static_cast<int>(data[0]) << " " << static_cast<int>(data[1]) << std::endl; | |
// Exemplu de afișare a datelor citite | |
printw("Date citite: %d %d\n", static_cast<int>(data[0]), static_cast<int>(data[1])); | |
refresh(); // Actualizează ecranul | |
} | |
// Citește o tastă de la tastatură | |
int ch = getch(); | |
//std::cout << "Tasta citita: " << static_cast<int>(ch) << " " << std::endl; | |
// Verifică dacă tasta 'q' a fost apăsată | |
if (ch == 'q') { | |
//std::cout << "Tasta citita: " << static_cast<int>(ch) << "Întrerupere program. " << std::endl; | |
printw("Tasta 'q' apăsată. Întrerupere program.\n"); | |
refresh(); | |
break; | |
} | |
delay(100); | |
} | |
// Termină modul curses înainte de închiderea programului | |
endwin(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment