{"id":7078,"date":"2022-02-02T08:26:22","date_gmt":"2022-02-02T08:26:22","guid":{"rendered":"http:\/\/fastbitlab.com\/?p=7078"},"modified":"2023-08-08T15:16:47","modified_gmt":"2023-08-08T09:46:47","slug":"fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1","status":"publish","type":"post","link":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/","title":{"rendered":"FSM Lecture 24: Exercise-003 Nested switch implementation of an FSM part 1"},"content":{"rendered":"<div class=\"boldgrid-section\" style=\"background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"container\">\n<div class=\"row\" style=\"padding-top: 35px; padding-bottom: 0px; background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"col-md-1 col-sm-12 col-xs-12 col-lg-1\">\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"col-md-10 col-sm-12 col-xs-12 col-lg-10\">\n<h1 class=\"\" style=\"text-align: center; font-size: 30px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; line-height: 50px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><strong><span style=\"color: #000080;\">Exercise-003 Nested switch implementation of an FSM part 1<\/span><\/strong><\/h1>\n<div class=\"row bg-editor-hr-wrap\" style=\"border-width: 0px; margin-top: -25px;\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\">\n<div>\n<p>&nbsp;<\/p>\n<div class=\"bg-hr bg-hr-10 color2-color\" style=\"border-style: solid; border-width: 0px 0px 3px;\"><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">In this article, let\u2019s continue the coding. Write one function to implement a state machine.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Go to the file \u2018protimer_state_mach.cpp\u2019, and let\u2019s create a function.<\/span><span style=\"font-weight: 400;\">&nbsp;<\/span><\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\">event_status_t protimer_state_machine(protimer_t *mobj, event_t&nbsp; *e){\r\n<span style=\"color: #ff9900;\">     switch<\/span> (mobj-&gt;active_state){\r\n<span style=\"color: #ff9900;\">         case<\/span> IDLE: {\r\n             protimer_state_handler_IDLE(mobj,e);\r\n          }\r\n<span style=\"color: #ff9900;\">         case<\/span> TIME_SET: {\r\n              protimer_state_handler_TIME_SET(mobj,e);\r\n}<\/pre>\n<p class=\"\" style=\"text-align: center;\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">protimer_state_mach.cpp<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">The function is a protimer; I will call this <\/span><b>protimer_state_machine<\/b><span style=\"font-weight: 400;\">. This state_machine will receive <\/span><b>protimer_t<\/b><span style=\"font-weight: 400;\">; its main application object is <\/span><b>mobj<\/b><span style=\"font-weight: 400;\">. And the state machine also receives the event. And the generalized event structure is <\/span><b>event_t<\/b><span style=\"font-weight: 400;\">, a pointer to the event structure.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And here we are using a Nested switch. Let&#8217;s use the switch case to switch between different states. <\/span><b>switch (mobj-&gt;active_state).<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Now, let&#8217;s implement the different cases\u2014<\/span><b>Case IDLE<\/b><span style=\"font-weight: 400;\"> (the first active_state IDLE). If the case is IDLE, then let&#8217;s call one state handler function here,<\/span><b> protimer_state_handler_IDLE<\/b><span style=\"font-weight: 400;\">. And for this, we have to send &#8216;<\/span><b>mobj<\/b><span style=\"font-weight: 400;\">&#8216; and<\/span><b> &#8216;e&#8217;<\/b><span style=\"font-weight: 400;\"> (the received event) like you have to implement in different cases. So, one case for each state of the state machine. And inside the <\/span><b>protimer_state_handler_IDLE()<\/b><span style=\"font-weight: 400;\"> function, you have to implement one more switch case, which switches between different events to take different actions.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And we will use the <\/span><b>\u2018return\u2019<\/b><span style=\"font-weight: 400;\"> type to return the status of the event handling. The status could be whether the event is really handled or ignored or caused any transition. That\u2019s why I will use <\/span><b>\u2018event_status_t.\u2019<\/b><span style=\"font-weight: 400;\"> Each event handler should also return the event status. Like this(as shown above), you implement different cases for different states.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7082\" aria-describedby=\"caption-attachment-7082\" style=\"width: 1175px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-7082\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2.png\" alt=\"Figure 2. Implement code for individual State handlers\" width=\"1175\" height=\"557\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2.png 1175w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-300x142.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-768x364.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-1024x485.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-600x284.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-120x57.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-500x237.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-200x95.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-400x190.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/Fig-2-protimer-code2-800x379.png 800w\" sizes=\"(max-width: 1175px) 100vw, 1175px\" \/><figcaption id=\"caption-attachment-7082\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 1. Implement code for individual State handlers<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And now, let\u2019s implement for individual State handlers. The first individual state handler is <\/span><b>protimer_state_handler_IDLE<\/b><span style=\"font-weight: 400;\">. They receive a pointer to the main application object(<\/span><b>mobj<\/b><span style=\"font-weight: 400;\">) and a pointer to the event, and this individual state handler returns the <\/span><b>event_status_t<\/b><span style=\"font-weight: 400;\">.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">The first state handler is an IDLE state. IDLE state processes various signals. Increment time, a TIME_TICK, START_PAUSE, etc., It also has an Internal activities Entry and Exit. We will also treat them as signals for the implementation purpose. We will call it an Entry event, an Exit event. <\/span><span style=\"font-weight: 400;\">&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let\u2019s use one more switch case. That\u2019s why it\u2019s called a Nested switch. First code snippet shows the upper switch, which is to switch between different states of the state machine, and&nbsp; second ccode snippets shows the inner switch is to switch between different signals the state processes.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><b>switch(e-&gt;sig). <\/b><span style=\"font-weight: 400;\">We have to implement one case for each signal, the state processes. Start with ENTRY. We are not using the <\/span><b>&#8216;break&#8217;<\/b><span style=\"font-weight: 400;\"> statements because we have to return the status of the event handling.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And one more case for EXIT, one more case for Increment time (INC_TIME), one more case for START_PAUSE, one more case for TIME_TICK. Like that, you have to complete another state handler&#8217;s implementation.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let&#8217;s define the event_status_t.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"color: #ff99cc;\">typedef enum<\/span>{\r\n    EVENT_HANDLED,\r\n    EVENT_IGNORED,\r\n    EVENT_TRANSITION\r\n}event_status_t;<strong>\r\n<\/strong><\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Implementation of event_status_t in main.h<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Go to the main.h, and here create a <\/span><b>typedef enum<\/b><span style=\"font-weight: 400;\">. The status is <\/span><b>EVENT_HANDLED, EVENT_IGNORED,<\/b><span style=\"font-weight: 400;\"> and <\/span><b>EVENT_TRANISTION<\/b><span style=\"font-weight: 400;\">. This indicates the status of event handling; After that, <\/span><b>event_status_t<\/b><span style=\"font-weight: 400;\">.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">As per our diagram, the ENTRY action is c_time=0 and e_time=0.&nbsp; <\/span><b>mobj-&gt;curr_time=0; mobj-&gt;elapsed_time=0;<\/b><span style=\"font-weight: 400;\"> And after that, display time. We will use one helper function called <\/span><b>display_time(0); <\/b><span style=\"font-weight: 400;\">After that, <\/span><b>display_message(\u201cSet time\u201d).<\/b><span style=\"font-weight: 400;\"> That\u2019s the Entry action. After completing the Entry action, it should <\/span><b>return EVENT_HANDLED<\/b><span style=\"font-weight: 400;\">.&nbsp;<\/span><\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\">event_status_t protimer_state_handler_IDLE(protimer_t *mobj, event_t&nbsp; *e){\r\n<span style=\"color: #ff9900;\">switch<\/span>(e-&gt;sig){\r\n<span style=\"color: #ff9900;\">    case<\/span> ENTRY:{\r\n          mobj-&gt;curr_time = 0;\r\n          mobj-&gt;elapsed_time = 0;\r\n          display_time(0);\r\n          display_message(\"Set\",0,0);\r\n<span style=\"color: #ff99cc;\">          return<\/span> EVENT_HANDLED;\r\n         }<\/pre>\n<p class=\"\" style=\"text-align: center;\"><span class=\"\" style=\"color: #000000;\">Entry action implementation&nbsp; in <span style=\"color: #000000;\"><span style=\"font-weight: 400;\">protimer_state_mach.cpp<\/span><\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">It has to return the status( as shown below).<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\">event_status_t protimer_state_machine(protimer_t *mobj, event_t *e){\r\n     <span style=\"color: #ff9900;\">switch<\/span> (mobj-&gt;active_state){\r\n         <span style=\"color: #ff9900;\">case<\/span> IDLE: {\r\n              <span style=\"color: #ff99cc;\"> return<\/span> protimer_state_handler_IDLE(mobj,e);\r\n         }\r\n         <span style=\"color: #ff9900;\">case<\/span> TIME_SET: {\r\n              <span style=\"color: #ff99cc;\"> return<\/span> protimer_state_handler_TIME_SET(mobj,e);\r\n         }<\/pre>\n<p class=\"\" style=\"text-align: center;\"><span style=\"color: #000000;\">return the status in <span class=\"\" style=\"color: #000000;\"><span style=\"font-weight: 400;\">protimer_state_mach.cpp<\/span><\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Next, let\u2019s implement the EXIT case.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"color: #ff9900;\"> case<\/span> EXIT:{\r\n      display_clear();\r\n     <span style=\"color: #ff99cc;\"> return<\/span> EVENT_HANDLED;\r\n }<\/pre>\n<p class=\"\" style=\"text-align: center;\"><span style=\"color: #000000;\">Exit action implementation in <span class=\"\" style=\"color: #000000;\"><span style=\"font-weight: 400;\">protimer_state_mach.cpp<\/span><\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And after that, Increment time. Increment time it has to add 60 seconds to c_time.&nbsp; <\/span><b>mobj-&gt;curr_time += 60; <\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">The increment time causes the Transition, <\/span><b>mobj-&gt;active_state=TIME_SET<\/b><span style=\"font-weight: 400;\"> (the new state is assigned to active_state<\/span><span style=\"font-weight: 400;\">); <\/span><span style=\"font-weight: 400;\">return the status as <\/span><b>EVENT_TRANSITION<\/b><span style=\"font-weight: 400;\">.<\/span><\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px;\"><span style=\"color: #ff6600;\"> case<\/span> INC_TIME:{\r\n    mobj-&gt;curr_time += 60;\r\n    mobj-&gt;active_state = TIME_SET;\r\n<span style=\"color: #ff99cc;\">    return<\/span> EVENT_TRANSITION;\r\n }<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">INC_TIME implementation<\/span><\/p>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Next START_PAUSE. For START_PAUSE, there is no Action, just a Transition. <\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><b>mobj-&gt;active_state = STAT;<\/b><span style=\"font-weight: 400;\"> here also return the status as&nbsp; <\/span><b>EVENT_TRANSITION<\/b><span style=\"font-weight: 400;\">.&nbsp;<\/span><span style=\"font-weight: 400;\">&nbsp;<\/span><\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\"><span style=\"color: #ff9900;\"> case<\/span> START_PAUSE:{\r\n      mobj-&gt;active_state = STAT;\r\n      <span style=\"color: #ff99cc;\">return<\/span> EVENT_TRANSITION;\r\n }<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">START_PAUSE implementation<\/span><\/p>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The next one is TIME_TICK. The TIME_TICK is the Internal Transition. So, there is a Guard. How to implement this? You have to access the sub-second field of the event. The TIME_TICK implementation is shown below.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 13px; box-shadow: #cecece 0px 0px 0px 0px inset;\"><span style=\"color: #ff9900;\">   case<\/span> TIME_TICK:{\r\n       <span style=\"color: #ff99cc;\"> if<\/span>(((protimer_tick_event_t*)(e))-&gt;ss == 5){\r\n           do_beep();\r\n          <span style=\"color: #ff99cc;\"> return<\/span> EVENT_HANDLED;\r\n        }\r\n        <span style=\"color: #ff99cc;\">return<\/span> EVENT_IGNORED;\r\n   }\r\n\r\n }<span style=\"color: #008000;\">\/\/END OF SWITCH<\/span>\r\n\r\n<span style=\"color: #ff99cc;\"> return<\/span> EVENT_IGNORED;\r\n}<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">TIME_TICK Implementation<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><b>If(protimer_tick_event *)<\/b><span style=\"font-weight: 400;\"> you have to typecast the pointer, you must access the &#8216;ss&#8217; field. If it = 5, then you have to call <\/span><b>do_beep();<\/b><span style=\"font-weight: 400;\"> then you have to return <\/span><b>EVENT_HANDLED.<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Suppose, if this condition fails, then you return <\/span><b>EVENT_IGNORED<\/b><span style=\"font-weight: 400;\">. This is the end of the switch case. If this state receives any other event, then it is ignored. That&#8217;s why at the end, you return <\/span><b>EVENT_IGNORED <\/b><span style=\"font-weight: 400;\">by default.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">That&#8217;s the implementation of the handler of the state IDLE. Likewise, you have to implement similar functions for other states.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7090\" aria-describedby=\"caption-attachment-7090\" style=\"width: 977px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-7090 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/helper-function-static.png\" alt=\"Exercise-003 Nested switch implementation of an FSM part 1\" width=\"977\" height=\"509\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static.png 977w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-300x156.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-768x400.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-600x313.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-120x63.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-500x260.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-200x104.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-400x208.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/helper-function-static-800x417.png 800w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><figcaption id=\"caption-attachment-7090\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 2. Helper functions implementation<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Look at Figure 2; These are helper functions to implement our actions. We will implement that at the end.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The first helper function is display_time. And after that, display_message, so this is a string value. \u2018String\u2019 is a user-defined data type provided by the Arduino framework to represent string values. I will use String s. After that, do_beep().&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Now you give the prototypes of these functions as shown below.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px;\"><span style=\"color: #ff99cc;\">#include<\/span> \"main.h\"\r\n<span style=\"color: #ff99cc;\">#include<\/span> \"lcd.h\"\r\n\r\n<span style=\"color: #008000;\">\/\/prototypes of helper functions<\/span> \r\n<span style=\"color: #ff99cc;\">static void<\/span> do_beep(void);\r\n<span style=\"color: #ff99cc;\">static void<\/span> display_clear(void);\r\n<span style=\"color: #ff99cc;\">static void<\/span> display_message(String s);\r\n<span style=\"color: #ff99cc;\">static void<\/span> display_time(<span style=\"color: #008000;\">uint32_t<\/span> time);<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Prototypes of helper functions<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Provide the prototypes of those helper functions. And after that, you have to provide the prototypes of state handlers functions as well. So, let\u2019s make them static because these are private to this file. All functions you can make it as static, no problem. <\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7091\" aria-describedby=\"caption-attachment-7091\" style=\"width: 1207px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-7091 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/const.png\" alt=\"Exercise-003 Nested switch implementation of an FSM part 1\" width=\"1207\" height=\"305\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const.png 1207w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-300x76.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-768x194.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-1024x259.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-600x152.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-120x30.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-500x126.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-200x51.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-400x101.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-800x202.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/const-1200x303.png 1200w\" sizes=\"(max-width: 1207px) 100vw, 1207px\" \/><figcaption id=\"caption-attachment-7091\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 10. Const representation<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Look at Figure 3; mobj is a const pointer, pointing to variable data. Data can be modified. But this pointer cannot be modified. That\u2019s why, as a best practice, you can use \u2018const\u2019 here.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">How to interpret this? <\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">\u2018e\u2019 is a const pointer pointing to const data of this type. Data, as well as pointers, are constant here. The handler should not modify that; it just uses it. But in Protimer_t, the data can be modified. That\u2019s why mobj is a const pointer pointing to variable data.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Now you have modified the function declarators. That\u2019s why these function prototypes also have to be modified.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 20px; line-height: 25px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000080;\"><b>FastBit Embedded Brain Academy Courses<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px;\"><b>&nbsp;<span style=\"color: #000000;\">C<\/span><\/b><span style=\"font-weight: 400; color: #000000;\">lick here:<\/span><span style=\"color: #3366ff;\"><a style=\"color: #3366ff; text-decoration: underline;\" href=\"http:\/\/fastbitlab.com\/course1\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/fastbitlab.com\/course1<\/span><\/a><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Exercise-003 Nested switch implementation of an FSM part 1 &nbsp; &nbsp; In this article, let\u2019s continue the coding. Write one function to implement a state machine. Go to the file \u2018protimer_state_mach.cpp\u2019, and let\u2019s create a function.&nbsp; event_status_t protimer_state_machine(protimer_t *mobj, event_t&nbsp; *e){ switch (mobj-&gt;active_state){ case IDLE: { protimer_state_handler_IDLE(mobj,e); } case TIME_SET: { protimer_state_handler_TIME_SET(mobj,e); } protimer_state_mach.cpp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7081,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[8],"tags":[17],"class_list":["post-7078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-finite-state-machine","entry","has-media"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Exercise-003 Nested switch implementation of an FSM part 1<\/title>\n<meta name=\"description\" content=\"Exercise-003 Nested switch implementation of an FSM part 1. In this article, let\u2019s continue the coding. Write one function to implement a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exercise-003 Nested switch implementation of an FSM part 1\" \/>\n<meta property=\"og:description\" content=\"Exercise-003 Nested switch implementation of an FSM part 1. In this article, let\u2019s continue the coding. Write one function to implement a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/\" \/>\n<meta property=\"og:site_name\" content=\"FastBit EBA\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fastbiteba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-02T08:26:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-08T09:46:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/fig-1-protimer-code-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1181\" \/>\n\t<meta property=\"og:image:height\" content=\"557\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"FastBitLab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:site\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FastBitLab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/\"},\"author\":{\"name\":\"FastBitLab\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\"},\"headline\":\"FSM Lecture 24: Exercise-003 Nested switch implementation of an FSM part 1\",\"datePublished\":\"2022-02-02T08:26:22+00:00\",\"dateModified\":\"2023-08-08T09:46:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/\"},\"wordCount\":1160,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/fig-1-protimer-code-1.png\",\"keywords\":[\"Finite state Machine(FSM)\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/\",\"name\":\"Exercise-003 Nested switch implementation of an FSM part 1\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/fig-1-protimer-code-1.png\",\"datePublished\":\"2022-02-02T08:26:22+00:00\",\"dateModified\":\"2023-08-08T09:46:47+00:00\",\"description\":\"Exercise-003 Nested switch implementation of an FSM part 1. In this article, let\u2019s continue the coding. Write one function to implement a\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/fig-1-protimer-code-1.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/fig-1-protimer-code-1.png\",\"width\":1181,\"height\":557,\"caption\":\"Figure 1. Implementation of state machine function\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"FSM Lecture 24: Exercise-003 Nested switch implementation of an FSM part 1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"name\":\"FastBit EBA\",\"description\":\"Your Online Academy of Embedded Systems\",\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\",\"name\":\"FastBit EBA\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"width\":640,\"height\":640,\"caption\":\"FastBit EBA\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/fastbiteba\\\/\",\"https:\\\/\\\/x.com\\\/fastbiteba\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/fastbit-embedded-brain-academy-b3167b124\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCa1REBV9hyrzGp2mjJCagBg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\",\"name\":\"FastBitLab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"caption\":\"FastBitLab\"},\"description\":\"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exercise-003 Nested switch implementation of an FSM part 1","description":"Exercise-003 Nested switch implementation of an FSM part 1. In this article, let\u2019s continue the coding. Write one function to implement a","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/","og_locale":"en_US","og_type":"article","og_title":"Exercise-003 Nested switch implementation of an FSM part 1","og_description":"Exercise-003 Nested switch implementation of an FSM part 1. In this article, let\u2019s continue the coding. Write one function to implement a","og_url":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/","og_site_name":"FastBit EBA","article_publisher":"https:\/\/www.facebook.com\/fastbiteba\/","article_published_time":"2022-02-02T08:26:22+00:00","article_modified_time":"2023-08-08T09:46:47+00:00","og_image":[{"width":1181,"height":557,"url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/fig-1-protimer-code-1.png","type":"image\/png"}],"author":"FastBitLab","twitter_card":"summary_large_image","twitter_creator":"@fastbiteba","twitter_site":"@fastbiteba","twitter_misc":{"Written by":"FastBitLab","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#article","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/"},"author":{"name":"FastBitLab","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d"},"headline":"FSM Lecture 24: Exercise-003 Nested switch implementation of an FSM part 1","datePublished":"2022-02-02T08:26:22+00:00","dateModified":"2023-08-08T09:46:47+00:00","mainEntityOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/"},"wordCount":1160,"commentCount":0,"publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/fig-1-protimer-code-1.png","keywords":["Finite state Machine(FSM)"],"articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/","url":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/","name":"Exercise-003 Nested switch implementation of an FSM part 1","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#primaryimage"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/fig-1-protimer-code-1.png","datePublished":"2022-02-02T08:26:22+00:00","dateModified":"2023-08-08T09:46:47+00:00","description":"Exercise-003 Nested switch implementation of an FSM part 1. In this article, let\u2019s continue the coding. Write one function to implement a","breadcrumb":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#primaryimage","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/fig-1-protimer-code-1.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/fig-1-protimer-code-1.png","width":1181,"height":557,"caption":"Figure 1. Implementation of state machine function"},{"@type":"BreadcrumbList","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-24-exercise-003-nested-switch-implementation-of-an-fsm-part-1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fastbitlab.com\/blog\/"},{"@type":"ListItem","position":2,"name":"FSM Lecture 24: Exercise-003 Nested switch implementation of an FSM part 1"}]},{"@type":"WebSite","@id":"https:\/\/fastbitlab.com\/blog\/#website","url":"https:\/\/fastbitlab.com\/blog\/","name":"FastBit EBA","description":"Your Online Academy of Embedded Systems","publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fastbitlab.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fastbitlab.com\/blog\/#organization","name":"FastBit EBA","url":"https:\/\/fastbitlab.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","width":640,"height":640,"caption":"FastBit EBA"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fastbiteba\/","https:\/\/x.com\/fastbiteba","https:\/\/www.linkedin.com\/in\/fastbit-embedded-brain-academy-b3167b124\/","https:\/\/www.youtube.com\/channel\/UCa1REBV9hyrzGp2mjJCagBg"]},{"@type":"Person","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d","name":"FastBitLab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","caption":"FastBitLab"},"description":"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries."}]}},"_links":{"self":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/comments?post=7078"}],"version-history":[{"count":5,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7078\/revisions"}],"predecessor-version":[{"id":15335,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7078\/revisions\/15335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media\/7081"}],"wp:attachment":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media?parent=7078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/categories?post=7078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/tags?post=7078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}