local sim = {}

local I2C_FLITECTRL              = 0x05
local I2C_POWERBOX               = 0x0A
local I2C_TEXT_GEN               = 0x0C
local I2C_GPS_STATS              = 0x17
local I2C_GPS_LOC                = 0x16
local I2C_ESC                    = 0x20
local I2C_GPS_BIN                = 0x26
local I2C_REMOTE_ID              = 0x27
local I2C_FP_BATT                = 0x34
local I2C_VARIO                  = 0x40
local I2C_SMART_BAT              = 0x42
local I2C_QOS                    = 0x7F

local simStep = 0
local lastGenTime = getTime()

local QOSSimData = {[0]=
    { 0x7F,0x00,0x00,0x5A,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x02,0x03,0x01 },
    { 0x7F,0x00,0x00,0x5B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x03,0x03,0x02 }
}

local TextGenSimData = {[0]=
   { 0x0C,0x00,0x00,0x4C,0x45,0x4D,0x4F,0x4E,0x20,0x52,0x58,0x00,0x00,0x00,0x00,0x00},
   { 0x0C,0x00,0x01,0x50,0x6F,0x74,0x20,0x41,0x2D,0x45,0x2D,0x52,0x3A,0x00,0x00,0x00},
   { 0x0C,0x00,0x02,0x36,0x30,0x20,0x20,0x35,0x38,0x20,0x20,0x35,0x37,0x20,0x20,0x20},
   { 0x0C,0x00,0x03,0x4D,0x6F,0x64,0x65,0x3A,0x20,0x52,0x78,0x20,0x4F,0x6E,0x6C,0x79},
   { 0x0C,0x00,0x04,0x46,0x53,0x3A,0x4F,0x66,0x66,0x20,0x54,0x4C,0x3A,0x4F,0x6E,0x00},
   { 0x0C,0x00,0x05,0x53,0x74,0x62,0x53,0x77,0x3A,0x20,0x43,0x37,0x28,0x41,0x32,0x29},
   { 0x0C,0x00,0x06,0x44,0x69,0x72,0x20,0x41,0x2D,0x45,0x2D,0x52,0x3A,0x4E,0x4E,0x4E},
   { 0x0C,0x00,0x07,0x41,0x6C,0x77,0x61,0x79,0x73,0x2D,0x4F,0x4E,0x3A,0x4F,0x66,0x66},
   { 0x0C,0x00,0x08,0x4D,0x2E,0x47,0x61,0x69,0x6E,0x20,0x25,0x3A,0x20,0x31,0x30,0x30}
}

local FCSimData = {[0]=
   { 0x05,0x00,0x07,0x02,0x82,0x80,0x00,0x00,0x00,0x17,0x21,0x03,0x00,0x00,0x00,0x00 },
   { 0x05,0x00,0x07,0x02,0x82,0x81,0x21,0x2B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
   { 0x05,0x00,0x07,0x02,0x82,0x82,0x3F,0x3F,0x35,0x2B,0x00,0x00,0x00,0x00,0x00,0x00 },

   { 0x05,0x00,0x0D,0x02,0x81,0x80,0x00,0x00,0x00,0x0C,0x16,0x02,0x00,0x00,0x00,0x00 },
   { 0x05,0x00,0x0D,0x02,0x81,0x81,0x20,0x2A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
   { 0x05,0x00,0x0D,0x02,0x81,0x82,0x3E,0x3E,0x34,0x2A,0x00,0x00,0x00,0x00,0x00,0x00 },
   
   { 0x05,0x00,0x00,0x02,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
   { 0x05,0x00,0x00,0x02,0x80,0x81,0x20,0x2A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
   { 0x05,0x00,0x00,0x02,0x80,0x82,0x3C,0x3C,0x32,0x28,0x00,0x00,0x00,0x00,0x00,0x00 }
}

local FlightPackSimData = {[0]=
   { 0x34,0x00,0x00,0x00,0x00,0x00,0xFF,0x7F,0xF6,0xFF,0xFF,0x7F,0xFF,0x7F,0x00,0x00  }
}

local PowerBoxSimData = {[0]=
   { 0x0A,0x00,0x04,0x21,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00  }
}

local VarioSimData = {[0]=
   { 0x40,0x00,0x00,0x00,0xFF,0xF6,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00 },
   { 0x40,0x00,0x00,0x05,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF },
   { 0x40,0x00,0x00,0x02,0x00,0x0A,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01 }
}

local GPSLocSimDataBCD = {[0]=
   { 0x16, 0x00, 0x25,0x00,0x00,0x28,0x15,0x17,0x06,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
}

local GPSStatSimDataBCD = {[0]=
    { 0x17, 0x00, 0x97,0x00,0x54,0x71,0x12,0x28,0x40,0x80,0x09,0x82,0x85,0x14,0x13,0xB9,0xFF }
}

local GPSSimDataBinary = {[0]=
    { 0x26,0x00,0x05,0x16,0x12,0x08,0xB6,0xB1,0xC5,0xA7,0xFF,0xFF,0x0C,0x2F,0x01 }
}

local RemoteIDSimData = {[0]=
    { 0x27, 0x16, 0x36, 0x48, 0x28, 0x96, 0x09, 0x33, 0x72, 0x16, 0x35, 0x11, 0x00,0x00,0x28,0x3D,0x20 },
    { 0x27, 0x17, 0x00, 0x00, 0x10, 0x17, 0x08, 0x01, 0x07, 0x00 }
}

function sim.getFrameData(id)
    local data
    if (lastGenTime + 50 > getTime()) then
        -- Generates only every 500ms
        return nil
    end

    lastGenTime = getTime()

    local dataArray
    if (id == I2C_QOS) then
        dataArray = QOSSimData
    elseif (id == I2C_TEXT_GEN) then
        dataArray = TextGenSimData
    elseif (id == I2C_FLITECTRL) then
        dataArray = FCSimData
    elseif (id == I2C_FP_BATT) then
        dataArray = FlightPackSimData
    elseif (id == I2C_POWERBOX) then
        dataArray = PowerBoxSimData
    elseif (id == I2C_VARIO) then
        dataArray = VarioSimData
    elseif (id == I2C_GPS_STATS) then
        dataArray = GPSStatSimDataBCD
    elseif (id == I2C_GPS_LOC) then
        dataArray = GPSLocSimDataBCD
    elseif (id == I2C_GPS_BIN) then
        dataArray = GPSSimDataBinary
    elseif (id == I2C_REMOTE_ID) then
        dataArray = RemoteIDSimData
    end

    if (dataArray) then
        simStep = (simStep + 1) % (#dataArray+1)
        data = dataArray[simStep]
    end

    return data
end

return sim

