My Project 1
Yet another Wii Balance Board Driver
Functions
Extended Data Processing

Functions for interpreting and displaying Wii Balance Board data. More...

Functions

void process_calibration_data (int *bytes_read, unsigned char *buffer, WiiBalanceBoard *board)
 Processes the calibration data from the Wii Balance Board. More...
 
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. More...
 
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. More...
 
void print_calibration_data (const WiiBalanceBoard *board)
 Displays the stored calibration data. More...
 

Detailed Description

Functions for interpreting and displaying Wii Balance Board data.

These functions provide additional capabilities for interpreting and converting raw data from the Wii Balance Board into a human-readable format. While not essential for the primary operation, they enable a deeper understanding of data processing and assist in debugging.

Currently, these functions accept variables in some cases, though the goal is to refactor them to exclusively use pointers where possible to streamline memory management and improve efficiency. Additionally, they would ideally be organized in a separate file, but due to challenges in managing memory access and avoiding segmentation faults, they remain integrated within this file for now.

Note
To activate these extended features, compile with the YAWIIBB_EXTENDED flag.
gcc -DYAWIIBB_EXTENDED -Wall -o YAWiiBBD YAWiiBBD.c YAWiiBBessentials.c -lbluetooth

Function Documentation

◆ bytes_to_int_big_endian()

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.

Interprets two bytes in the given buffer starting from the provided position as a 16-bit integer in Big-Endian format (most significant byte first). The function returns this integer so that it can be output in decimal form.

Parameters
bufferPointer to the buffer that contains the bytes.
positionThe starting position of the most significant byte in the buffer.
Returns
uint16_t The Big-Endian number interpreted as a decimal integer.

◆ calc_mass()

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.

This function uses calibration data from the Wii Balance Board to calculate a weight in grams based on the given raw data. The weight is interpolated within four calibration ranges or, if the value exceeds the highest calibration range, is linearly extrapolated.

Parameters
boardA constant pointer to the Wii Balance Board structure that contains the calibration data.
rawThe raw data value representing the weight, measured from one of the four sensor positions (Top-Right, Bottom-Right, etc.).
posThe position of the sensor (0 to 3) for the calibration data of the raw value. Must be within the range [0, 3].
Returns
The calculated weight in grams as uint16_t.
Note
The function can interpolate weights up to 34 kg with the given calibration values. For values above this range, the weight is linearly extrapolated.

The calculation uses four cases based on the raw data:

  • If the raw value is less than the calibration for 0 kg, 0 g is returned.
  • If the raw value is between the calibrations for 0 kg and 17 kg, linear interpolation occurs between these points.
  • If the raw value is between the calibrations for 17 kg and 34 kg, interpolation occurs within this range.
  • If the raw value is greater than or equal to the calibration for 34 kg, linear extrapolation is performed based on the last range.

◆ print_calibration_data()

void print_calibration_data ( const WiiBalanceBoard board)

Displays the stored calibration data.

This function is solely for debugging purposes and is no longer used. If one wants to know whether the calibration data has been stored, they can implement it.

◆ process_calibration_data()

void process_calibration_data ( int *  bytes_read,
unsigned char *  buffer,
WiiBalanceBoard board 
)

Processes the calibration data from the Wii Balance Board.

This function analyzes a data buffer with calibration information and extracts this into the calibration array of the passed WiiBalanceBoard instance. The calibration data are 16-bit values stored in a big-endian format and consist of 4 pairs for each of the four corners (topright, topleft, bottomright, bottomleft).

Parameters
bytes_readNumber of bytes actually read in the buffer.
bufferPointer to a buffer containing the received bytes. The expected structure is:
  • Bytes 7-14: Calibration set 0 (4 pairs, 8 bytes)
  • Bytes 15-22: Calibration set 1 (4 pairs, 8 bytes)
  • If Byte 15 == 0x00:
    • Bytes 7-14: Calibration set 1
    • Bytes 15-22: Calibration set 2
boardPointer to the WiiBalanceBoard instance where calibration data will be stored.
Note
This function assumes that the buffer contains at least 23 bytes in order to process the expected calibration data.

The operation of the function is as follows:

  1. Check if the second calibration packet has been received (Byte 15 == 0x00).
  2. If the second packet is not present:
    • Bytes 7-14 are stored in calibration[0] (Calibration set 0).
    • Bytes 15-22 are stored in calibration[1] (Calibration set 1).
  3. If the second packet is present:
    • Bytes 7-14 are stored in calibration[2] (Calibration set 2).

Example:

  • Received data as an example:
    • buffer[7] = 0x01, buffer[8] = 0x02 (Calibration 0, Pair 0) In this case, the function would store the values in board->calibration[0][0]
    • buffer[15] = 0x00 (indicates that this is packet 2) In this case, the function would only read bytes 7-8,[..],13-14 and store them in board->calibration[2][i]