/* *****************************************************************************
 * File:      screen.c
 * Date:      25 Mar 2026
 * Author:    Andrew Levido
 *
 * Copyright 2026 Imbrius Pty Ltd - All Rights Reserved
 *
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "screen.h"
#include "graphics.h"
#include "resources.h"


/* Defines -------------------------------------------------------------------*/
#define SCRN_WIDTH          240
#define SCRN_HEIGHT         320
#define SCRN_TITLE_Y        30
#define SCRN_IMAGE_Y        75
#define SCRN_LINE1_Y        270
#define SCRN_LINE2_Y        300
#define SCRN_IMAGE_LOW_Y    90

/* Typedefs & Enumerations ---------------------------------------------------*/

/* Global Variables ----------------------------------------------------------*/
// Splash Screen - - - - - - - - - - - - - - - 
const screen_text_t scrn_splash_title = {
  .text = "Semiconductor Analyser",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_splash_version = {
  .text = "Version 0.0a",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 280,
};

const screen_text_t scrn_splash_message = {
  .text = "Copyright 2026",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 310,
};

const screen_image_t scrn_splash_logo_outer = {
  .image = &logo_1_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_LOW_Y,
};

const screen_image_t scrn_splash_logo_inner = {
  .image = &logo_2_image,
  .colour = GFX_BLUE,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_LOW_Y + 29,
};

const screen_t scrn_splash = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_splash_title,
  .image1 = &scrn_splash_logo_outer,
  .image2 = &scrn_splash_logo_inner,
  .text1 = &scrn_splash_version,
  .text2 = &scrn_splash_message,
  .top_node = NULL,
  .left_node = NULL,
  .bottom_node = NULL,
};
// Wait Screen - - - - - - - - - - - - - - - 
const screen_text_t scrn_wait_title = {
  .text = "ANALYSING",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_wait_wait = {
  .text = "...Please Wait...",
  .colour = GFX_BLUE,
  .x = SCRN_CENTRED,
  .y = 280,
};

const screen_image_t scrn_wait_clock = {
  .image = &clock_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_LOW_Y,
};

const screen_image_t scrn_wait_hands = {
  .image = &hands_image,
  .colour = GFX_RED,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_LOW_Y + 27,
};

const screen_t scrn_wait = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_wait_title,
  .image1 = &scrn_wait_clock,
  .image2 = &scrn_wait_hands,
  .text1 = &scrn_wait_wait,
  .text2 = NULL,
  .top_node = NULL,
  .left_node = NULL,
  .bottom_node = NULL,
};

// Error Screen. - - - - - - - - - - - - - - - - 
const screen_text_t scrn_error_title = {
  .text = "ERROR",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_error_error = {
  .text = "Unknown or Bad Part",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 280,
};
const screen_text_t scrn_error_error2 = {
  .text = " ",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 310,
};


const screen_image_t scrn_error_warning = {
  .image = &warning_image,
  .colour = GFX_RED,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_LOW_Y,
};

const screen_image_t scrn_error_exclaimation = {
  .image = &exclaimation_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_LOW_Y + 45,
};

const screen_t scrn_error = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_error_title,
  .image1 = &scrn_error_warning,
  .image2 = &scrn_error_exclaimation,
  .text1 = &scrn_error_error,
  .text2 = &scrn_error_error2,
  .top_node = NULL,
  .left_node = NULL,
  .bottom_node = NULL,
};

// Diode Screens  - - - - - - - - - - - - - - - - - - - - - - - - - - -
const screen_text_t scrn_diode_title = {
  .text = "DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_schottky_title = {
  .text = "SCHOTTKY DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_led_title = {
  .text = "LED",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_zener_title = {
  .text = "ZENER DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_led_b2b_title = {
  .text = "BACK TO BACK LEDs",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_diode_b2b_title = {
  .text = "BACK TO BACK DIODES",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_diode_vf = {
  .text = "Vfwd = 0.625 V",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 280,
};

const screen_text_t scrn_diode_vr = {
  .text = "Vrev = 5.18 V",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 310,
};

const screen_image_t scrn_diode_image = {
  .image = &diode_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_schottky_image = {
  .image = &schottky_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_led_image = {
  .image = &led_image,
  .colour = GFX_BLACK,
  .x = 68,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_zener_image = {
  .image = &zener_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_led_b2b_image = {
  .image = &led_b2b_image,
  .colour = GFX_BLACK,
  .x = 62,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_diode_b2b_image = {
  .image = &diode_b2b_image,
  .colour = GFX_BLACK,
  .x = 87,
  .y = SCRN_IMAGE_Y,
};

const screen_node_t scrn_diode_top_node = {
  .x = 111,
  .y = 50,
};

const screen_node_t scrn_diode_bottom_node = {
  .x = 111,
  .y = 225,
};

const screen_t scrn_diode = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_diode_title,
  .image1 = &scrn_diode_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf ,
  .text2 = NULL,
  .top_node = &scrn_diode_top_node,
  .left_node = NULL,
  .bottom_node = &scrn_diode_bottom_node,
};


const screen_t scrn_schottky = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_schottky_title,
  .image1 = &scrn_schottky_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf ,
  .text2 = NULL,
  .top_node = &scrn_diode_top_node,
  .left_node = NULL,
  .bottom_node = &scrn_diode_bottom_node,
};

const screen_t scrn_led = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_led_title,
  .image1 = &scrn_led_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf ,
  .text2 = NULL,
  .top_node = &scrn_diode_top_node,
  .left_node = NULL,
  .bottom_node = &scrn_diode_bottom_node,
};

const screen_t scrn_zener = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_zener_title,
  .image1 = &scrn_zener_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf ,
  .text2 = &scrn_diode_vr,
  .top_node = &scrn_diode_top_node,
  .left_node = NULL,
  .bottom_node = &scrn_diode_bottom_node,
};

const screen_t scrn_led_b2b= {
  .bg_colour = GFX_WHITE,
  .title = &scrn_led_b2b_title,
  .image1 = &scrn_led_b2b_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf ,
  .text2 = &scrn_diode_vr,
  .top_node = &scrn_diode_top_node,
  .left_node = NULL,
  .bottom_node = &scrn_diode_bottom_node,
};

const screen_t scrn_diode_b2b= {
  .bg_colour = GFX_WHITE,
  .title = &scrn_diode_b2b_title,
  .image1 = &scrn_diode_b2b_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf ,
  .text2 = &scrn_diode_vr,
  .top_node = &scrn_diode_top_node,
  .left_node = NULL,
  .bottom_node = &scrn_diode_bottom_node,
};

// Triode Screens  - - - - - - - - - - - - - - - - - - - - - - - - - - -
const screen_text_t scrn_bjt_npn_title = {
  .text = "BJT - NPN",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_bjt_pnp_title = {
  .text = "BJT - PNP",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_mosfet_nch_title = {
  .text = "MOSFET - NCh",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_mosfet_depletion_title = {
  .text = "DEPLETION MOSFET",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};


const screen_text_t scrn_mosfet_pch_title = {
  .text = "MOSFET - PCh",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_igbt_title = {
  .text = "IGBT",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_igbt_diode_title = {
  .text = "IGBT + DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_bjt_npn_diode_title = {
  .text = "BJT NPN + DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_bjt_pnp_diode_title = {
  .text = "BJT PNP + DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_darlington_npn_title = {
  .text = "DARLINGTON NPN ",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_darlington_pnp_title = {
  .text = "DARLINGTON PNP",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_darlington_npn_diode_title = {
  .text = "DARLINGTON NPN + DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_darlington_pnp_diode_title = {
  .text = "DARLINGTON PNP + DIODE",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_thyristor_title = {
  .text = "THYRISTOR",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_triac_title = {
  .text = "TRIAC",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};


const screen_text_t scrn_jfet_nch_title = {
  .text = "J-FET Nch",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_jfet_pch_title = {
  .text = "J-FET Pch",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_diode_pair_title = {
  .text = "DIODE PAIR",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_led_pair_title = {
  .text = "LED PAIR",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_text_t scrn_schottky_pair_title = {
  .text = "SCHOTTKY DIODE PAIR",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_TITLE_Y,
};

const screen_image_t scrn_bjt_npn_image = {
  .image = &bjt_npn_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_bjt_npn_diode_image = {
  .image = &bjt_npn_diode_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_darlington_npn_image = {
  .image = &darlington_npn_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_darlington_npn_diode_image = {
  .image = &darlington_npn_diode_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_bjt_pnp_image = {
  .image = &bjt_pnp_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_bjt_pnp_diode_image = {
  .image = &bjt_pnp_diode_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_darlington_pnp_image = {
  .image = &darlington_pnp_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_darlington_pnp_diode_image = {
  .image = &darlington_pnp_diode_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_mosfet_nch_image = {
  .image = &mosfet_nch_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_mosfet_pch_image = {
  .image = &mosfet_pch_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

  const screen_image_t scrn_jfet_nch_image = {
  .image = &jfet_nch_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_jfet_pch_image = {
  .image = &jfet_pch_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_depletion_mosfet_image = {
  .image = &mosfet_depletion_nch_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};
const screen_image_t scrn_igbt_image = {
  .image = &igbt_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_igbt_diode_image = {
  .image = &igbt_diode_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_diode_pair_aa_image = {
  .image = &diode_pair_aa_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_led_pair_aa_image = {
  .image = &led_pair_aa_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_schottky_pair_aa_image = {
  .image = &schottky_pair_aa_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_schottky_pair_ak_image = {
  .image = &schottky_pair_ak_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_diode_pair_ak_image = {
  .image = &diode_pair_ak_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_diode_pair_kk_image = {
  .image = &diode_pair_kk_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_led_pair_kk_image = {
  .image = &led_pair_kk_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_schottky_pair_kk_image = {
  .image = &schottky_pair_kk_image,
  .colour = GFX_BLACK,
  .x = 75,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_thyristor_image = {
  .image = &thyristor_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};

const screen_image_t scrn_triac_image = {
  .image = &triac_image,
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = SCRN_IMAGE_Y,
};
const screen_text_t scrn_bjt_vbe = {
  .text = "Vbe = 0.62 V",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 280,
};

const screen_text_t scrn_bjt_hfe = {
  .text = "hFE = 219",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 310,
};

const screen_text_t scrn_fet_vgs = {
  .text = "Vgs(th) = 1.87 V",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 280,
};
const screen_text_t scrn_fet_id = {
  .text = "@ = 1.87 mA",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 310,
};

const screen_text_t scrn_diode_vf1 = {
  .text = "Vf1 = 0.62 V",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 280,
};

const screen_text_t scrn_diode_vf2 = {
  .text = "Vf2 = 0.760 V",
  .colour = GFX_BLACK,
  .x = SCRN_CENTRED,
  .y = 310,
};
const screen_node_t scrn_bjt_top_node = {
  .x = 151,
  .y = 50,
};

const screen_node_t scrn_bjt_left_node = {
  .x = 50,
  .y = 137,
};

const screen_node_t scrn_darlington_npn_left_node = {
  .x = 50,
  .y = 127,
};

const screen_node_t scrn_thyristor_top_node = {
  .x = 145,
  .y = 50,
};
const screen_node_t scrn_thyristor_left_node = {
  .x = 50,
  .y = 160,
};
const screen_node_t scrn_thyristor_bottom_node = {
  .x = 145,
  .y = 225,
};

const screen_node_t scrn_darlington_pnp_left_node = {
  .x = 50,
  .y = 144,
};
const screen_node_t scrn_bjt_bottom_node = {
  .x = 151,
  .y = 225,
};

const screen_t scrn_bjt_npn = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_bjt_npn_title,
  .image1 = &scrn_bjt_npn_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_bjt_npn_diode = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_bjt_npn_diode_title,
  .image1 = &scrn_bjt_npn_diode_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_darlington_npn = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_darlington_npn_title,
  .image1 = &scrn_darlington_npn_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_darlington_npn_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_darlington_npn_diode = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_darlington_npn_diode_title,
  .image1 = &scrn_darlington_npn_diode_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_darlington_npn_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_bjt_pnp = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_bjt_pnp_title,
  .image1 = &scrn_bjt_pnp_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_bjt_pnp_diode = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_bjt_pnp_diode_title,
  .image1 = &scrn_bjt_pnp_diode_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_darlington_pnp = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_darlington_pnp_title,
  .image1 = &scrn_darlington_pnp_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_darlington_pnp_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_darlington_pnp_diode = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_darlington_pnp_diode_title,
  .image1 = &scrn_darlington_pnp_diode_image,
  .image2 = NULL,
  .text1 = &scrn_bjt_vbe,
  .text2 = &scrn_bjt_hfe,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_darlington_pnp_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_mosfet_nch = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_mosfet_nch_title,
  .image1 = &scrn_mosfet_nch_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = &scrn_fet_id,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_mosfet_pch = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_mosfet_pch_title,
  .image1 = &scrn_mosfet_pch_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = &scrn_fet_id,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_jfet_nch = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_jfet_nch_title,
  .image1 = &scrn_jfet_nch_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = NULL,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_jfet_pch = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_jfet_pch_title,
  .image1 = &scrn_jfet_pch_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = &scrn_fet_id,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_depletion_mosfet_nch = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_mosfet_depletion_title,
  .image1 = &scrn_depletion_mosfet_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = &scrn_fet_id,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_igbt = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_igbt_title,
  .image1 = &scrn_igbt_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = &scrn_fet_id,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_igbt_diode = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_igbt_diode_title,
  .image1 = &scrn_igbt_diode_image,
  .image2 = NULL,
  .text1 = &scrn_fet_vgs,
  .text2 = &scrn_fet_id,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_diode_pair_aa = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_diode_pair_title,
  .image1 = &scrn_diode_pair_aa_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_led_pair_aa = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_led_pair_title,
  .image1 = &scrn_led_pair_aa_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_schottky_pair_aa = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_schottky_pair_title,
  .image1 = &scrn_schottky_pair_aa_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_diode_pair_ak = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_diode_pair_title,
  .image1 = &scrn_diode_pair_ak_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_schottky_pair_ak = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_diode_pair_title,
  .image1 = &scrn_schottky_pair_ak_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_diode_pair_kk = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_diode_pair_title,
  .image1 = &scrn_diode_pair_kk_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_led_pair_kk = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_led_pair_title,
  .image1 = &scrn_led_pair_kk_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_schottky_pair_kk = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_schottky_pair_title,
  .image1 = &scrn_schottky_pair_kk_image,
  .image2 = NULL,
  .text1 = &scrn_diode_vf1,
  .text2 = &scrn_diode_vf2,
  .top_node = &scrn_bjt_top_node,
  .left_node = &scrn_bjt_left_node,
  .bottom_node = &scrn_bjt_bottom_node,
};

const screen_t scrn_thyristor = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_thyristor_title,
  .image1 = &scrn_thyristor_image,
  .image2 = NULL,
  .text1 = NULL,
  .text2 = NULL,
  .top_node = &scrn_thyristor_top_node,
  .left_node = &scrn_thyristor_left_node,
  .bottom_node = &scrn_thyristor_bottom_node,
};

const screen_t scrn_triac = {
  .bg_colour = GFX_WHITE,
  .title = &scrn_triac_title,
  .image1 = &scrn_triac_image,
  .image2 = NULL,
  .text1 = NULL,
  .text2 = NULL,
  .top_node = &scrn_thyristor_top_node,
  .left_node = &scrn_thyristor_left_node,
  .bottom_node = &scrn_thyristor_bottom_node,
};

/* Private Functions ---------------------------------------------------------*/

 void scrn_write_text(const screen_text_t * text, char * override)
 {
  uint16_t current_colour;
  
  if(override == NULL) {
    override = (char*)text->text;
  }

  if(text != NULL) {
    current_colour = gfx_get_fg_colour();
    if(text->colour != current_colour) {
      gfx_set_fg_colour(text->colour);
    }
    if(text->x == SCRN_CENTRED) {
      gfx_draw_text_centred(override, text->y, &roboto_bold_20);
    }
    else {
      gfx_draw_text(override, text->x, text->y, &roboto_bold_20);
    }
    if(text->colour != current_colour) {
      gfx_set_fg_colour(current_colour);
    }
    
  }
 }

 void scrn_draw_image (const screen_image_t * image)
 {
   uint16_t current_colour;

   if(image != NULL) {
    current_colour = gfx_get_fg_colour();
    if(image->colour != current_colour) {
      gfx_set_fg_colour(image->colour);
    }
    if(image->x == SCRN_CENTRED) {
      gfx_draw_image_centred(image->y, image->image);
    }
    else {
      gfx_draw_image(image->x, image->y, image->image);
    }
    if(image->colour != current_colour) {
      gfx_set_fg_colour(current_colour);
    }
  }
 }

  void scrn_draw_node(const screen_node_t * node, scrn_node_id_t id)
  {
    uint16_t current_colour;

    if(node == NULL) { return; }

    current_colour = gfx_get_fg_colour();
    switch(id) {
      case SCRN_NODE_RED:
        gfx_set_fg_colour(GFX_RED);
        gfx_draw_image(node->x, node->y, &red_term_image);
        gfx_set_fg_colour(current_colour);
        break;
      case SCRN_NODE_GRN:
        gfx_set_fg_colour(GFX_GREEN);
        gfx_draw_image(node->x, node->y, &green_term_image);
        gfx_set_fg_colour(current_colour);
        break;
      case SCRN_NODE_BLU:
        gfx_set_fg_colour(GFX_BLUE);
        gfx_draw_image(node->x, node->y, &blue_term_image);
        gfx_set_fg_colour(current_colour);
        break;
      case SCRN_NODE_NONE:
      default:
        break;
    }


  }
/* Public Functions ----------------------------------------------------------*/


void scrn_draw(screen_desc_t * desc)

{
  if(desc->screen == NULL) { return; }
  
  // Turn screen off
  gfx_screen_off();

  // Clear everything
  gfx_draw_block(0, 0, SCRN_WIDTH, SCRN_HEIGHT, desc->screen->bg_colour);

  // Draw title
  scrn_write_text(desc->screen->title, NULL);

  // Draw text 1
  scrn_write_text(desc->screen->text1, desc->string1_override);

  // Draw text 2
  scrn_write_text(desc->screen->text2, desc->string2_override);
  
  // Draw image 1
  scrn_draw_image(desc->screen->image1);

  // Draw image 2
  scrn_draw_image(desc->screen->image2);

  // Draw top_node
  scrn_draw_node(desc->screen->top_node, desc->top_node_id);

  // Draw left node
  scrn_draw_node(desc->screen->left_node, desc->left_node_id);

  // Draw bottom node
  scrn_draw_node(desc->screen->bottom_node, desc->bottom_node_id);

  // Turn screen on
  gfx_screen_on();

}
/* Interrupt Service Routines ------------------------------------------------*/

/* End screen */