/ src / Adafruit_dvhstx.cpp
Adafruit_dvhstx.cpp
  1  #include "Adafruit_dvhstx.h"
  2  
  3  int16_t dvhstx_width(DVHSTXResolution r) {
  4    switch (r) {
  5    default:
  6    case DVHSTX_RESOLUTION_320x180:
  7      return 320;
  8    case DVHSTX_RESOLUTION_640x360:
  9      return 640;
 10    case DVHSTX_RESOLUTION_480x270:
 11      return 480;
 12    case DVHSTX_RESOLUTION_400x225:
 13      return 400;
 14    case DVHSTX_RESOLUTION_320x240:
 15      return 320;
 16    case DVHSTX_RESOLUTION_360x240:
 17      return 360;
 18    case DVHSTX_RESOLUTION_360x200:
 19      return 360;
 20    case DVHSTX_RESOLUTION_360x288:
 21      return 360;
 22    case DVHSTX_RESOLUTION_400x300:
 23      return 400;
 24    case DVHSTX_RESOLUTION_512x384:
 25      return 512;
 26    case DVHSTX_RESOLUTION_400x240:
 27      return 400;
 28    }
 29    return 0;
 30  }
 31  
 32  int16_t dvhstx_height(DVHSTXResolution r) {
 33    switch (r) {
 34    default:
 35    case DVHSTX_RESOLUTION_320x180:
 36      return 180;
 37    case DVHSTX_RESOLUTION_640x360:
 38      return 360;
 39    case DVHSTX_RESOLUTION_480x270:
 40      return 270;
 41    case DVHSTX_RESOLUTION_400x225:
 42      return 225;
 43    case DVHSTX_RESOLUTION_320x240:
 44      return 240;
 45    case DVHSTX_RESOLUTION_360x240:
 46      return 240;
 47    case DVHSTX_RESOLUTION_360x200:
 48      return 200;
 49    case DVHSTX_RESOLUTION_360x288:
 50      return 288;
 51    case DVHSTX_RESOLUTION_400x300:
 52      return 300;
 53    case DVHSTX_RESOLUTION_512x384:
 54      return 384;
 55    case DVHSTX_RESOLUTION_400x240:
 56      return 240;
 57    }
 58  }
 59  
 60  void DVHSTX16::swap(bool copy_framebuffer) {
 61    if (!double_buffered) {
 62      return;
 63    }
 64    hstx.flip_blocking();
 65    if (copy_framebuffer) {
 66      memcpy(hstx.get_front_buffer<uint8_t>(), hstx.get_back_buffer<uint8_t>(),
 67             sizeof(uint16_t) * _width * _height);
 68    }
 69  }
 70  void DVHSTX8::swap(bool copy_framebuffer) {
 71    if (!double_buffered) {
 72      return;
 73    }
 74    hstx.flip_blocking();
 75    if (copy_framebuffer) {
 76      memcpy(hstx.get_front_buffer<uint8_t>(), hstx.get_back_buffer<uint8_t>(),
 77             sizeof(uint8_t) * _width * _height);
 78    }
 79  }
 80  
 81  void DVHSTXText3::clear() {
 82    memset(getBuffer(), 0, WIDTH * HEIGHT * sizeof(uint16_t));
 83  }
 84  
 85  // Character framebuffer is actually a small GFXcanvas16, so...
 86  size_t DVHSTXText3::write(uint8_t c) {
 87    if (c == '\r') { // Carriage return
 88      cursor_x = 0;
 89    } else if ((c == '\n') ||
 90               (c >= 32 &&
 91                cursor_x > WIDTH)) { // Newline OR right edge and printing
 92      cursor_x = 0;
 93      if (cursor_y >= (HEIGHT - 1)) { // Vert scroll?
 94        memmove(getBuffer(), getBuffer() + WIDTH,
 95                WIDTH * (HEIGHT - 1) * sizeof(uint16_t));
 96        drawFastHLine(0, HEIGHT - 1, WIDTH, ' '); // Clear bottom line
 97        cursor_y = HEIGHT - 1;
 98      } else {
 99        cursor_y++;
100      }
101    }
102    if (c >= 32) {
103      drawPixel(cursor_x, cursor_y, (attr << 8) | c);
104      cursor_x++;
105    }
106    sync_cursor_with_hstx();
107    return 1;
108  }