From ca51098ca0ac23d88f5b9235c0cc0244521175b8 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Thu, 25 Dec 2014 02:53:43 -0800 Subject: [PATCH] GBA: Hook up light sensor API --- src/gba/gba-gpio.c | 10 +++++++++- src/gba/gba-gpio.h | 3 ++- src/gba/gba-sensors.h | 6 ++++++ src/gba/gba.c | 4 ++++ src/gba/gba.h | 2 ++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/gba/gba-gpio.c b/src/gba/gba-gpio.c index 53c138e82..19192e7df 100644 --- a/src/gba/gba-gpio.c +++ b/src/gba/gba-gpio.c @@ -322,6 +322,7 @@ void GBAGPIOInitLightSensor(struct GBACartridgeGPIO* gpio) { gpio->gpioDevices |= GPIO_LIGHT_SENSOR; gpio->lightCounter = 0; gpio->lightEdge = false; + gpio->lightSample = 0xFF; } void _lightReadPins(struct GBACartridgeGPIO* gpio) { @@ -330,15 +331,22 @@ void _lightReadPins(struct GBACartridgeGPIO* gpio) { return; } if (gpio->p1) { + struct GBALuminanceSource* lux = gpio->p->luminanceSource; GBALog(0, GBA_LOG_DEBUG, "[SOLAR] Got reset"); gpio->lightCounter = 0; + if (lux) { + lux->sample(lux); + gpio->lightSample = lux->readLuminance(lux); + } else { + gpio->lightSample = 0xFF; + } } if (gpio->p0 && gpio->lightEdge) { ++gpio->lightCounter; } gpio->lightEdge = !gpio->p0; - bool sendBit = gpio->lightCounter >= 0x80; + bool sendBit = gpio->lightCounter >= gpio->lightSample; _outputPins(gpio, sendBit << 3); GBALog(0, GBA_LOG_DEBUG, "[SOLAR] Output %u with pins %u", gpio->lightCounter, gpio->pinState); } diff --git a/src/gba/gba-gpio.h b/src/gba/gba-gpio.h index 1664cba43..a876703e8 100644 --- a/src/gba/gba-gpio.h +++ b/src/gba/gba-gpio.h @@ -104,7 +104,8 @@ struct GBACartridgeGPIO { uint16_t gyroSample; bool gyroEdge; - int lightCounter : 12; + unsigned lightCounter : 12; + uint8_t lightSample; bool lightEdge; }; diff --git a/src/gba/gba-sensors.h b/src/gba/gba-sensors.h index 3d2d63b37..301f7f496 100644 --- a/src/gba/gba-sensors.h +++ b/src/gba/gba-sensors.h @@ -17,4 +17,10 @@ struct GBARotationSource { int32_t (*readGyroZ)(struct GBARotationSource*); }; +struct GBALuminanceSource { + void (*sample)(struct GBALuminanceSource*); + + uint8_t (*readLuminance)(struct GBALuminanceSource*); +}; + #endif diff --git a/src/gba/gba.c b/src/gba/gba.c index 26a560bab..5f4131d5e 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -732,6 +732,10 @@ void _checkOverrides(struct GBA* gba, uint32_t id) { GBAGPIOInitRumble(&gba->memory.gpio); } + if (_overrides[i].gpio & GPIO_LIGHT_SENSOR) { + GBAGPIOInitLightSensor(&gba->memory.gpio); + } + gba->busyLoop = _overrides[i].busyLoop; return; } diff --git a/src/gba/gba.h b/src/gba/gba.h index 5f104278e..8d90cce02 100644 --- a/src/gba/gba.h +++ b/src/gba/gba.h @@ -115,7 +115,9 @@ struct GBA { int* keySource; uint32_t busyLoop; struct GBARotationSource* rotationSource; + struct GBALuminanceSource* luminanceSource; struct GBARumble* rumble; + struct GBARRContext* rr; void* pristineRom; size_t pristineRomSize;