Advertisement
LAK132

ImDuino Example

Aug 8th, 2022 (edited)
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. #define IMDUINO_TFT_22_ILI9225
  2. // #define IMDUINO_SSD1306
  3. // #define IMDUINO_DESKTOP
  4.  
  5. #include "imgui.h"
  6. texture_alpha8_t fontAtlas;
  7.  
  8. #ifdef IMDUINO_TFT_22_ILI9225
  9. #  define SCREENX 220
  10. #  define SCREENY 176
  11. #  include "SPI.h"
  12. #  include "TFT_22_ILI9225.h"
  13.  
  14. const uint8_t TFTLED = 32;
  15. const uint8_t TFTRST = 33;
  16. const uint8_t TFTRS  = 27;
  17. const uint8_t TFTCS  = 15;
  18. const uint8_t TFTCLK = 14;
  19. const uint8_t TFTSDI = 13;
  20.  
  21. TFT_22_ILI9225 tft = TFT_22_ILI9225(TFTRST, TFTRS, TFTCS, TFTLED, 128);
  22. SPIClass tftspi(HSPI);
  23. texture_color16_t screen;
  24.  
  25. void screen_init()
  26. {
  27.   tft.begin(tftspi);
  28.   tft.setFont(Terminal6x8);
  29.   tft.setOrientation(3);
  30.   digitalWrite(TFTLED, HIGH);
  31.   screen.init(SCREENX, SCREENY);
  32. }
  33.  
  34. void screen_draw()
  35. {
  36.   tft.drawBitmap(0, 0, (uint16_t *)screen.pixels, screen.w, screen.h);
  37. }
  38. #endif // IMDUINO_TFT_22_ILI9225
  39.  
  40. #ifdef IMDUINO_SSD1306
  41. #  define SCREENX 128
  42. #  define SCREENY 64
  43. #  include "Adafruit_SSD1306.h"
  44. #  include "Wire.h"
  45.  
  46. const uint8_t OLEDSDA  = 5;
  47. const uint8_t OLEDSCL  = 4;
  48. const int8_t OLEDRST   = -1;
  49. const uint8_t OLEDADDR = 0x3C;
  50.  
  51. const uint8_t OLEDROTATION = 0; // = 2;
  52.  
  53. Adafruit_SSD1306 oled = Adafruit_SSD1306(SCREENX, SCREENY, &Wire, OLEDRST);
  54. texture_alpha8_t screen;
  55.  
  56. void screen_init()
  57. {
  58.   Wire.begin(OLEDSDA, OLEDSCL);
  59.   if (!oled.begin(SSD1306_SWITCHCAPVCC, OLEDADDR, true, false))
  60.   {
  61.     Serial.println("SSD1306 allocation failed");
  62.     for (;;)
  63.       ;
  64.   }
  65.   screen.init(oled.width(), oled.height());
  66.   oled.setRotation(OLEDROTATION);
  67. }
  68.  
  69. void screen_draw()
  70. {
  71.   for (int y = 0; y < SCREENY; ++y)
  72.     for (int x = 0; x < SCREENX; ++x)
  73.       oled.drawPixel(
  74.         x, y, screen.at(x, y).a > 0x7F ? SSD1306_WHITE : SSD1306_BLACK);
  75.   oled.display();
  76. }
  77. #endif // IMDUINO_SSD1306
  78.  
  79. #ifdef IMDUINO_DESKTOP
  80. #  define SCREENX 164
  81. #  define SCREENY 64
  82. #  include <iostream>
  83. #  include <thread>
  84.  
  85. texture_color32_t screen;
  86.  
  87. void screen_init()
  88. {
  89.   screen.init(SCREENX, SCREENY);
  90. }
  91.  
  92. #  define ESC_CODE "\x1B"
  93. #  define CSI_CODE ESC_CODE "["
  94.  
  95. void screen_draw()
  96. {
  97.   std::cout << CSI_CODE "H";
  98.   for (size_t y = 0; y < screen.h; ++y)
  99.   {
  100.     for (size_t x = 0; x < screen.w; ++x)
  101.     {
  102.       const auto &color = screen.at(x, y);
  103.       std::cout << CSI_CODE "48;2;" << int(color.r) << ";" << int(color.g)
  104.                 << ";" << int(color.b) << "m ";
  105.     }
  106.     std::cout << "\n";
  107.   }
  108.   std::cout << CSI_CODE "m";
  109.   std::this_thread::yield();
  110. }
  111.  
  112. void setup();
  113. void loop();
  114.  
  115. int main()
  116. {
  117.   setup();
  118.   for (;;) loop();
  119. }
  120.  
  121. unsigned long millis()
  122. {
  123.   static unsigned long m = 0;
  124.   return ++m;
  125. }
  126.  
  127. #endif // IMDUINO_DESKTOP
  128.  
  129. unsigned long drawTime;
  130. unsigned long renderTime;
  131. unsigned long rasterTime;
  132.  
  133. ImGuiContext *context;
  134.  
  135. void setup()
  136. {
  137. #ifndef IMDUINO_DESKTOP
  138.   Serial.begin(115200);
  139. #endif
  140.  
  141.   context = ImGui::CreateContext();
  142.  
  143.   ImGui_ImplSoftraster_Init(&screen);
  144.  
  145.   ImGuiStyle &style      = ImGui::GetStyle();
  146.   style.AntiAliasedLines = false;
  147.   style.AntiAliasedFill  = false;
  148.   style.WindowRounding   = 0.0f;
  149.   style.WindowPadding    = ImVec2(0.0f, 0.0f);
  150.  
  151.   ImGuiIO &io = ImGui::GetIO();
  152.   io.Fonts->Flags |=
  153.     ImFontAtlasFlags_NoPowerOfTwoHeight | ImFontAtlasFlags_NoMouseCursors;
  154.   io.FontGlobalScale = 1.0f;
  155.  
  156.   uint8_t *pixels;
  157.   int width, height;
  158.   io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  159.   fontAtlas.init(width, height, (alpha8_t *)pixels);
  160.   io.Fonts->TexID = &fontAtlas;
  161.  
  162.   screen_init();
  163. }
  164.  
  165. float f         = 0.0f;
  166. unsigned long t = 0;
  167.  
  168. void loop()
  169. {
  170.   ImGuiIO &io  = ImGui::GetIO();
  171.   io.DeltaTime = 1.0f / 60.0f;
  172.  
  173.   // io.MousePos = mouse_pos;
  174.   // io.MouseDown[0] = mouse_button_0;
  175.   // io.MouseDown[1] = mouse_button_1;
  176.  
  177.   /* [0.0f - 1.0f] */
  178.   io.NavInputs[ImGuiNavInput_Activate] =
  179.     0.0f; // activate / open / toggle / tweak value       // e.g. Circle (PS4),
  180.           // A (Xbox), B (Switch), Space (Keyboard)
  181.   io.NavInputs[ImGuiNavInput_Cancel] =
  182.     0.0f; // cancel / close / exit                        // e.g. Cross (PS4),
  183.           // B (Xbox), A (Switch), Escape (Keyboard)
  184.   io.NavInputs[ImGuiNavInput_Input] =
  185.     0.0f; // text input / on-screen keyboard              // e.g. Triang.(PS4),
  186.           // Y (Xbox), X (Switch), Return (Keyboard)
  187.   io.NavInputs[ImGuiNavInput_Menu] =
  188.     0.0f; // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4),
  189.           // X (Xbox), Y (Switch), Alt (Keyboard)
  190.   io.NavInputs[ImGuiNavInput_DpadLeft] =
  191.     0.0f; // move / tweak / resize window (w/ PadMenu)    // e.g. D-pad
  192.           // Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard)
  193.   io.NavInputs[ImGuiNavInput_DpadRight] = 0.0f;
  194.   io.NavInputs[ImGuiNavInput_DpadUp]    = 0.0f;
  195.   io.NavInputs[ImGuiNavInput_DpadDown]  = 0.0f;
  196.   io.NavInputs[ImGuiNavInput_TweakSlow] =
  197.     0.0f; // slower tweaks                                // e.g. L1 or L2
  198.           // (PS4), LB or LT (Xbox), L or ZL (Switch)
  199.   io.NavInputs[ImGuiNavInput_TweakFast] =
  200.     0.0f; // faster tweaks                                // e.g. R1 or R2
  201.           // (PS4), RB or RT (Xbox), R or ZL (Switch)
  202.  
  203.   ImGui_ImplSoftraster_NewFrame();
  204.   ImGui::NewFrame();
  205.  
  206.   bool main_open = true;
  207.   ImGui::SetNextWindowPos(ImVec2(0.0, 0.0));
  208.   ImGui::SetNextWindowSize(ImVec2(SCREENX, SCREENY));
  209.   if (ImGui::Begin(
  210.         "My Main Window",
  211.         &main_open,
  212.         ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
  213.           ImGuiWindowFlags_NoSavedSettings // these 3 you will probably always
  214.                                            // want on an embedded device.
  215.           | ImGuiWindowFlags_NoScrollbar   // if you don't want a scrollbar.
  216.           | ImGuiWindowFlags_MenuBar       // if you want a menu bar.
  217.           | ImGuiWindowFlags_NoTitleBar    // if you don't want a title bar.
  218.         ))
  219.   {
  220.     f += 0.05;
  221.     if (f > 1.0f) f = 0.0f;
  222.  
  223.     unsigned int deltaTime = millis() - t;
  224.     t += deltaTime;
  225.  
  226.     deltaTime -= (drawTime + renderTime + rasterTime);
  227.  
  228.     ImGui::Text("Hardware write time %d ms", (int)drawTime);
  229.     ImGui::Text("Render time %d ms", (int)renderTime);
  230.     ImGui::Text("Raster time %d ms", (int)rasterTime);
  231.     ImGui::Text("Remaining time %d ms", (int)deltaTime);
  232.     ImGui::SliderFloat("SliderFloat", &f, 0.0f, 1.0f);
  233.  
  234.     ImGui::End();
  235.   }
  236.  
  237.   renderTime = millis();
  238.   ImGui::Render();
  239.   renderTime = millis() - renderTime;
  240.  
  241.   rasterTime = millis();
  242.   ImGui_ImplSoftraster_RenderDrawData(ImGui::GetDrawData());
  243.   rasterTime = millis() - rasterTime;
  244.  
  245.   drawTime = millis();
  246.   screen_draw();
  247.   drawTime = millis() - drawTime;
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement