Add indications for opening and closing FETs
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 19 Feb 2024 22:12:48 +0000 (23:12 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 19 Feb 2024 22:12:48 +0000 (23:12 +0100)
inc/FET_sim.h
src/colors.c
src/sim_main.c

index 285a7a243894d1d6bc9a45482f9e2e5ed7e4e8e6..6a6bccf94024dfc3c4ca08438d696100d7644d52 100644 (file)
@@ -106,6 +106,7 @@ const char  *state_color_escape(t_state state);
 void           draw_single(t_vec *mosfets, size_t i);
 
 void   update_nodes(t_vec *nodes);
+int            should_open(t_type type, t_state state);
 int            sim_step(t_vec *nodes, t_vec *mosfets);
 
 void   print_start(void);
index 343ac73bdc50bb09fe69c6e3af1a4e7817a3d99e..060031e242960924287bff1ab97fb243b2b57229 100644 (file)
@@ -37,10 +37,14 @@ void        draw_single(t_vec *mosfets, size_t i)
        }
        if (mosfet->gate)
        {
-               if (!mosfet->is_opened)
-                       ft_printf("%4u%s-%s%c-\n", mosfet->gate->id, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type);
-               else
+               if (mosfet->is_opened && should_open(mosfet->type, mosfet->gate->state))
                        ft_printf("%4u%s-%s%c%s|%s\n", mosfet->gate->id, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type, state_color_escape(mosfet->source->state), g_default_ac);
+               else if (should_open(mosfet->type, mosfet->gate->state))
+                       ft_printf("%4u%s-%s%c\\\n", mosfet->gate->id, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type);
+               else if (mosfet->is_opened)
+                       ft_printf("%4u%s-%s%c%s\\%s\n", mosfet->gate->id, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type, state_color_escape(mosfet->source->state), g_default_ac);
+               else
+                       ft_printf("%4u%s-%s%c-\n", mosfet->gate->id, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type);
        }
        else
                ft_printf("%sNULL--%s%c\n", g_red_ac, g_default_ac, mosfet->type);
index 37929e6b13d76f0d224056e6653155ca829f2303..54e35f2a31e7942afcf481f1da66de165154e2d8 100644 (file)
@@ -1,23 +1,20 @@
 #include "FET_sim.h"
 #include "libft.h"
 
+int    should_open(t_type type, t_state state)
+{
+       return ((type == p && state == on) || (type == n && state == off));
+}
+
 // Maybe an error should be return when gate is NULL
 static void    update_mosfet(t_mosfet *mosfet)
 {
-       t_state state;
-
        if (!mosfet->gate)
        {
                mosfet->is_opened = 0;
                return ;
        }
-       state = mosfet->gate->state;
-       if (mosfet->type == p && state == on)
-               mosfet->is_opened = 1;
-       else if (mosfet->type == n && state == off)
-               mosfet->is_opened = 1;
-       else
-               mosfet->is_opened = 0;
+       mosfet->is_opened = should_open(mosfet->type, mosfet->gate->state);
        return ;
 }
 
@@ -38,7 +35,7 @@ static void   update_mosfets(t_vec *mosfets)
 
 int    sim_step(t_vec *nodes, t_vec *mosfets)
 {
-       update_nodes(nodes);
        update_mosfets(mosfets);
+       update_nodes(nodes);
        return (1);
 }