/*
    MIT License

    Copyright (c) 2018-2020, Alexey Dynda

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/

#include "sdl_ssd1351.h"
#include "sdl_oled_basic.h"
#include "sdl_emulator_common.h"
#include "sdl_graphics.h"
#include "sdl_core.h"

static int s_activeColumn = 0;
static int s_activePage = 0;
static int s_columnStart = 0;
static int s_columnEnd = 127;
static int s_pageStart = 0;
static int s_pageEnd = 7;
static uint8_t s_incrementMode = 0;
static uint8_t s_topToBottom = 0;
static uint8_t s_leftToRight = 0;
static uint8_t detected = 0;
static uint8_t s_rgbMode = 0;

static void sdl_ssd1351_reset(void)
{
    detected = 0;
}


static int sdl_ssd1351_detect(uint8_t data)
{
    if (detected)
    {
        return 1;
    }
    detected = (data == SDL_LCD_SSD1351);
    return 0;
}

static void sdl_ssd1351_process_data(uint8_t data)
{
    switch (s_commandId)
    {
        case 0xA0:
            if (s_cmdArgIndex == 0)
            {
                s_incrementMode = data & 0x01;
                s_leftToRight = data & 0x02;
                s_rgbMode = data & 0x04;
                s_topToBottom = data & 0x10;

                s_commandId = SSD_COMMAND_NONE;
            }
            break;
        case 0x15:
            switch (s_cmdArgIndex)
            {
                case 0:
                     s_columnStart = data;
                     s_activeColumn = data;
                     break;
                case 1:
                     s_columnEnd = data;
                     s_commandId = SSD_COMMAND_NONE;
                     break;
                default: break;
            }
            break;
        case 0x75:
            switch (s_cmdArgIndex)
            {
                case 0:
                     s_activePage = data;
                     s_pageStart = data;
                     break;
                case 1:
                     s_pageEnd = data;
                     s_commandId = SSD_COMMAND_NONE;
                     break;
                default: break;
            }
            break;
        case 0x5C:
            sdl_set_data_mode( SDM_WRITE_DATA );
            s_commandId = SSD_COMMAND_NONE;
            break;
        case 0xB4:
            if ( s_cmdArgIndex == 2)
            {
                s_commandId = SSD_COMMAND_NONE;
            }
            break;
        // 1-arg config commands: consume argument to avoid misinterpretation
        case 0xA1: // Display Start Line
        case 0xA2: // Display Offset
        case 0xAB: // Function Selection
        case 0xB1: // Phase Length
        case 0xB3: // Front Clock Divider
        case 0xB5: // Set GPIO
        case 0xB6: // Second Pre-charge Period
        case 0xBB: // Pre-charge Voltage
        case 0xBE: // VCOMH Voltage
        case 0xC7: // Master Contrast Current
        case 0xCA: // Set MUX Ratio
        case 0xFD: // Command Lock
            if ( s_cmdArgIndex == 0 )
            {
                s_commandId = SSD_COMMAND_NONE;
            }
            break;
        case 0xC1: // Set Contrast (3 args)
            if ( s_cmdArgIndex == 2 )
            {
                s_commandId = SSD_COMMAND_NONE;
            }
            break;
        default:
            s_commandId = SSD_COMMAND_NONE;
            break;
    }
}

static void sdl_ssd1351_commands(uint8_t data)
{
    // SSD1351 display can process command args only in data mode
    if ( !sdl_is_dc_mode() && s_commandId != data )
    {
        s_commandId = SSD_COMMAND_NONE;
    }
    sdl_ssd1351_process_data( data );
}

void sdl_ssd1351_data(uint8_t data)
{
    int y = s_topToBottom ? s_activePage : (sdl_ssd1351.height - s_activePage - 1);
    int x = s_leftToRight ? (sdl_ssd1351.width - s_activeColumn - 1) : s_activeColumn;
    static uint8_t firstByte = 1;  /// SSD1351
    static uint8_t dataFirst = 0x00;  /// SSD1351
    if (firstByte)
    {
        dataFirst = data;
        firstByte = 0;
        return;
    }
    firstByte = 1;
    sdl_put_pixel(x, y, s_rgbMode ? ((dataFirst<<8) | data) : ((data<<8) | dataFirst));

    sdl_emu_advance_xy(&s_activeColumn, &s_activePage,
                       s_columnStart, s_columnEnd, s_pageStart, s_pageEnd,
                       s_incrementMode);
}

sdl_oled_info sdl_ssd1351 =
{
    .width = 128,
    .height = 128,
    .bpp = 16,
    .pixfmt = SDL_PIXELFORMAT_RGB565,
    .dataMode = SDMS_CONTROLLER,
    .detect = sdl_ssd1351_detect,
    .run_cmd = sdl_ssd1351_commands,
    .run_data = sdl_ssd1351_data,
    .reset = sdl_ssd1351_reset,
};
