Skip to content

Commit

Permalink
bugfixed nunchuk init, cleanup, nunchuck accelerometer
Browse files Browse the repository at this point in the history
  • Loading branch information
djtulan committed Feb 2, 2018
1 parent b7d74ea commit 614e431
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 44 deletions.
6 changes: 3 additions & 3 deletions controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ typedef enum {
*
* @return always 0
*/
uint8_t controller_init(void);
extern uint8_t controller_init(void);

/**
* @brief read current controller data
*
* @param [out] cd a struct of 6 bytes to store data of the controller
* @return always 0
*/
uint8_t controller_read(ContollerData *cd);
extern uint8_t controller_read(ContollerData *cd);

/**
* @brief get controller id
*
* @return ControllerID
*/
ControllerID get_id(void);
extern ControllerID get_id(void);

#endif
2 changes: 1 addition & 1 deletion driver_nes_classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
#include "driver.h"

/// \brief NES classic driver
Driver drv_nes_classic;
extern Driver drv_nes_classic;

#endif
83 changes: 69 additions & 14 deletions driver_nunchuk.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,88 @@
//=============================================================================
#include "enums.h"
#include "joystick.h"
#include "led.h"

#include "driver_nunchuk.h"

#define ACCEL_ZEROX 510
#define ACCEL_ZEROY 489
#define ACCEL_ZEROZ 525

static inline uint16_t nunchuk_accelx(const ContollerData *cd) {
return ((0x0000 | (cd->byte[2] << 2)) + ((cd->byte[5] & 0x0c) >> 2));
}

static inline uint16_t nunchuk_accely(const ContollerData *cd) {
return ((0x0000 | (cd->byte[3] << 2)) + ((cd->byte[5] & 0xc0) >> 4));
}

static inline uint16_t nunchuk_accelz(const ContollerData *cd) {
return ((0x0000 | (cd->byte[4] << 2)) + ((cd->byte[5] & 0xc0) >> 6));
}

static inline int nunchuk_caccelx(const ContollerData *cd) {
return (int)(nunchuk_accelx(cd) - ACCEL_ZEROX);
}

static inline int nunchuk_caccely(const ContollerData *cd) {
return (int)(nunchuk_accely(cd) - ACCEL_ZEROY);
}

static inline int nunchuk_caccelz(const ContollerData *cd) {
return (int)(nunchuk_accelz(cd) - ACCEL_ZEROZ);
}

static void get_joystick_state_nunchuk(const ContollerData *cd, Joystick *joystick) {

// see: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck
// Analog stick X returns data from around 35 (fully left) to 228(fully right),
// while analog stick Y returns from around 27 to 220. Center for both is around 128.

(*joystick) = 0;

// Analog Joystick X
uint8_t sx = cd->byte[0];
// ===================================
// LED is OFF (Joystick mode)
// ===================================
if (led_get_state() == LED_OFF) {

if (sx > 180) {
(*joystick) |= RIGHT;
} else if (sx < 76) {
(*joystick) |= LEFT;
}
// Analog Joystick X
uint8_t sx = cd->byte[0];

// Analog Joystick Y
uint8_t sy = cd->byte[1];
if (sx > 180) {
(*joystick) |= RIGHT;
} else if (sx < 76) {
(*joystick) |= LEFT;
}

if (sy > 180) {
(*joystick) |= UP;
} else if (sy < 76) {
(*joystick) |= DOWN;
// Analog Joystick Y
uint8_t sy = cd->byte[1];

if (sy > 180) {
(*joystick) |= UP;
} else if (sy < 76) {
(*joystick) |= DOWN;
}

// ===================================
// LED is ON (Accelerometer mode)
// ===================================
} else {
int x = nunchuk_caccelx(cd);
int y = nunchuk_caccely(cd);

if (x < -128) {
(*joystick) |= LEFT;

} else if (x > 128) {
(*joystick) |= RIGHT;
}

if (y < -128) {
(*joystick) |= UP;

} else if (y > 128) {
(*joystick) |= DOWN;
}
}

// Z Button
Expand Down
2 changes: 1 addition & 1 deletion driver_nunchuk.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
#include "driver.h"

/// \brief nunchuk driver
Driver drv_nunchuk;
extern Driver drv_nunchuk;

#endif
2 changes: 1 addition & 1 deletion driver_wii_classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
#include "driver.h"

/// \brief wii classic driver
Driver drv_wii_classic;
extern Driver drv_wii_classic;

#endif
4 changes: 2 additions & 2 deletions joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef uint8_t Joystick;
* All joystick ddr bits are set to zero (Tri-state)
* Should be default anyway
*/
void joystick_init(void);
extern void joystick_init(void);

/**
* @brief Update Joystick state
Expand All @@ -53,6 +53,6 @@ void joystick_init(void);
*
* The bit (see Joystick_State) are set inside the respective parameter
*/
void joystick_update(Joystick port_a, Joystick port_b);
extern void joystick_update(Joystick port_a, Joystick port_b);

#endif
12 changes: 10 additions & 2 deletions led.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@

#include "led.h"

static LED_State led_state = LED_OFF;

void led_init(void) {
BIT_SET(DDR_LED, BIT_LED); // enable output
BIT_CLEAR(PORT_LED, BIT_LED); // set to 0 => LED OFF
}

void led_switch(uint8_t on) {
if (on) {
void led_switch(LED_State state) {
led_state = state;

if (state) {
BIT_SET(PORT_LED, BIT_LED);
} else {
BIT_CLEAR(PORT_LED, BIT_LED);
}
}

LED_State led_get_state(void) {
return (led_state);
}
26 changes: 24 additions & 2 deletions led.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,30 @@
#ifndef _LED_H_
#define _LED_H_

void led_init(void);
#include <inttypes.h>

void led_switch(uint8_t on);
typedef enum {
LED_OFF,
LED_ON
} LED_State;

/**
* @brief init LED
*
*/
extern void led_init(void);

/**
* @brief switch LED
*
* @param state LED_ON / LED_OFF
*/
extern void led_switch(LED_State state);

/**
* @brief init LED
* @return state of led
*/
extern LED_State led_get_state(void);

#endif
27 changes: 14 additions & 13 deletions nunchuk64.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ void init(void) {
// init modules
// ===================================
led_init(); // init led output
// button_init(); // init button input
i2c_init(); // init i2c routines
button_init(); // init button input
init_selector(); // init i2c bus selector
i2c_init(); // init i2c routines
joystick_init(); // init joystick outputs
paddle_init(); // init paddle outputs

led_switch(1); // diagnose
led_switch(LED_ON); // diagnose (in init)

_delay_ms(1);

Expand All @@ -94,21 +94,25 @@ void init(void) {
// ===================================
// enable interrupts
// ===================================
// sei();
sei();

// ===================================
// start paddle routines
// ===================================
paddle_start();

button_init(); // init button input

led_switch(0); // diagnose
led_switch(LED_OFF); // diagnose (init done)
}

int main(void) {
// ===================================
// init everything
// ===================================
init();

// ===================================
// get the correct driver
// ===================================
Driver *driver[NUMBER_PORTS] = {NULL, NULL};

ContollerData cd[NUMBER_PORTS]; // controller data
Expand All @@ -121,8 +125,6 @@ int main(void) {
driver[p] = GetDriver(get_id());
}

uint8_t led_on = 0;

#ifdef DEBUG
uint8_t toggle = 0;
#endif
Expand All @@ -134,12 +136,11 @@ int main(void) {
button_debounce();

if (button_get()) {
led_on = ~led_on;

if (led_on) {
led_switch(1);
if (led_get_state() == LED_OFF) {
led_switch(LED_ON);
} else {
led_switch(0);
led_switch(LED_OFF);
}
}

Expand Down
6 changes: 3 additions & 3 deletions paddle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
/**
* @brief init paddle-related IOs and interrupts
*/
void paddle_init(void);
extern void paddle_init(void);

/**
* @brief state paddle hardware and interrupt routines
*/
void paddle_start(void);
extern void paddle_start(void);

/**
* @brief update paddle state
*/
void paddle_update(Joystick port_a, Joystick port_b);
extern void paddle_update(Joystick port_a, Joystick port_b);

#endif
4 changes: 2 additions & 2 deletions selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
* @brief init select outputs for i2c select
*
*/
void init_selector(void);
extern void init_selector(void);

/**
* @brief select i2c device
*
* @param port PORT_A or PORT_B
*/
void switch_selector(Port port);
extern void switch_selector(Port port);

#endif

0 comments on commit 614e431

Please sign in to comment.