Advertisement
Guest User

Simple Cardputer Terminal

a guest
Nov 28th, 2023
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.24 KB | Source Code | 0 0
  1. /**
  2.  * @file CardputerSerialTerminalWDK.ino
  3.  * @author Skinwill (https://reddit.com/u/skinwill)
  4.  * @brief Very basic serial terminal for the M5Cardputer.
  5.  * @version 0.1
  6.  * @date 2023-11-28
  7.  *
  8.  *
  9.  * @Hardwares: M5Cardputer
  10.  * @Platform Version: Arduino M5Stack Board Manager v2.0.7
  11.  * @Dependent Library:
  12.  * M5GFX: https://github.com/m5stack/M5GFX
  13.  * M5Unified: https://github.com/m5stack/M5Unified
  14.  */
  15.  
  16. #include "M5Cardputer.h"
  17. #include "M5GFX.h"
  18.  
  19. M5Canvas canvas(&M5Cardputer.Display);
  20. String data = "> ";
  21. const int textBufferSize = 50;
  22. char textBuffer[textBufferSize];
  23. int textIndex = 0;
  24.  
  25. void setup() {
  26.     Serial.begin(115200);
  27.     auto cfg = M5.config();
  28.     M5Cardputer.begin(cfg, true);
  29.     M5Cardputer.Display.setRotation(1);
  30.     M5Cardputer.Display.setTextSize(0.5);
  31.     M5Cardputer.Display.drawRect(0, 0, M5Cardputer.Display.width(),
  32.                                  M5Cardputer.Display.height() - 28, GREEN);
  33.     M5Cardputer.Display.setTextFont(&fonts::FreeSansBold18pt7b);
  34.  
  35.     M5Cardputer.Display.fillRect(0, M5Cardputer.Display.height() - 4,
  36.                                  M5Cardputer.Display.width(), 4, GREEN);
  37.  
  38.     canvas.setTextFont(&fonts::FreeSansBold18pt7b);
  39.     canvas.setTextSize(0.5);
  40.     canvas.createSprite(M5Cardputer.Display.width() - 8,
  41.                         M5Cardputer.Display.height() - 36);
  42.     canvas.setTextScroll(true);
  43.     canvas.println("Press Key and Enter to Input Text");
  44.     canvas.pushSprite(4, 4);
  45.     M5Cardputer.Display.drawString(data, 4, M5Cardputer.Display.height() - 24);
  46. }
  47.  
  48. void loop() {
  49.     M5Cardputer.update();
  50.     if (M5Cardputer.Keyboard.isChange()) {
  51.         if (M5Cardputer.Keyboard.isPressed()) {
  52.             Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();
  53.  
  54.             for (auto i : status.word) {
  55.                 data += i;
  56.             }
  57.  
  58.             if (status.del) {
  59.                 data.remove(data.length() - 1);
  60.             }
  61.  
  62.             if (status.enter) {
  63.                 data.remove(0, 2);
  64.                 canvas.println(data);
  65.                 Serial.println(data);
  66.                 canvas.pushSprite(4, 4);
  67.                 data = "> ";
  68.             }
  69.  
  70.             M5Cardputer.Display.fillRect(0, M5Cardputer.Display.height() - 28,
  71.                                          M5Cardputer.Display.width(), 25,
  72.                                          BLACK);
  73.  
  74.             M5Cardputer.Display.drawString(data, 4,
  75.                                            M5Cardputer.Display.height() - 24);
  76.         }
  77.     }
  78.         while (Serial.available() > 0) {
  79.         char incomingChar = Serial.read();
  80.  
  81.         // Check for carriage return to end the string
  82.         if (incomingChar == '\r') {
  83.             // Display the received string on the screen
  84.             canvas.println(textBuffer);
  85.             canvas.pushSprite(4, 4);
  86.  
  87.             // Clear the text buffer
  88.             memset(textBuffer, 0, textBufferSize);
  89.             textIndex = 0;
  90.         } else {
  91.             // Add the received character to the text buffer
  92.             if (textIndex < textBufferSize - 1) {
  93.                 textBuffer[textIndex++] = incomingChar;
  94.                 textBuffer[textIndex] = '\0'; // Ensure null-termination
  95.             }
  96.         }
  97.       }  
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement