Skip to content

Instantly share code, notes, and snippets.

@SonoSooS
Created August 6, 2018 17:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SonoSooS/d9dfd74a65f5c9ae2c13d76c2c29b5fa to your computer and use it in GitHub Desktop.
Save SonoSooS/d9dfd74a65f5c9ae2c13d76c2c29b5fa to your computer and use it in GitHub Desktop.
CTR_Redshift ported to Luma3DS
#include <3ds.h>
#include "menus.h"
#include "menu.h"
#include "draw.h"
#include "colorramp.h"
#include "ifile.h"
#include "memory.h"
#include "fmt.h"
/*
CTR_Redshift - redshift for Nintendo 3DS
Copyright (C) 2017-2018 Sono
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Lesser Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Redshift - https://github.com/jonls/redshift
Redshift is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Redshift is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2009-2017 Jon Lund Steffensen <jonlst@gmail.com>
*/
#define COLORREG_IDX_T REG32(0x10400480)
#define COLORREG_DAT_T REG32(0x10400484)
#define COLORREG_IDX_B REG32(0x10400580)
#define COLORREG_DAT_B REG32(0x10400584)
static void WriteAt(u32 v, u8 idx, int screen)
{
if(screen & 1)
{
COLORREG_IDX_B = (u32)idx;
COLORREG_DAT_B = v;
}
else
{
COLORREG_IDX_T = (u32)idx;
COLORREG_DAT_T = v;
}
}
static void ClampCS(color_setting_t* cs)
{
if(cs->temperature < MIN_TEMP) cs->temperature = MIN_TEMP;
if(cs->temperature > MAX_TEMP) cs->temperature = MAX_TEMP;
if(cs->gamma[0] < MIN_GAMMA) cs->gamma[0] = MIN_GAMMA;
if(cs->gamma[1] < MIN_GAMMA) cs->gamma[1] = MIN_GAMMA;
if(cs->gamma[2] < MIN_GAMMA) cs->gamma[2] = MIN_GAMMA;
if(cs->gamma[0] > MAX_GAMMA) cs->gamma[0] = MAX_GAMMA;
if(cs->gamma[1] > MAX_GAMMA) cs->gamma[1] = MAX_GAMMA;
if(cs->gamma[2] > MAX_GAMMA) cs->gamma[2] = MAX_GAMMA;
if(cs->brightness < MIN_BRIGHTNESS) cs->brightness = MIN_BRIGHTNESS;
if(cs->brightness > MAX_BRIGHTNESS) cs->brightness = MAX_BRIGHTNESS;
}
static void ApplyCS(color_setting_t* cs, int screen)
{
u16 c[0x300];
u8 i = 0;
do
{
*(c + i + 0x000) = i | (i << 8);
*(c + i + 0x100) = i | (i << 8);
*(c + i + 0x200) = i | (i << 8);
}
while(++i);
colorramp_fill(c + 0x000, c + 0x100, c + 0x200, 0x100, cs);
do
{
WriteAt((c[i] >> 8) | ((c[i + 0x100] >> 8) << 8) | ((c[i + 0x200] >> 8) << 16), i, screen);
}
while(++i);
}
float fmodf(float f1, float f2);
static void redshift(void)
{
Draw_Lock();
Draw_ClearFramebuffer();
Draw_FlushFramebuffer();
Draw_Unlock();
color_setting_t cs;
memset(&cs, 0, sizeof(cs));
cs.temperature = NEUTRAL_TEMP;
cs.gamma[0] = 1.0F;
cs.gamma[1] = 1.0F;
cs.gamma[2] = 1.0F;
cs.brightness = 1.0F;
color_setting_t screns[2];
memcpy(screns + 0, &cs, sizeof(cs));
memcpy(screns + 1, &cs, sizeof(cs));
IFile file;
Result res = 0;
if(!(HID_PAD & ~(BUTTON_A | BUTTON_B)))
{
res = IFile_Open(&file, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""),
fsMakePath(PATH_ASCII, "/luma/redshift.bin"), FS_OPEN_READ);
if(R_SUCCEEDED(res))
{
u64 total;
IFile_Read(&file, &total, screns, sizeof(screns));
ApplyCS(screns + 0, 0);
ApplyCS(screns + 1, 1);
IFile_Close(&file);
}
}
while(HID_PAD & (BUTTON_A | BUTTON_B))
svcSleepThread(1 * 1000 * 1000LL);
u32 kDown = 0;
//u32 kUp = 0;
u32 kHeld = 0;
u32 kOld = 0;
int sel = 0;
char fmtbuf[0x40];
do
{
kOld = kHeld;
kHeld = HID_PAD;
kDown = (~kOld) & kHeld;
//kUp = kOld & (~kHeld);
if(kDown & BUTTON_START)
{
res = IFile_Open(&file, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""),
fsMakePath(PATH_ASCII, "/luma/redshift.bin"), FS_OPEN_CREATE | FS_OPEN_WRITE);
if(R_SUCCEEDED(res))
{
u64 total;
res = IFile_Write(&file, &total, screns, sizeof(screns), 0);
IFile_Close(&file);
}
}
if(kDown & BUTTON_SELECT)
break;
if(kDown & BUTTON_RIGHT)
{
if(++sel > 4) sel = 4;
}
if(kDown & BUTTON_LEFT)
{
if(--sel < 0) sel = 0;
}
if(kDown & (BUTTON_X | BUTTON_Y))
{
memset(&cs, 0, sizeof(cs));
cs.temperature = NEUTRAL_TEMP;
cs.gamma[0] = 1.0F;
cs.gamma[1] = 1.0F;
cs.gamma[2] = 1.0F;
cs.brightness = 1.0F;
}
if((((kDown & BUTTON_UP) ? 1 : 0) ^ ((kDown & BUTTON_DOWN) ? 1 : 0)))
{
if(!sel)
{
if(kDown & BUTTON_UP)
cs.temperature += (kHeld & (BUTTON_L1 | BUTTON_R1)) ? 1 : 100;
else
cs.temperature -= (kHeld & (BUTTON_L1 | BUTTON_R1)) ? 1 : 100;
}
else
{
float* f = 0;
if(!(sel >> 2))
f = &cs.gamma[sel - 1];
if(!(sel ^ 4))
f = &cs.brightness;
if(f)
{
if(kDown & BUTTON_UP)
*f = (((float)(((int)((*f * 100.0F) + 0.0001F)) + ((kHeld & (BUTTON_L1 | BUTTON_R1)) ? 1 : 10))) / 100.0F) + 0.000001F;
else
*f = (((float)(((int)((*f * 100.0F) + 0.0001F)) - ((kHeld & (BUTTON_L1 | BUTTON_R1)) ? 1 : 10))) / 100.0F) + 0.000001F;
}
}
}
if(kDown)
{
ClampCS(&cs);
if(kHeld & BUTTON_A)
{
memcpy(screns + 1, &cs, sizeof(cs));
ApplyCS(&cs, 1);
}
if(kHeld & BUTTON_B)
{
memcpy(screns + 0, &cs, sizeof(cs));
ApplyCS(&cs, 0);
}
}
Draw_Lock();
Draw_DrawString(10, 8, COLOR_TITLE, "CTR_Redshift - ported by Sono");
Draw_DrawString(10, 18, COLOR_TITLE, "START save config, SELECT go back,");
Draw_DrawString(10, 28, COLOR_TITLE, "B set top screen, A set bottom screen");
Draw_DrawString(10, 38, COLOR_TITLE, "UP/DOWN change value, LEFT/RIGHT move menu");
sprintf(fmtbuf, "%c Colortemp: %iK", (sel == 0 ? '>' : ' '), cs.temperature);
Draw_DrawString(10, 56, COLOR_TITLE, fmtbuf);
sprintf(fmtbuf, "%c Gamma[R]: %i.%02i", (sel == 1 ? '>' : ' '),
(int)cs.gamma[0], (int)fmodf((cs.gamma[0] * 100.0F) + 0.0001F, 100.0F));
Draw_DrawString(10, 72, COLOR_TITLE, fmtbuf);
sprintf(fmtbuf, "%c Gamma[G]: %i.%02i", (sel == 2 ? '>' : ' '),
(int)cs.gamma[1], (int)fmodf((cs.gamma[1] * 100.0F) + 0.0001F, 100.0F));
Draw_DrawString(10, 80, COLOR_TITLE, fmtbuf);
sprintf(fmtbuf, "%c Gamma[B]: %i.%02i", (sel == 3 ? '>' : ' '),
(int)cs.gamma[2], (int)fmodf((cs.gamma[2] * 100.0F) + 0.0001F, 100.0F));
Draw_DrawString(10, 88, COLOR_TITLE, fmtbuf);
sprintf(fmtbuf, "%c Brightness: %i.%02i", (sel == 4 ? '>' : ' '),
(int)cs.brightness, (int)fmodf((cs.brightness * 100.0F) + 0.0001F, 100.0F));
Draw_DrawString(10, 104, COLOR_TITLE, fmtbuf);
Draw_FlushFramebuffer();
Draw_Unlock();
svcSleepThread(1 * 1000 * 1000LL);
}
while(!terminationRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment