55 lines
1.9 KiB
C
Raw Normal View History

#include "hid.h"
#include "i2c.h"
2016-05-30 01:54:09 +02:00
#include "timer.h"
#include "power.h"
u32 InputWait(u32 timeout_sec) {
2016-05-30 03:03:40 +02:00
static u64 delay = 0;
u32 pad_state_old = HID_STATE;
2017-01-16 01:56:04 +01:00
u32 cart_state_old = CART_STATE;
2017-03-01 17:03:27 +01:00
u32 sd_state_old = SD_STATE;
u64 timer = timer_start();
u64 timer_mcu = timer;
2016-06-18 15:31:31 +02:00
delay = (delay) ? 72 : 128;
while (true) {
u32 pad_state = HID_STATE;
if (timeout_sec && (timer_sec(timer) >= timeout_sec))
return TIMEOUT_HID; // HID timeout
if (!(pad_state & BUTTON_ANY)) { // no buttons pressed
2017-01-16 01:56:04 +01:00
u32 cart_state = CART_STATE;
if (cart_state != cart_state_old)
return cart_state ? CART_INSERT : CART_EJECT;
2017-03-01 17:03:27 +01:00
u32 sd_state = SD_STATE;
if (sd_state != sd_state_old)
return sd_state ? SD_INSERT : SD_EJECT;
2017-07-10 01:46:52 +02:00
u8 special_key;
if ((timer_msec(timer_mcu) >= 64) && (I2C_readRegBuf(I2C_DEV_MCU, 0x10, &special_key, 1))) {
CheckBrightness();
2017-07-10 01:46:52 +02:00
if (special_key == 0x01)
return pad_state | BUTTON_POWER;
else if (special_key == 0x04)
return pad_state | BUTTON_HOME;
timer_mcu = timer_start();
2017-07-10 01:46:52 +02:00
}
2016-04-05 23:19:29 +02:00
pad_state_old = pad_state;
2016-05-30 03:03:40 +02:00
delay = 0;
2016-04-05 23:19:29 +02:00
continue;
}
2016-05-30 03:03:40 +02:00
if ((pad_state == pad_state_old) &&
(!(pad_state & BUTTON_ARROW) ||
(delay && (timer_msec(timer) < delay))))
2016-04-05 23:19:29 +02:00
continue;
// make sure the key is pressed
2016-04-05 23:19:29 +02:00
u32 t_pressed = 0;
for(; (t_pressed < 0x13000) && (pad_state == HID_STATE); t_pressed++);
if (t_pressed >= 0x13000)
return pad_state;
}
}
bool CheckButton(u32 button) {
u32 t_pressed = 0;
for(; (t_pressed < 0x13000) && ((HID_STATE & button) == button); t_pressed++);
return (t_pressed >= 0x13000);
}