Created
July 11, 2024 04:23
-
-
Save jamesy0ung/a39b6bd455f9b18d9a1f2b8aff5b1cab to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <linux/i2c-dev.h> | |
#include <sys/ioctl.h> | |
#include <ncurses.h> | |
#define NUNCHUCK_ADDR 0x52 // Wii Nunchuck I2C address | |
// Function prototypes | |
void nunchuck_init(int file); | |
void nunchuck_read(int file, unsigned char *data); | |
void print_data(unsigned char *data, int row); | |
int main() { | |
const char *device = "/dev/i2c-1"; | |
int file; | |
if ((file = open(device, O_RDWR)) < 0) { | |
perror("Failed to open the I2C device"); | |
return 1; | |
} | |
if (ioctl(file, I2C_SLAVE, NUNCHUCK_ADDR) < 0) { | |
perror("Failed to acquire bus access and/or talk to slave"); | |
return 1; | |
} | |
// Initialize Nunchuck | |
nunchuck_init(file); | |
// Initialize ncurses | |
initscr(); // Start ncurses | |
cbreak(); // Disable line buffering | |
noecho(); // Don't echo key presses | |
timeout(0); // Non-blocking getch | |
keypad(stdscr, TRUE); // Enable keypad input | |
curs_set(0); // Hide cursor | |
int row = 0; // Track current row for printing data | |
// Print initial UI headers | |
printw("Wii Nunchuck Data:\n\n"); | |
printw(" Z Button | C Button | Joystick X | Joystick Y | Accel X | Accel Y | Accel Z\n"); | |
printw("------------------------------------------------------------------------------\n"); | |
refresh(); // Refresh the screen | |
unsigned char data[6]; | |
while (1) { | |
// Read data from Nunchuck | |
nunchuck_read(file, data); | |
// Print data in UI format | |
print_data(data, row); | |
// Move to the next row | |
row++; | |
// Check if we've reached the end of the screen | |
if (row >= LINES - 3) { | |
// Clear screen and reset row count | |
clear(); | |
row = 0; | |
// Print headers again | |
printw("Wii Nunchuck Data:\n\n"); | |
printw(" Z Button | C Button | Joystick X | Joystick Y | Accel X | Accel Y | Accel Z\n"); | |
printw("------------------------------------------------------------------------------\n"); | |
} | |
refresh(); // Refresh the screen | |
usleep(100000); // Sleep for 100ms | |
} | |
// End ncurses | |
endwin(); | |
close(file); | |
return 0; | |
} | |
void nunchuck_init(int file) { | |
unsigned char buf[2]; | |
buf[0] = 0xF0; | |
buf[1] = 0x55; | |
if (write(file, buf, 2) != 2) { | |
perror("Failed to initialize Nunchuck"); | |
exit(1); | |
} | |
usleep(5000); | |
buf[0] = 0xFB; | |
buf[1] = 0x00; | |
if (write(file, buf, 2) != 2) { | |
perror("Failed to initialize Nunchuck"); | |
exit(1); | |
} | |
} | |
void nunchuck_read(int file, unsigned char *data) { | |
if (write(file, NULL, 0) != 0) { | |
perror("Failed to request data from Nunchuck"); | |
exit(1); | |
} | |
usleep(5000); | |
if (read(file, data, 6) != 6) { | |
perror("Failed to read data from Nunchuck"); | |
exit(1); | |
} | |
} | |
void print_data(unsigned char *data, int row) { | |
// Extract button states | |
int z_btn = !(data[5] & 0x01); | |
int c_btn = !(data[5] & 0x02); | |
// Move cursor to the specified row and column | |
move(row + 4, 0); | |
// Print data in UI format | |
printw(" %s | %s | %3d | %3d | %3d | %3d | %3d\n", | |
z_btn ? "ON " : "OFF", c_btn ? "ON " : "OFF", data[0], data[1], data[2], data[3], data[4]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment