paulo@0: #include paulo@0: paulo@0: /* paulo@0: by Mighty Max and melw, paulo@0: after http://forum.gbadev.org/viewtopic.php?t=12011 paulo@0: */ paulo@0: paulo@0: int needLidSleep(void) { paulo@0: // when reading keys paulo@0: if (IPC->buttons & 0x0080) { paulo@0: /* hinge is closed */ paulo@0: paulo@0: /* Step 1: Turn off all interrupts but vblank for waking up */ paulo@0: unsigned long oldIE = REG_IE; paulo@0: REG_IE = IRQ_VBLANK; paulo@0: paulo@0: /* Step 2: Power off most of the system */ paulo@0: powerOFF(POWER_LCD); paulo@0: paulo@0: /* Step 3: Wait one vblank at a time until the ARM7 tells us paulo@0: that the lid is no longer closed */ paulo@0: while (IPC->buttons & 0x0080) { paulo@0: swiWaitForVBlank(); paulo@0: } paulo@0: paulo@0: /* Step 4: Wait a bit more (necessary?) */ paulo@0: swiWaitForVBlank(); paulo@0: paulo@0: /* Step 5: Restore old machine state */ paulo@0: powerON(POWER_LCD); paulo@0: REG_IE = oldIE; paulo@0: return 1; paulo@0: } paulo@0: return 0; paulo@0: }