My Project 1
Yet another Wii Balance Board Driver
YAWiiBBessentials.h
Go to the documentation of this file.
1#ifndef YAWIIBBESSENTIALS_H
2#define YAWIIBBESSENTIALS_H
3//#define YAWIIBB_EXTENDED
4
5
62#include <stdio.h>
63#include <stdlib.h>
64#include <unistd.h>
65#include <string.h>
66#include <stdbool.h>
67#include <sys/socket.h>
68#include <bluetooth/bluetooth.h>
69#include <bluetooth/l2cap.h>
70#include <bluetooth/hci.h>
71#include <bluetooth/hci_lib.h>
72#include <pthread.h>
73#include <ctype.h>
74
75#define WII_BALANCE_BOARD_ADDR "00:23:CC:43:DC:C2"
76#define BUFFER_SIZE 24
85typedef enum {
87 #ifdef YAWIIBB_EXTENDED
90 VERBOSE
91 #endif //YAWIIBB_EXTENDED
93
94extern const LogLevel debug_level;
95
102extern unsigned char buffer[BUFFER_SIZE];
103
157extern const unsigned char status_command[];
158extern const unsigned char activate_command[];
159extern const unsigned char calibration_command[];
160extern const unsigned char led_on_command[];
161extern const unsigned char data_dump_command[];
174typedef struct {
175 char mac[19];
181 bool led;
184 #ifdef YAWIIBB_EXTENDED
185 uint16_t calibration[3][4];
186 #endif //YAWIIBB_EXTENDED
188
223void print_info(const LogLevel* is_debug_level, const char* message, const unsigned char* buffer, int length, const WiiBalanceBoard* board);
224
238
249void send_command(int sock, const unsigned char* command, int length);
250
262int connect_l2cap(const char* bdaddr_str, uint16_t psm);
263
264
276void process_received_data(int bytes_read, unsigned char* buffer, WiiBalanceBoard* board);
277
289void* threadFunction(void* arg);
290
301void createThread(WiiBalanceBoard* board, pthread_t* threadId);
302
318int is_valid_mac(int argc, char *argv[]);
347void handle_status(WiiBalanceBoard* board);
348
358
367void handle_led_on(WiiBalanceBoard* board);
368
378
390#ifdef YAWIIBB_EXTENDED
452void process_calibration_data(int* bytes_read, unsigned char* buffer, WiiBalanceBoard* board);
453
454
466uint16_t bytes_to_int_big_endian(const unsigned char *buffer, size_t position, int* max);
467
498uint16_t calc_mass(const WiiBalanceBoard* board, uint16_t raw, int pos);
499
506void print_calibration_data(const WiiBalanceBoard* board);
508#endif //YAWIIBB_EXTENDED
509#endif // YAWIIBBESSENTIALS_H
#define BUFFER_SIZE
Definition: YAWiiBBessentials.h:76
const LogLevel debug_level
Default logging level for output verbosity, dependent on compilation mode.
Definition: YAWiiBBD.c:64
LogLevel
Specifies the level of detail for logging output.
Definition: YAWiiBBessentials.h:85
@ DEBUG
Definition: YAWiiBBessentials.h:89
@ RAW
Definition: YAWiiBBessentials.h:86
@ VERBOSE
Definition: YAWiiBBessentials.h:90
@ DECODE
Definition: YAWiiBBessentials.h:88
unsigned char buffer[BUFFER_SIZE]
Buffer for storing responses from the Wii Balance Board.
Definition: YAWiiBBessentials.c:14
const unsigned char status_command[]
Definition: YAWiiBBessentials.c:9
const unsigned char calibration_command[]
Definition: YAWiiBBessentials.c:11
const unsigned char led_on_command[]
Definition: YAWiiBBessentials.c:12
const unsigned char data_dump_command[]
Definition: YAWiiBBessentials.c:13
const unsigned char activate_command[]
Definition: YAWiiBBessentials.c:10
void handle_activation(WiiBalanceBoard *board)
Processes sending an activation command to the Wii Balance Board.
Definition: YAWiiBBessentials.c:177
void handle_led_on(WiiBalanceBoard *board)
Processes sending an LED on command to the Wii Balance Board.
Definition: YAWiiBBessentials.c:171
void handle_data_dump(WiiBalanceBoard *board)
Processes sending a data dump command (continous report of readings) to the Wii Balance Board.
Definition: YAWiiBBessentials.c:183
void handle_calibration(WiiBalanceBoard *board)
Processes sending a calibration command to the Wii Balance Board.
Definition: YAWiiBBessentials.c:165
void handle_status(WiiBalanceBoard *board)
Processes sending a status command to the Wii Balance Board.
Definition: YAWiiBBessentials.c:159
void print_info(const LogLevel *is_debug_level, const char *message, const unsigned char *buffer, int length, const WiiBalanceBoard *board)
Logs messages with different verbosity levels, providing diagnostics and raw data output.
Definition: YAWiiBBessentials.c:17
void send_command(int sock, const unsigned char *command, int length)
Sends a command to the Wii Balance Board over the control socket.
Definition: YAWiiBBessentials.c:131
void createThread(WiiBalanceBoard *board, pthread_t *threadId)
Creates a new thread and starts the threadFunction to monitor user control.
Definition: YAWiiBBessentials.c:219
int connect_l2cap(const char *bdaddr_str, uint16_t psm)
Establishes a L2CAP connection with the Wii Balance Board.
Definition: YAWiiBBessentials.c:138
void process_received_data(int bytes_read, unsigned char *buffer, WiiBalanceBoard *board)
Processes received data from the Wii Balance Board.
Definition: YAWiiBBessentials.c:189
void * threadFunction(void *arg)
Thread function for monitoring user input to control the Wii Balance Board.
Definition: YAWiiBBessentials.c:203
int is_valid_mac(int argc, char *argv[])
Validates a given MAC address for format and content.
Definition: YAWiiBBessentials.c:228
int find_wii_balance_board(WiiBalanceBoard *board)
Finds the Wii Balance Board by scanning nearby Bluetooth devices.
Definition: YAWiiBBessentials.c:83
uint16_t calc_mass(const WiiBalanceBoard *board, uint16_t raw, int pos)
Calculates the weight in grams from the raw data of the Wii Balance Board.
Definition: YAWiiBBessentials.c:302
void print_calibration_data(const WiiBalanceBoard *board)
Displays the stored calibration data.
Definition: YAWiiBBessentials.c:331
void process_calibration_data(int *bytes_read, unsigned char *buffer, WiiBalanceBoard *board)
Processes the calibration data from the Wii Balance Board.
Definition: YAWiiBBessentials.c:281
uint16_t bytes_to_int_big_endian(const unsigned char *buffer, size_t position, int *max)
Converts two consecutive bytes in the buffer from Big-Endian representation to a decimal integer.
Definition: YAWiiBBessentials.c:264
Represents the Wii Balance Board connection and status.
Definition: YAWiiBBessentials.h:174
bool needDumpStart
Definition: YAWiiBBessentials.h:182
bool needStatus
Definition: YAWiiBBessentials.h:178
int receive_sock
Definition: YAWiiBBessentials.h:177
bool needCalibration
Definition: YAWiiBBessentials.h:180
bool is_running
Definition: YAWiiBBessentials.h:183
int control_sock
Definition: YAWiiBBessentials.h:176
bool led
Definition: YAWiiBBessentials.h:181
bool needActivation
Definition: YAWiiBBessentials.h:179