local modelName = "Unknown"
local LIHV = 0	
local lowVoltage = 9.9
local highVoltage = 12.6
local VoltageSourceId = -1
local GPSId = -1
local GSpdId = -1
local GpsStatusId = -1
local HomeDistanceId = -1
local VFASId = -1
local AltId = -1
local CurrId = -1
local CelsId = -1
local FuelId = -1
local HdgId = -1
local ModeId = -1
local PitchId = -1
local RollId = -1
local MY_LCD_W = LCD_W
local defaultscreen = 0		-- iniciar en pantalla por defecto
local maxscreen = 4	-- number of screens to show (change screen with + and - buttons, and page button)
local screen = defaultscreen
local armed = 0
local mode = 0
local animationIncrement = 0
local isArmed = 0
local rssi = 0
local status = ""
local lat = "99.9999999"
local lon = "99.9999999"
local dist = 0
local voltage = 0	-- voltaje ultimo visto (para resetear timer1 cuando se quita bateria)
local volzero = 0	-- boolean para saber si se ha quitado bateria
local volmin = 0	-- voltaje minimo (se actualiza desde background)
local volminreset = 1	-- boolean para saber cuando se ha desarmado (para
local currmax = 0	-- consumo mas alto
local timearmed = 0	-- hora de armado desarmado
local timeunarmed = 0	-- hora de armado desarmado


local function HelpScreen()
    -- estara dibujado un header, si no lo quieres ejecuta un borrar pantalla
    lcd.drawText(2, 9, "SE: Disarm / Arm / Beeper",SMLSIZE)
    lcd.drawText(2, 17, "SA: Angle / Horizon / AcroTrain",SMLSIZE)
    lcd.drawText(2, 25, "SB: - / AirMode / Antigravity",SMLSIZE)
    lcd.drawText(2, 33, "SC: - / HeadFree Adj / HeadFree",SMLSIZE)
    lcd.drawText(2, 41, "SD: - / Horizon / GPS Rescue",SMLSIZE)
    lcd.drawText(2, 49, "SF: - / Flip over after crash",SMLSIZE)
    lcd.drawText(2, 57, "SG: Change OSD (1/2/3)",SMLSIZE)
end
local function mod(a,b)
  return a - math.floor(a/b)*b
end
local function div(a,b)
  return math.floor(a/b)
end
local function convertVoltageToPercentage(voltage)
  local curVolPercent = math.ceil(((((highVoltage - voltage) / (highVoltage - lowVoltage)) - 1) * -1) * 100)
  if curVolPercent < 0 then
    curVolPercent = 0
  end
  if curVolPercent > 100 then
    curVolPercent = 100
  end
  return curVolPercent
end
local function stepsTime(divisor)
  return math.ceil(math.fmod(getTime() / divisor, 2) )
end
local function setAnimationIncrement()
  animationIncrement = math.fmod(math.ceil(math.fmod(getTime() / 100, 2) * 8), 4)
end
local function drawPropellor(start_x, start_y, invert)
  local animationIncrementLocal = animationIncrement
  if invert == true then
    animationIncrementLocal = (animationIncrementLocal - 3) * -1
    animationIncrementLocal = animationIncrementLocal + 3
    if animationIncrementLocal > 3 then
      animationIncrementLocal = animationIncrementLocal - 4
    end
  end
  
  -- Animated Quadcopter propellors
  if ((isArmed == 0 or isArmed == 2) and invert == false) or (isArmed == 1 and animationIncrementLocal == 0) then
    -- Top left Propellor
    lcd.drawLine(start_x + 1, start_y + 9, start_x + 9, start_y + 1, SOLID, FORCE)
    lcd.drawLine(start_x + 1, start_y + 10, start_x + 8, start_y + 1, SOLID, FORCE)
  elseif isArmed == 1 and animationIncrementLocal == 1 then
    -- Top left Propellor
    lcd.drawLine(start_x, start_y + 5, start_x + 9, start_y + 5, SOLID, FORCE)
    lcd.drawLine(start_x, start_y + 4, start_x + 9, start_y + 6, SOLID, FORCE)
  elseif ((isArmed == 0 or isArmed == 2) and invert == true) or (isArmed == 1 and animationIncrementLocal == 2) then
    -- Top left Propellor
    lcd.drawLine(start_x + 1, start_y + 1, start_x + 9, start_y + 9, SOLID, FORCE)
    lcd.drawLine(start_x + 1, start_y + 2, start_x + 10, start_y + 9, SOLID, FORCE)
  elseif isArmed == 1 and animationIncrementLocal == 3 then
    -- Top left Propellor
    lcd.drawLine(start_x + 5, start_y, start_x + 5, start_y + 10, SOLID, FORCE)
    lcd.drawLine(start_x + 6, start_y, start_x + 4, start_y + 10, SOLID, FORCE)
  end
end
local function drawQuadcopter(start_x,start_y)
  
  -- Top left to bottom right
  lcd.drawLine(start_x + 4, start_y + 4, start_x + 26, start_y + 26, SOLID, FORCE)
  lcd.drawLine(start_x + 4, start_y + 5, start_x + 25, start_y + 26, SOLID, FORCE)
  lcd.drawLine(start_x + 5, start_y + 4, start_x + 26, start_y + 25, SOLID, FORCE)
  
  -- Bottom left to top right
  lcd.drawLine(start_x + 4, start_y + 26, start_x + 26, start_y + 4, SOLID, FORCE)
  lcd.drawLine(start_x + 4, start_y + 25, start_x + 25, start_y + 4, SOLID, FORCE)
  lcd.drawLine(start_x + 5, start_y + 26, start_x + 26, start_y + 5, SOLID, FORCE)
  
  -- Middle of Quad
  lcd.drawRectangle(start_x + 11, start_y + 11, 9, 9, SOLID)
  lcd.drawRectangle(start_x + 12, start_y + 12, 7, 7, SOLID)
  lcd.drawRectangle(start_x + 13, start_y + 13, 5, 5, SOLID)

  -- ARMED text
  if isArmed == 1 then
    lcd.drawText(start_x + 3, start_y + 12, "ARMED", SMLSIZE + BLINK)
  end
  
  -- Top-left propellor
  drawPropellor(start_x, start_y, false)
  -- Bottom-Right Propellor
  drawPropellor(start_x + 20, start_y + 20, false)
  -- Top-Right Propellor
  drawPropellor(start_x + 20, start_y, true)
  -- Bottom-left Propellor
  drawPropellor(start_x, start_y + 20, true)
  
end
local function drawTransmitterVoltage(start_x,start_y,voltage)
  
  local batteryWidth = 17
  
  -- Battery Outline
  lcd.drawRectangle(start_x, start_y, batteryWidth + 2, 6, SOLID)
  lcd.drawLine(start_x + batteryWidth + 2, start_y + 1, start_x + batteryWidth + 2, start_y + 4, SOLID, FORCE) -- Positive Nub

  -- Battery Percentage (after battery)
  local curVolPercent = convertVoltageToPercentage(voltage)
  if curVolPercent < 20 then
    lcd.drawText(start_x + batteryWidth + 5, start_y, curVolPercent.."%", SMLSIZE + BLINK)
  else
    if curVolPercent == 100 then
      lcd.drawText(start_x + batteryWidth + 5, start_y, "99%", SMLSIZE)
    else
      lcd.drawText(start_x + batteryWidth + 5, start_y, curVolPercent.."%", SMLSIZE)
    end
      
  end
  
  -- Filled in battery
  local pixels = math.ceil((curVolPercent / 100) * batteryWidth)
  if pixels == 1 then
    lcd.drawLine(start_x + pixels, start_y + 1, start_x + pixels, start_y + 4, SOLID, FORCE)
  end
  if pixels > 1 then
    lcd.drawRectangle(start_x + 1, start_y + 1, pixels, 4)
  end
  if pixels > 2 then
    lcd.drawRectangle(start_x + 2, start_y + 2, pixels - 1, 2)
    lcd.drawLine(start_x + pixels, start_y + 2, start_x + pixels, start_y + 3, SOLID, FORCE)
  end
end
local function drawFlightTimer(start_x, start_y)
  local timerWidth = 44
  local timerHeight = 20
  local myWidth = 0
  local percentageLeft = 0
  lcd.drawRectangle( start_x, start_y, timerWidth, 10 )
  lcd.drawText( start_x + 2, start_y + 2, "Fly Timer", SMLSIZE )
  lcd.drawRectangle( start_x, start_y + 10, timerWidth, timerHeight )
  lcd.drawTimer( start_x + 2, start_y + 12, timerLeft, DBLSIZE )
end
local function drawTime(starx,stary)
  local datenow = getDateTime()
  local min = string.format("%02.0f", datenow.min)
  local hour = string.format("%02.0f", datenow.hour)
  if stepsTime(100) == 1 then  
    hour = hour .. ":"
  end
  lcd.drawText(starx,stary,hour, SMLSIZE)
  lcd.drawText(starx+12,stary,min, SMLSIZE)
end
local function drawRSSI(start_x, start_y)
  local timerWidth = 44
  local timerHeight = 15
  local myWidth = 0
  local percentageLeft = 0
  
  lcd.drawRectangle( start_x, start_y, timerWidth, 10 )
  lcd.drawText( start_x + 2, start_y + 2, "RSSI:", SMLSIZE)
  if rssi < 50 then
    lcd.drawText( start_x + 25, start_y + 2, rssi, SMLSIZE + BLINK + INVERS)
  else
    lcd.drawText( start_x + 25, start_y + 2, rssi, SMLSIZE)
  end
  lcd.drawRectangle( start_x, start_y + 10, timerWidth, timerHeight )
  
  local end_y = start_y + 23

  if rssi > 0 then
    lcd.drawLine(start_x + 1,  start_y + 20, start_x + 1,  end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 2,  start_y + 20, start_x + 2,  end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 3,  start_y + 20, start_x + 3,  end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 4,  start_y + 20, start_x + 4,  end_y, SOLID, FORCE)
  end
  if rssi > 10 then
    lcd.drawLine(start_x + 5,  start_y + 19, start_x + 5,  end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 6,  start_y + 19, start_x + 6,  end_y, SOLID, FORCE)
  end
  if rssi > 13 then
    lcd.drawLine(start_x + 7,  start_y + 19, start_x + 7,  end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 8,  start_y + 19, start_x + 8,  end_y, SOLID, FORCE)
  end
  if rssi > 16 then
    lcd.drawLine(start_x + 9,  start_y + 18, start_x + 9,  end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 10, start_y + 18, start_x + 10, end_y, SOLID, FORCE)
  end
  if rssi > 19 then
    lcd.drawLine(start_x + 11, start_y + 18, start_x + 11, end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 12, start_y + 18, start_x + 12, end_y, SOLID, FORCE)
  end
  if rssi > 22 then
    lcd.drawLine(start_x + 13, start_y + 17, start_x + 13, end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 14, start_y + 17, start_x + 14, end_y, SOLID, FORCE)
  end
  if rssi > 25 then
    lcd.drawLine(start_x + 15, start_y + 17, start_x + 15, end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 16, start_y + 17, start_x + 16, end_y, SOLID, FORCE)
  end
  if rssi > 28 then
    lcd.drawLine(start_x + 17, start_y + 16, start_x + 17, end_y, SOLID, FORCE)
    lcd.drawLine(start_x + 18, start_y + 16, start_x + 18, end_y, SOLID, FORCE)
  end
  if rssi > 31 then
    lcd.drawLine(start_x + 19, start_y + 16, start_x + 19, end_y, SOLID, FORCE)
  end
  if rssi > 34 then
    lcd.drawLine(start_x + 20, start_y + 16, start_x + 20, end_y, SOLID, FORCE)
  end
  if rssi > 37 then
    lcd.drawLine(start_x + 21, start_y + 15, start_x + 21, end_y, SOLID, FORCE)
  end
  if rssi > 40 then
    lcd.drawLine(start_x + 22, start_y + 15, start_x + 22, end_y, SOLID, FORCE)
  end
  if rssi > 43 then
    lcd.drawLine(start_x + 23, start_y + 15, start_x + 23, end_y, SOLID, FORCE)
  end
  if rssi > 46 then
    lcd.drawLine(start_x + 24, start_y + 15, start_x + 24, end_y, SOLID, FORCE)
  end
  if rssi > 49 then
    lcd.drawLine(start_x + 25, start_y + 14, start_x + 25, end_y, SOLID, FORCE)
  end
  if rssi > 52 then
    lcd.drawLine(start_x + 26, start_y + 14, start_x + 26, end_y, SOLID, FORCE)
  end
  if rssi > 55 then
    lcd.drawLine(start_x + 27, start_y + 14, start_x + 27, end_y, SOLID, FORCE)
  end
  if rssi > 58 then
    lcd.drawLine(start_x + 28, start_y + 14, start_x + 28, end_y, SOLID, FORCE)
  end
  if rssi > 61 then
    lcd.drawLine(start_x + 29, start_y + 13, start_x + 29, end_y, SOLID, FORCE)
  end
  if rssi > 64 then
    lcd.drawLine(start_x + 30, start_y + 13, start_x + 30, end_y, SOLID, FORCE)
  end
  if rssi > 67 then
    lcd.drawLine(start_x + 31, start_y + 13, start_x + 31, end_y, SOLID, FORCE)
  end
  if rssi > 70 then
    lcd.drawLine(start_x + 32, start_y + 13, start_x + 32, end_y, SOLID, FORCE)
  end
  if rssi > 73 then
    lcd.drawLine(start_x + 33, start_y + 12, start_x + 33, end_y, SOLID, FORCE)
  end
  if rssi > 76 then
    lcd.drawLine(start_x + 34, start_y + 12, start_x + 34, end_y, SOLID, FORCE)
  end
  if rssi > 79 then
    lcd.drawLine(start_x + 35, start_y + 12, start_x + 35, end_y, SOLID, FORCE)
  end
  if rssi > 82 then
    lcd.drawLine(start_x + 36, start_y + 12, start_x + 36, end_y, SOLID, FORCE)
  end
  if rssi > 85 then
    lcd.drawLine(start_x + 37, start_y + 11, start_x + 37, end_y, SOLID, FORCE)
  end
  if rssi > 88 then
    lcd.drawLine(start_x + 38, start_y + 11, start_x + 38, end_y, SOLID, FORCE)
  end
  if rssi > 91 then
    lcd.drawLine(start_x + 39, start_y + 11, start_x + 39, end_y, SOLID, FORCE)
  end
  if rssi > 94 then
    lcd.drawLine(start_x + 40, start_y + 11, start_x + 40, end_y, SOLID, FORCE)
  end
  if rssi > 97 then
    lcd.drawLine(start_x + 41, start_y + 11, start_x + 41, end_y, SOLID, FORCE)
  end
  if rssi > 98 then
    lcd.drawLine(start_x + 42, start_y + 11, start_x + 42, end_y, SOLID, FORCE)
  end
end
local function drawHeaderSignal(starx,stary)  
  if rssi > 0 then
    lcd.drawLine(starx+3, stary+5, starx+3, stary+5, SOLID, FORCE)
    lcd.drawLine(starx+2, stary+2, starx+4, stary+2, SOLID, FORCE)
    lcd.drawLine(starx+1, stary+3, starx+1, stary+3, SOLID, FORCE)
    lcd.drawLine(starx+5, stary+3, starx+5, stary+3, SOLID, FORCE)
    lcd.drawLine(starx+1, stary, starx+5, stary, SOLID, FORCE)
    lcd.drawLine(starx, stary+1, starx, stary+1, SOLID, FORCE)
    lcd.drawLine(starx+6, stary+1, starx+6, stary+1, SOLID, FORCE)
  end
end
local function drawStatus()
  if ( status ~= "" ) then
    local x=(MY_LCD_W-7*#status)/2
    lcd.drawText(x,LCD_H/2-5,status,MIDSIZE+INVERS+BLINK)
    lcd.drawRectangle(x-1,LCD_H/2-6,lcd.getLastPos()-x+2,14,ERASE)
    lcd.drawRectangle(x-2,LCD_H/2-7,lcd.getLastPos()-x+4,16,SOLID)
    lcd.drawRectangle(x-3,LCD_H/2-8,lcd.getLastPos()-x+6,18,ERASE)
    status = ""
  end
end
local function Interpolar(x1,y1,x2,y2,size)
  return (size-y1)/(y2-y1)*(x2-x1)+x1
end
local function drawHorizon(starx,stary,size)
  local pitch=getValue(PitchId)/10
  local roll=getValue(RollId)/10
  local dx=math.cos(math.rad(roll)) * size
  local dy=math.sin(math.rad(roll)) * size
  local p=math.sin(math.rad(pitch)) * size * 0.85
  local x1,y1=-dx,-dy-p
  local x2,y2=dx,dy-p

  -- dibujar rectangulo
  lcd.drawFilledRectangle(starx-size,stary-size,size+size+1,size+size+1,ERASE)
  lcd.drawRectangle(starx-size-1,stary-size-1,size+size+3,size+size+3,SOLID)

  -- lcd.drawText(0,0,string.format("%2.1f,%2.1f,%2.1f,%2.1f",x1,y1,x2,y2),SMLSIZE) -- for debugging

  -- ver si se sale y corregir
  if (y1>size) then		-- se sale linea por arriba? esta bien comparacion?
    x1=Interpolar(x2,y2,x1,y1,size)  -- deberiamos calcular esto
    y1=size
  end
  if (y2>size) then		-- se sale linea por arriba? esta bien comparacion?
    x2=Interpolar(x1,y1,x2,y2,size)  -- deberiamos calcular esto
    y2=size
  end
  if (y1<-size) then		-- se sale linea por abajo? esta bien comparacion?
    x1=Interpolar(x2,y2,x1,y1,-size)  -- deberiamos calcular esto
    y1=-size
  end
  if (y2<-size) then		-- se sale linea por abajo? esta bien comparacion?
    x2=Interpolar(x1,y1,x2,y2,-size)  -- deberiamos calcular esto
    y2=-size
  end

  -- lcd.drawText(0,8,string.format("%2.1f,%2.1f,%2.1f,%2.1f",x1,y1,x2,y2),SMLSIZE) -- for debuging
  -- texto con el angulo de pitch
  -- lcd.drawText(starx-size,stary+size-7,string.format("%3.0fº", pitch),SMLSIZE)
  -- dibujar linea
  lcd.drawLine(starx+x1, stary+y1, starx+x2, stary+y2, SOLID, FORCE)	-- dibujar linea horizonte
  -- dibujar cruz en medio
  lcd.drawLine(starx-2,stary,starx+2,stary,SOLID,FORCE)	-- dibujar cruz en medio
  lcd.drawLine(starx,stary-2,starx,stary+2,SOLID,FORCE)	-- dibujar cruz en medio
end
local function getModeTelemetry()
  if (ModeId==-1) then		-- si no hay variable Tmp1 salir sin mas
    return 0
  end
  local mode=getValue(ModeId)	-- leer telemetria
  if (mode) then
    if(mode<10000 and mode~=0) then		-- si no es el tmp1 correcto....
      status="There are 2 Tmp1, remove one"
      return 0
    end
    return mode
  else
    return 0
  end
end
local function getGpsSat()
  local n = mod(getValue (GpsStatusId),100)
  return n
end
local function getArmed()
  armed=mod(getModeTelemetry(),10)
  if (armed>=4) then
    return 1
  else
    return 0
  end
end
local function getModeText()
  mode = mod(div(getModeTelemetry(),10),10)
  if(mode==0) then
    return "Acro"
  elseif(mode==1) then
    return "Angle"
  elseif(mode==2) then
    return "Horizon"
  else
    return "Mode " .. mode
  end
end
local function getVoltage()
  local voltage = getValue(VoltageSourceId)
  if CelsId ~= -1 then
    local cells = getValue(CelsId)
    if typeof(cells) == 'table' then
      for k, v in pairs(cells) do
        if(k==0) then
          voltage=v
        elseif(v<voltage) then
          voltage=v
        end
      end
    end
  end

  return voltage
end
local function drawVoltageText(start_x, start_y)
  local voltage=getVoltage()

  local mode=0
  if LIHV and voltage<=3.3 then
    mode=BLINK+INVERS
  end
  if LIHV==0 and voltage<=3.45 then 
    mode=BLINK+INVERS
  end
  lcd.drawText(start_x,start_y,string.format("%4.2f", voltage),MIDSIZE+mode)
  lcd.drawText(lcd.getLastPos(), start_y + 4, 'V',0+mode)
  -- lcd.drawText(lcd.getLastPos(), start_y, 'v',MIDSIZE+mode)
end
local function drawVoltageImage(start_x, start_y)
  -- Define the battery width (so we can adjust it later)
  local batteryWidth = 12 

  -- read cell value
  local voltage = getValue(VoltageSourceId)

  if voltage>=4.21 then		-- si celda en algun momento esta por encima de 4.21 supondremos que es bateria LIHV
    LIHV = 1
  end

  -- Draw our battery outline
  lcd.drawLine(start_x + 2, start_y + 1, start_x + batteryWidth - 2, start_y + 1, SOLID, 0)
  lcd.drawLine(start_x, start_y + 2, start_x + batteryWidth - 1, start_y + 2, SOLID, 0)
  lcd.drawLine(start_x, start_y + 2, start_x, start_y + 50, SOLID, 0)
  lcd.drawLine(start_x, start_y + 50, start_x + batteryWidth - 1, start_y + 50, SOLID, 0)
  lcd.drawLine(start_x + batteryWidth, start_y + 3, start_x + batteryWidth, start_y + 49, SOLID, 0)

  -- top one eighth line
  lcd.drawLine(start_x + batteryWidth - math.ceil(batteryWidth / 4), start_y + 8, start_x + batteryWidth - 1, start_y + 8, SOLID, 0)
  -- top quarter line
  lcd.drawLine(start_x + batteryWidth - math.ceil(batteryWidth / 2), start_y + 14, start_x + batteryWidth - 1, start_y + 14, SOLID, 0)
  -- third eighth line
  lcd.drawLine(start_x + batteryWidth - math.ceil(batteryWidth / 4), start_y + 20, start_x + batteryWidth - 1, start_y + 20, SOLID, 0)
  -- Middle line
  lcd.drawLine(start_x + 1, start_y + 26, start_x + batteryWidth - 1, start_y + 26, SOLID, 0)
  -- five eighth line
  lcd.drawLine(start_x + batteryWidth - math.ceil(batteryWidth / 4), start_y + 32, start_x + batteryWidth - 1, start_y + 32, SOLID, 0)
  -- bottom quarter line
  lcd.drawLine(start_x + batteryWidth - math.ceil(batteryWidth / 2), start_y + 38, start_x + batteryWidth - 1, start_y + 38, SOLID, 0)
  -- seven eighth line
  lcd.drawLine(start_x + batteryWidth - math.ceil(batteryWidth / 4), start_y + 44, start_x + batteryWidth - 1, start_y + 44, SOLID, 0)

  if (LIHV == 1) then  -- if LIHV
    -- Voltage top
    lcd.drawText(start_x + batteryWidth + 4, start_y + 0, "4.35v", SMLSIZE)
    -- Voltage middle
    lcd.drawText(start_x + batteryWidth + 4, start_y + 24, "3.85v", SMLSIZE)
    -- Voltage bottom
    lcd.drawText(start_x + batteryWidth + 4, start_y + 47, "3.35v", SMLSIZE)
    voltageLow = 3.35
    voltageHigh = 4.35
  else  -- if LIPO
    -- Voltage top
    lcd.drawText(start_x + batteryWidth + 4, start_y + 0, "4.2v", SMLSIZE)
    -- Voltage middle
    lcd.drawText(start_x + batteryWidth + 4, start_y + 24, "3.7v", SMLSIZE)
    -- Voltage bottom
    lcd.drawText(start_x + batteryWidth + 4, start_y + 47, "3.2v", SMLSIZE)
    voltageLow = 3.2
    voltageHigh = 4.2
  end
 
  -- Now draw how full our voltage is...
  voltageIncrement = ((voltageHigh - voltageLow) / 47)
  
  local offset = 0  -- Start from the bottom up
  while offset < 47 do
    if ((offset * voltageIncrement) + voltageLow) < tonumber(voltage) then
      lcd.drawLine( start_x + 1, start_y + 49 - offset, start_x + batteryWidth - 1, start_y + 49 - offset, SOLID, 0)
    end
    offset = offset + 1
  end
end
local function drawValueH(starx,stary,desc,value,unit)
  lcd.drawText(starx, stary+4, desc,SMLSIZE)
  local mode=0
  if rssi==0 then
    mode=BLINK+INVERS
  end
  lcd.drawText(lcd.getLastPos(), stary, value,MIDSIZE+mode)
  lcd.drawText(lcd.getLastPos(), stary+4, unit, mode)
end
local function drawValueV(starx,stary,desc,value,unit)
  lcd.drawText(starx, stary, desc,SMLSIZE)
  local mode=0
  if rssi==0 then
    mode=BLINK+INVERS
  end
  lcd.drawText(starx, stary+8, value,MIDSIZE+mode)
  if(unit=="º") then
    lcd.drawText(lcd.getLastPos(), stary+9, "o", SMLSIZE+mode)
  elseif (#unit<=2) then
    lcd.drawText(lcd.getLastPos(), stary+12, unit, mode)
  else
    lcd.drawText(lcd.getLastPos(), stary+13, unit, SMLSIZE+mode)
  end
end
local function drawValuesBigScreen(starx,stary,threecols)
    local nextx=43
    local nexty=24
    local step=stepsTime(200)

    if (GPSId == -1) then
      drawValueV(starx,stary,"Vol-:",string.format("%4.2f", volmin),"V") -- voltage
      drawValueV(starx+nextx,stary+nexty,"Fuel: ",string.format("%2.0f", getValue(FuelId)),"mah") -- si es peque
    else
      drawValueV(starx,stary,"Fuel:",string.format("%2.0f", getValue(FuelId)),"mah") -- si es grande
      if (threecols==1) then
        drawValueV(starx+nextx,stary+nexty,"Fuel:",string.format("%2.0f", getValue(FuelId)),"mah") -- si es peque
      else
        drawValueV(starx+nextx,stary+nexty,"Sats:",getGpsSat(),"")   -- gps
      end
    end

    if (step==1 or GPSId==-1) then
      lcd.drawText(starx,stary+nexty,"Vol:",SMLSIZE)
      drawVoltageText(starx,stary+nexty+8)
      drawValueV(starx+nextx,stary,"Curr:",string.format("%3.1f", getValue(CurrId)),"A")
    else
      drawValueV(starx,stary+nexty,"Vol-:",string.format("%4.2f", volmin),"V") -- voltage
      drawValueV(starx+nextx,stary,"Curr+:",string.format("%3.1f", currmax),"A") -- consumo
    end

    if threecols == 1 then
      if (AltId == -1) then	-- si no tienen altitud imprimir heading (de relleno)
        drawValueV(starx+nextx+nextx,stary,"Hdg: ",string.format("%3.0f", getValue(HdgId)),"º")
      else
        drawValueV(starx+nextx+nextx,stary,"Alt: ",string.format("%5.1f", getValue(AltId)),"m")
      end

      if (GSpdId == -1) then	-- si no tienen gps poner inclinacion
        -- drawValueV(starx+nextx,stary+nexty,"Vol:",string.format("%5.2f", getValue(VFASId)),"V")
        drawValueV(starx+nextx+nextx,stary+nexty,"Pitch: ",string.format("%3.0f", getValue(PitchId)/10),"º")
      else
        drawValueV(starx+nextx+nextx,stary+nexty,"Spd: ",string.format("%2.0f", getValue(GSpdId)),"km/h")
      end
    end
end
local function drawValuesSmallScreen(starx,stary)
    local nexty=24
    local step=stepsTime(200)
    if (step == 1) then
      drawValueV(starx,stary,"Fuel: ",string.format("%2.0f", getValue(FuelId)),"mah") -- consumo
      drawValueV(starx,stary+nexty,"Vol:",string.format("%4.2f", getVoltage()),"V") -- voltage
    else
      drawValueV(starx,stary,"Curr: ",string.format("%3.1f", getValue(CurrId)),"A") -- consumo
      drawValueV(starx,stary+nexty,"Vol-:",string.format("%4.2f", volmin),"V") -- voltage minimo
    end
end
local function drawHeader(title)
  lcd.drawLine(0, 7, MY_LCD_W, 7, SOLID, FORCE)
  local currentVoltage = getValue('tx-voltage')
  drawTransmitterVoltage(0,0, currentVoltage)
  lcd.drawText( MY_LCD_W/2 - math.ceil((#title * 5) / 2)+9,0, title, SMLSIZE)
  drawTime(MY_LCD_W-21,0)
  drawHeaderSignal(MY_LCD_W-30,0)
end
local function gatherInput(event)
  if event == 131 then
    killEvents(131)
  elseif event == 99 then
    screen = defaultscreen		-- si cambiamos de pantalla ir a pantalla 0
  elseif event == 96 then
    screen = screen + 1		-- change screen
    if(screen>=maxscreen) then
      screen = 0
    end

  elseif event == EVT_PLUS_FIRST then
    screen = screen + 1		-- change screen
    if(screen>=maxscreen) then
      screen = 0
    end
    killEvents(EVT_PLUS_FIRST)

  elseif event == EVT_MINUS_FIRST then
    screen = screen - 1		-- change screen
    if(screen<0) then
      screen = maxscreen - 1
    end
    killEvents(EVT_MINUS_FIRST)
  end

end
local function screenprearm()
  setAnimationIncrement()
  isArmed = getArmed()
  --drawHeader(modelName .. " Prearm")
  modeText = getModeText()
  lcd.drawText( 60 - math.ceil((#modeText * 5) / 2),9, modeText, SMLSIZE)
  drawQuadcopter(42, 16)
  drawFlightTimer(MY_LCD_W-44, 34)
  drawRSSI(MY_LCD_W-44, 8)
  if(MY_LCD_W<=128) then
    drawVoltageText(41,50)
  end
  --drawVoltageImage(3, 10)

  -- Draw some other information
  --if(MY_LCD_W>128) then
    --drawValuesBigScreen(83,12,0)
  --end

  drawStatus()

  -- 3 segundos despues de armar cambiar a pantalla 1
  if(isArmed == 1) then
    if (timearmed ~= 0 and getTime()-timearmed>300) then
      screen=1
      timearmed=0
    end
  else
    timearmed=getTime()
  end
end
local function screenarm()

  drawHeader(modelName .. " - Arm")
  
  -- Draw our flight timer
  drawFlightTimer(MY_LCD_W-44, 34)
  
  -- Draw RSSI
  drawRSSI(MY_LCD_W-44, 8)
  
  -- Draw voltage battery graphic
  --drawVoltageImage(3, 10)

  -- Draw some other information
  if(MY_LCD_W>128) then
    drawValuesBigScreen(40,12,1)
  else
    drawValuesSmallScreen(40,12)
  end

  -- drawHorizon(126,47,10)

  drawStatus()

  -- 5 segundos despues de desarmar cambiar a pantalla 0
  isArmed = getArmed()
  if (isArmed == 0) then
    if (timeunarmed ~= 0 and getTime()-timeunarmed>500) then
      screen=0
      timeunarmed=0
    end
  else
    timeunarmed=getTime()
  end
end
local function screenrecover()
  drawHeader("Drone Recover")

  drawValueH(8, 12, "RSSI: ",rssi,"")

  local voltage=getVoltage()

  local mode=0
  if LIHV and voltage<=3.3 then
    mode=BLINK+INVERS
  end
  if LIHV==0 and voltage<=3.45 then
    mode=BLINK+INVERS
  end
  drawValueH(8, 24, "Vol:", string.format("%5.2f", voltage),"V")

  if (GPSId ~= -1) then
    local url = ""
    if lat ~= "" then
      --url = "HTTPS://[Log in to view URL]" .. lat .. "," .. lon .. ",99m/data=!3m1!1e3"
      url = lat .. "," .. lon
    end

    if HomeDistanceId ~= -1 then
      drawValueH(MY_LCD_W/2+2, 36, "Dis: ",string.format("%3.0f", dist),"")
    end

    if type(pos) == "table" then
      lcd.drawText(MY_LCD_W/2+2, 12+4, "Lat: ", SMLSIZE)	-- si hay posicion gps (hay cobertura)
      lcd.drawText(lcd.getLastPos(), 12, lat, MIDSIZE)
      lcd.drawText(MY_LCD_W/2+2, 24+4, "Lon: ", SMLSIZE)
      lcd.drawText(lcd.getLastPos(), 24, lon, MIDSIZE)
    else						-- si no hay posicion gps (sin cobertura o sin telemetria)
      lcd.drawText(MY_LCD_W/2+2, 12+4, "Lat: ", SMLSIZE)
      lcd.drawText(lcd.getLastPos(), 12, lat,MIDSIZE+BLINK+INVERS)
      lcd.drawText(MY_LCD_W/2+2, 24+4, "Lon: ", SMLSIZE)
      lcd.drawText(lcd.getLastPos(), 24, lon,MIDSIZE+BLINK+INVERS)
    end

    -- draw qr with the url
    -- lcd.drawRectangle(100, 6, 37, 37, SOLID)
    -- pruebaQR(102,3,table_pruebaQR)
  
    if url ~= "" then
      -- draw qr
      -- print url
      -- lcd.drawText(0, 36, string.sub(url,1,208/8),0)
      -- lcd.drawText(0, 50, string.sub(url,208/8+1),MIDSIZE)
      lcd.drawText(0, 44, "Search in Google Maps:",SMLSIZE)
      lcd.drawText(0, 56, "GEO: ",SMLSIZE)
      lcd.drawText(lcd.getLastPos(), 52, url,MIDSIZE)
    end
  else
    lcd.drawText(0, 44, "Your Quad doesn't have GPS",SMLSIZE)
  end
end
local function screenhelp()
  drawHeader("Help")
  HelpScreen()  -- screen2 is help screen
end 

-- run
local function run(event)
  if (mod(getModeTelemetry(),10)==2) then
    status = "Arming disabled"
  else
    status = ""
  end
  rssi = getRSSI()
  timerLeft = getValue('timer1')
  lcd.clear()
  gatherInput(event)
 
  if (screen==1) then
    screenprearm()
    --screenarm()
  elseif (screen==2) then
    screenrecover()
  elseif (screen==3) then
    screenhelp()
  else
    screenprearm()
  end

  return 0
end


local function getTelemetryId(name)
    field = getFieldInfo(name)
    if field then
      return field.id
    else
      return -1
    end
end
local function background()
  -- actualizar distancia de home al drone
  if HomeDistanceId ~= -1 then
    local distnow=getValue(HomeDistanceId)
    if distnow ~= 0 then
      dist = distnow
    end
  end
  -- guardar lat y lon
  if (GPSId ~= -1) then
    local pos = getValue(GPSId)
    if type(pos) == "table" then
      lat = pos["lat"]
      lon = pos["lon"]
    end
  end
  -- si se cambia bateria por una nueva resetear timer 2
  local v = getVoltage()
  if (v == 0) then
    voltzero = 1
  else
    if (voltzero == 1 and v > voltage + 0.3) then
      model.resetTimer(0)	-- resetear timer 1 (que es el 0 :-P
    end
    voltage = v
    volzero = 0
  end
  -- actualizar el voltaje minimo (si armado)
  -- v tiene el valor del voltaje ya
  if (volmin==0) then	-- si es 0 poneos el actual
    volmin = v
  end
  local armed = getArmed()
  local a = getValue(CurrId)
  if armed == 1 then
    if volminreset == 1 or volmin > v then
       volmin = v
    end
    if volminreset == 1 or currmax < a then
       currmax = a
    end

    volminreset = 0
  else
    volminreset = 1
  end
end
local function init_func()
  -- Called once when model is loaded, only need to get model name once...
  local modeldata = model.getInfo()
  if modeldata then
    modelName = modeldata['name']
  end

  local settings = getGeneralSettings()		-- obtener bateria minima y maxima
  lowVoltage = settings['battMin']
  highVoltage = settings['battMax']

  screen = defaultscreen		-- iniciar en pantalla por defecto

  VoltageSourceId = getTelemetryId('A4')	-- indicadores de telemetría
  GPSId = getTelemetryId("GPS")
  GSpdId = getTelemetryId("GSpd")
  GpsStatusId = getTelemetryId("Tmp2")
  HomeDistanceId = getTelemetryId("0420")
  VFASId = getTelemetryId("VFAS")
  AltId = getTelemetryId("Alt")
  CurrId = getTelemetryId("Curr")
  CelsId = getTelemetryId("Cels")
  FuelId = getTelemetryId("Fuel")
  HdgId = getTelemetryId("Hdg")
  PitchId = getTelemetryId("5230")
  RollId = getTelemetryId("5240")

  ModeId = getTelemetryId("Mode")		-- buscamos primero si hemos renombrado Tmp1 a Mode
  if(ModeId==-1) then				-- y sino buscamos el Tmp1
    ModeId = getTelemetryId("Tmp1")		-- en teoria no se pueden leer varios registros con mismo nombre
  end

  MY_LCD_W = LCD_W		-- usamos esto por si queremos probar en X9D como se vería en una X7
end
return { run=run, init=init_func, background=background  }


Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: