LAK132

TFT ImGui

Feb 9th, 2018 (edited)
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #include "imgui.h"
  2.  
  3. #define TFTX 220
  4. #define TFTY 176
  5.  
  6. #include "SPI.h"
  7. #include <TFT_22_ILI9225.h>
  8.  
  9. const uint8_t HSPICLK = 14;
  10. const uint8_t HSPICS0 = 15;
  11. const uint8_t HSPIMISO = 12;
  12. const uint8_t HSPIMOSI = 13;
  13. const uint8_t HSPIHD = 4;
  14. const uint8_t HSPIWP = 2;
  15.  
  16. const uint8_t ADC2CH1 = 0;
  17. const uint8_t BOOTBTN = ADC2CH1;
  18. const uint8_t LEDPIN = 16;
  19.  
  20. const uint8_t TFTLED = 25;
  21. const uint8_t TFTRST = 26;
  22. const uint8_t TFTRS = 27;
  23. const uint8_t TFTCS = HSPICS0;
  24. const uint8_t TFTCLK = HSPICLK;
  25. const uint8_t TFTSDI = HSPIMOSI;
  26.  
  27. TFT_22_ILI9225 tft = TFT_22_ILI9225(TFTRST, TFTRS, TFTCS, TFTSDI, TFTCLK, TFTLED, 128);
  28.            
  29. float displayScale = 1.2f;
  30.  
  31. typedef struct fontAtlas
  32. {
  33.   unsigned char* pixels;
  34.   int width, height;
  35. } fontAtlas_t;
  36.  
  37. fontAtlas_t fonts;
  38.  
  39. void TFTRenderFunc(ImDrawData* draw_data)
  40. {
  41.   for (int n = 0; n < draw_data->CmdListsCount; n++)
  42.   {
  43.     ImDrawList* cmd_list = *draw_data->CmdLists;
  44.     const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data;
  45.     const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;
  46.     for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  47.     {
  48.       const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  49.       if (pcmd->UserCallback)
  50.       {
  51.         pcmd->UserCallback(cmd_list, pcmd);
  52.         Serial.println("User callback");
  53.       }
  54.       else
  55.       {
  56.         Serial.println("Other callback");
  57.         Serial.println((int)pcmd->TextureId);
  58.         //MyEngineBindTexture(pcmd->TextureId);
  59.         Serial.print("ClipRect.x ");
  60.         Serial.println(pcmd->ClipRect.x);
  61.         //MyEngineScissor((int)pcmd->ClipRect.x, (int)pcmd->ClipRect.y, (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
  62.         Serial.print("ElemCount ");
  63.         Serial.println(pcmd->ElemCount);
  64.         //MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer);
  65.         //for(int i = 0; i < pcmd->ElemCount/3; i++)
  66.         for(int i = 0; i < pcmd->ElemCount; i+=3)
  67.         {
  68.           for(int j = 0; j < 3; j++)
  69.           {
  70.             Serial.println("Color ");
  71.             Serial.println(vtx_buffer[idx_buffer[i+j]].col, HEX);;
  72.            
  73.             Serial.println("UV ");
  74.             Serial.print(vtx_buffer[idx_buffer[i+j]].uv.x);
  75.             Serial.print(" ");
  76.             Serial.println(vtx_buffer[idx_buffer[i+j]].uv.y);
  77.           }
  78.  
  79.           uint8_t alpha = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_A_SHIFT) & 0xFF +
  80.             (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_A_SHIFT) & 0xFF +
  81.             (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_A_SHIFT) & 0xFF)/3;
  82.           if (alpha == 0x00) continue;
  83.           uint8_t red = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_R_SHIFT) & 0xFF +
  84.             (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_R_SHIFT) & 0xFF +
  85.             (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_R_SHIFT) & 0xFF)/3;
  86.           uint8_t green = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_G_SHIFT) & 0xFF +
  87.             (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_G_SHIFT) & 0xFF +
  88.             (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_G_SHIFT) & 0xFF)/3;
  89.           uint8_t blue = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_B_SHIFT) & 0xFF +
  90.             (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_B_SHIFT) & 0xFF +
  91.             (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_B_SHIFT) & 0xFF)/3;
  92.          
  93.           tft.fillTriangle(
  94.             vtx_buffer[idx_buffer[i]].pos.x * displayScale, vtx_buffer[idx_buffer[i]].pos.y * displayScale,
  95.             vtx_buffer[idx_buffer[i+1]].pos.x * displayScale, vtx_buffer[idx_buffer[i+1]].pos.y * displayScale,
  96.             vtx_buffer[idx_buffer[i+2]].pos.x * displayScale, vtx_buffer[idx_buffer[i+2]].pos.y * displayScale,
  97.             tft.setColor(red, green, blue));
  98.         }
  99.       }
  100.       idx_buffer += pcmd->ElemCount;
  101.     }
  102.   }
  103. }
  104.  
  105. void setup()
  106. {
  107.   Serial.begin(115200);
  108.  
  109.   tft.begin();//tftspi);
  110.   tft.setFont(Terminal6x8);
  111.   tft.setOrientation(3);
  112.   digitalWrite(TFTLED, HIGH);
  113.    
  114.   ImGui::CreateContext();
  115.   ImGuiIO& io = ImGui::GetIO();
  116.   io.DisplaySize.x = TFTX;
  117.   io.DisplaySize.y = TFTY;
  118.   io.RenderDrawListsFn = TFTRenderFunc;
  119.   //io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  120.   io.Fonts->GetTexDataAsAlpha8(&(fonts.pixels), &(fonts.width), &(fonts.height));
  121.   //MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA)
  122.   //io.Fonts->TexID = (void*)texture;
  123. }
  124.  
  125. void loop()
  126. {
  127.   ImGuiIO& io = ImGui::GetIO();
  128.   io.DeltaTime = 1.0f/60.0f;
  129.   //io.MousePos = mouse_pos;
  130.   //io.MouseDown[0] = mouse_button_0;
  131.   //io.MouseDown[1] = mouse_button_1;
  132.   //[0.0f - 1.0f]
  133.   io.NavFlags |= ImGuiNavFlags_EnableGamepad;
  134.   io.NavInputs[ImGuiNavInput_Activate] = 0.0f;    // activate / open / toggle / tweak value       // e.g. Circle (PS4), A (Xbox), B (Switch), Space (Keyboard)
  135.   io.NavInputs[ImGuiNavInput_Cancel] = 0.0f;      // cancel / close / exit                        // e.g. Cross  (PS4), B (Xbox), A (Switch), Escape (Keyboard)
  136.   io.NavInputs[ImGuiNavInput_Input] = 0.0f;       // text input / on-screen keyboard              // e.g. Triang.(PS4), Y (Xbox), X (Switch), Return (Keyboard)
  137.   io.NavInputs[ImGuiNavInput_Menu] = 0.0f;        // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4), X (Xbox), Y (Switch), Alt (Keyboard)
  138.   io.NavInputs[ImGuiNavInput_DpadLeft] = 0.0f;    // move / tweak / resize window (w/ PadMenu)    // e.g. D-pad Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard)
  139.   io.NavInputs[ImGuiNavInput_DpadRight] = 0.0f;
  140.   io.NavInputs[ImGuiNavInput_DpadUp] = 0.0f;
  141.   io.NavInputs[ImGuiNavInput_DpadDown] = 0.0f;
  142.   io.NavInputs[ImGuiNavInput_TweakSlow] = 0.0f;   // slower tweaks                                // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch)
  143.   io.NavInputs[ImGuiNavInput_TweakFast] = 0.0f;   // faster tweaks                                // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch)
  144.  
  145.   ImGui::NewFrame();
  146.  
  147.   static float f = 0.0f;
  148.   ImGui::Text("Hello, world!");
  149.   ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  150.   ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  151.  
  152.   tft.clear();
  153.   ImGui::Render();
  154.   //SwapBuffers();
  155.   Serial.print("Cool ");
  156.   Serial.println(f);
  157.   delay(1000);
  158.   displayScale += 0.1f;
  159.   if (displayScale > 1.5f) displayScale = 0.3f;
  160. }
Add Comment
Please, Sign In to add comment