{"id":56,"date":"2011-10-14T12:05:57","date_gmt":"2011-10-14T12:05:57","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=56"},"modified":"2022-02-17T10:13:11","modified_gmt":"2022-02-17T10:13:11","slug":"typedef-for-state-machines","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/typedef\/typedef-for-state-machines","title":{"rendered":"Typedef For State Machines"},"content":{"rendered":"<pre>\r\n<code>\r\ntypedef enum _SM_USER_MODE\r\n{\r\n    SM_POWERUP,\r\n    SM_ONLINE,\r\n    SM_OFFLINE,\r\n    SM_IDENTIFY\r\n} SM_USER_MODE;\r\n<\/code><\/pre>\n<h4>\nExample Usage<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\r\ntypedef enum _SM_USER_MODE\r\n{\r\n    SM_POWERUP,\r\n    SM_IDLE\r\n} SM_USER_MODE;\r\n\r\n\r\nvoid my_function (void)\r\n{\r\n\tstatic int user_mode = SM_POWERUP;\r\n\tstatic int user_mode_last = ~SM_POWERUP;\r\n\tBYTE this_is_new_mode = 0;\r\n\r\n\t\/\/----- CHECK FOR THIS IS NEW MODE -----\r\n\tif (user_mode_last != user_mode)\r\n\t{\r\n\t\tthis_is_new_mode = 1;\r\n\t\tuser_mode_last = user_mode;\r\n\t}\r\n\r\n\r\n\t\/\/--------------------------------\r\n\t\/\/--------------------------------\r\n\t\/\/----- PROCESS CURRENT MODE -----\r\n\t\/\/--------------------------------\r\n\t\/\/--------------------------------\r\n\tswitch (user_mode)\r\n\t{\r\n\tcase SM_POWERUP:\r\n\t\t\/\/--------------------\r\n\t\t\/\/--------------------\r\n\t\t\/\/----- POWER UP -----\r\n\t\t\/\/--------------------\r\n\t\t\/\/--------------------\r\n\t\tif (this_is_new_mode)\r\n\t\t{\r\n\t\t\t\/\/----- JUST ENTERED THIS MODE -----\r\n\r\n\t\t}\r\n\r\n\t\t\/\/Do something, then\r\n\t\t\/\/user_mode = SM_IDLE;\r\n\t\tbreak;\r\n\r\n\tcase SM_IDLE:\r\n\t\t\/\/----------------\r\n\t\t\/\/----------------\r\n\t\t\/\/----- IDLE -----\r\n\t\t\/\/----------------\r\n\t\t\/\/----------------\r\n\t\tif (this_is_new_mode)\r\n\t\t{\r\n\t\t\t\/\/----- JUST ENTERED THIS MODE -----\r\n\r\n\t\t}\r\n\r\n\t\tbreak;\r\n\r\n\r\n\t\/\/&lt;&lt;&lt;&lt;&lt; ADD OTHER MODES HERE\r\n\r\n\r\n\t} \/\/switch (user_mode)\r\n\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>typedef enum _SM_USER_MODE { SM_POWERUP, SM_ONLINE, SM_OFFLINE, SM_IDENTIFY } SM_USER_MODE; Example Usage typedef enum _SM_USER_MODE { SM_POWERUP, SM_IDLE } SM_USER_MODE; void my_function (void) { static int user_mode = SM_POWERUP; static int user_mode_last = ~SM_POWERUP; BYTE this_is_new_mode = 0; \/\/&#8212;&#8211; CHECK FOR THIS IS NEW MODE &#8212;&#8211; if (user_mode_last != user_mode) { this_is_new_mode = 1; user_mode_last [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-typedef"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":656,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/56\/revisions\/656"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}