/* Copyright 2018 Jarred Steenvoorden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ // TODO: Add Alt-Tab to nav + W layer #ifndef USERSPACE #define USERSPACE #include "quantum.h" enum userspace_custom_keycodes { VRSN = SAFE_RANGE, // Prints QMK Firmware and board info ALT_TAB, CTL_TAB, // Layer keys NAVI, LOWER, RAISE, NUMPAD }; // Layers enum { _QW = 0, _GAME, _LW, _NV, _NP, _MS, }; #define MS_A LT(_MS,KC_A) #define WIN_Z LGUI_T(KC_Z) #define CTL_SLH RCTL_T(KC_SLSH) // Wrappers #define LAYOUT_planck_grid_wrapper(...) LAYOUT_planck_grid(__VA_ARGS__) #define LAYOUT_plaid_grid_wrapper(...) LAYOUT_plaid_grid(__VA_ARGS__) #define LAYOUT_atreus62_grid_wrapper(...) LAYOUT(__VA_ARGS__) #define LAYOUT_ergotravel_grid_wrapper(...) LAYOUT(__VA_ARGS__) /* Qwerty Layer */ #define QWERTY_L1 KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T #define QWERTY_L2 NAVI, KC_A, KC_S, KC_D, KC_F, KC_G #define QWERTY_L3 KC_LSFT, WIN_Z, KC_X, KC_C, KC_V, KC_B #define QWERTY_L4 KC_LCTL, KC_LGUI, NUMPAD, KC_LALT, LOWER, KC_SPC #define QWERTY_R1 KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC #define QWERTY_R2 KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT #define QWERTY_R3 KC_N, KC_M, KC_COMM, KC_DOT, CTL_SLH, KC_RSFT #define QWERTY_R4 KC_ENT, RAISE, KC_RALT, MO(_MS), KC_APP, KC_RCTL #define QWERTY_4_DOX KC_LCTL, LOWER, KC_SPC, KC_ENT, RAISE, KC_RALT /* Game Layer */ #define GAME_L1 _______, _______, _______, _______, _______, _______ #define GAME_L2 _______, _______, _______, _______, _______, _______ #define GAME_L3 _______, _______, _______, _______, _______, _______ #define GAME_L4 _______, _______, KC_LALT, LOWER, KC_SPC, KC_SPC #define GAME_R1 _______, _______, _______, _______, _______, _______ #define GAME_R2 _______, _______, _______, _______, _______, _______ #define GAME_R3 _______, _______, _______, _______, _______, _______ #define GAME_R4 _______, _______, _______, _______, _______, _______ /* Lower / Upper Layer */ #define LOWER_L1 KC_ESC , KC_1, KC_2, KC_3, KC_4, KC_5 #define LOWER_L2 _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5 #define LOWER_L3 _______, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10 #define LOWER_L4 _______, _______, _______, _______, _______, _______ #define LOWER_R1 KC_6, KC_7, KC_8, KC_9, KC_0, _______ #define LOWER_R2 KC_F11, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC, KC_BSLS #define LOWER_R3 KC_F12, KC_GRV, _______, _______, _______, _______ #define LOWER_R4 _______, _______, _______, _______, _______, _______ #define LOWER_4_DOX _______, _______, _______, _______, _______, _______ /* Navigation Layer */ #define NAV_L1 _______, _______, _______, KC_LGUI, KC_DEL, KC_BSPC #define NAV_L2 _______, _______, _______, KC_LSFT, KC_LCTL, KC_ENT #define NAV_L3 _______, _______, _______, _______, _______, _______ #define NAV_L4 _______, _______, _______, CTL_TAB, ALT_TAB, _______ #define NAV_R1 _______, KC_HOME, KC_UP , KC_END , KC_INS, _______ #define NAV_R2 _______, KC_LEFT, KC_DOWN, KC_RGHT, KC_DEL, _______ #define NAV_R3 _______, KC_PGUP, KC_PGDN, _______, _______, _______ #define NAV_R4 _______, KC_APP, _______, _______, _______, _______ #define NAV_4_DOX CTL_TAB, ALT_TAB, _______, _______, KC_APP, _______ /* Numpad Layer */ #define NUMPAD_L1 RGB_TOG, RGB_MOD,RGB_RMOD, _______, RGB_HUD, RGB_HUI #define NUMPAD_L2 BL_TOGG, BL_STEP, BL_BRTG, _______, RGB_SAD, RGB_SAI #define NUMPAD_L3 _______, _______, _______, _______, RGB_VAD, RGB_VAI #define NUMPAD_L4 _______, _______, _______, _______, RGB_SPD, RGB_SPI #define NUMPAD_R1 DF(_QW),DF(_GAME), _______, _______, _______, RESET #define NUMPAD_R2 _______, _______, _______, _______, _______, _______ #define NUMPAD_R3 VRSN, _______, _______, _______, _______, _______ #define NUMPAD_R4 _______, _______, _______, _______, _______, _______ #define NUMPAD_4_DOX _______, _______, _______, _______, _______, _______ /* Mouse Layer */ #define MOUSE_L1 _______, _______, _______, _______, _______, _______ #define MOUSE_L2 _______, _______, KC_ACL1, KC_ACL0, KC_BTN1, KC_BTN2 #define MOUSE_L3 _______, _______, _______, _______, _______, _______ #define MOUSE_L4 _______, _______, _______, _______, _______, KC_BTN1 #define MOUSE_R1 _______, KC_WH_U, KC_MS_U, KC_WH_D, _______, _______ #define MOUSE_R2 _______, KC_MS_L, KC_MS_D, KC_MS_R, _______, _______ #define MOUSE_R3 _______, _______, _______, _______, _______, _______ #define MOUSE_R4 KC_BTN2, _______, _______, _______, _______, _______ #define MOUSE_4_DOX _______, _______, _______, _______, _______, _______ #define BLANK_12 KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO #define QWERTY_1_12 QWERTY_L1, QWERTY_R1 #define QWERTY_2_12 QWERTY_L2, QWERTY_R2 #define QWERTY_3_12 QWERTY_L3, QWERTY_R3 #define QWERTY_4_12 QWERTY_L4, QWERTY_R4 #define GAME_1_12 GAME_L1, GAME_R1 #define GAME_2_12 GAME_L2, GAME_R2 #define GAME_3_12 GAME_L3, GAME_R3 #define GAME_4_12 GAME_L4, GAME_R4 #define LOWER_1_12 LOWER_L1, LOWER_R1 #define LOWER_2_12 LOWER_L2, LOWER_R2 #define LOWER_3_12 LOWER_L3, LOWER_R3 #define LOWER_4_12 LOWER_L4, LOWER_R4 #define NAV_1_12 NAV_L1, NAV_R1 #define NAV_2_12 NAV_L2, NAV_R2 #define NAV_3_12 NAV_L3, NAV_R3 #define NAV_4_12 NAV_L4, NAV_R4 #define NUMPAD_1_12 NUMPAD_L1, NUMPAD_R1 #define NUMPAD_2_12 NUMPAD_L2, NUMPAD_R2 #define NUMPAD_3_12 NUMPAD_L3, NUMPAD_R3 #define NUMPAD_4_12 NUMPAD_L4, NUMPAD_R4 #define MOUSE_1_12 MOUSE_L1, MOUSE_R1 #define MOUSE_2_12 MOUSE_L2, MOUSE_R2 #define MOUSE_3_12 MOUSE_L3, MOUSE_R3 #define MOUSE_4_12 MOUSE_L4, MOUSE_R4 #define QWERTY_4x12 QWERTY_1_12, QWERTY_2_12, QWERTY_3_12, QWERTY_4_12 #define GAME_4x12 GAME_1_12, GAME_2_12, GAME_3_12, GAME_4_12 #define LOWER_4x12 LOWER_1_12, LOWER_2_12, LOWER_3_12, LOWER_4_12 #define NAV_4x12 NAV_1_12, NAV_2_12, NAV_3_12, NAV_4_12 #define NUMPAD_4x12 NUMPAD_1_12, NUMPAD_2_12, NUMPAD_3_12, NUMPAD_4_12 #define MOUSE_4x12 MOUSE_1_12, MOUSE_2_12, MOUSE_3_12, MOUSE_4_12 #endif /a> 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
#ifdef SSD1306OLED

#include "ssd1306.h"
#include "i2c.h"
#include <string.h>
#include "print.h"
#ifndef LOCAL_GLCDFONT
#include "common/glcdfont.c"
#else
#include <helixfont.h>
#endif
#ifdef ADAFRUIT_BLE_ENABLE
#include "adafruit_ble.h"
#endif
#ifdef PROTOCOL_LUFA
#include "lufa.h"
#endif
#include "sendchar.h"
#include "timer.h"

// Set this to 1 to help diagnose early startup problems
// when testing power-on with ble.  Turn it off otherwise,
// as the latency of printing most of the debug info messes
// with the matrix scan, causing keys to drop.
#define DEBUG_TO_SCREEN 0

//static uint16_t last_battery_update;
//static uint32_t vbat;
//#define BatteryUpdateInterval 10000 /* milliseconds */
#define ScreenOffInterval 300000 /* milliseconds */
#if DEBUG_TO_SCREEN
static uint8_t displaying;
#endif
static uint16_t last_flush;

// Write command sequence.
// Returns true on success.
static inline bool _send_cmd1(uint8_t cmd) {
  bool res = false;

  if (i2c_start_write(SSD1306_ADDRESS)) {
    xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
    goto done;
  }

  if (i2c_master_write(0x0 /* command byte follows */)) {
    print("failed to write control byte\n");

    goto done;
  }

  if (i2c_master_write(cmd)) {
    xprintf("failed to write command %d\n", cmd);
    goto done;
  }
  res = true;
done:
  i2c_master_stop();
  return res;
}

// Write 2-byte command sequence.
// Returns true on success
static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
  if (!_send_cmd1(cmd)) {
    return false;
  }
  return _send_cmd1(opr);
}

// Write 3-byte command sequence.
// Returns true on success
static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
  if (!_send_cmd1(cmd)) {
    return false;
  }
  if (!_send_cmd1(opr1)) {
    return false;
  }
  return _send_cmd1(opr2);
}

#define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
#define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
#define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}

static void clear_display(void) {
  matrix_clear(&display);

  // Clear all of the display bits (there can be random noise
  // in the RAM on startup)
  send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
  send_cmd3(ColumnAddr, 0, DisplayWidth - 1);

  if (i2c_start_write(SSD1306_ADDRESS)) {
    goto done;
  }
  if (i2c_master_write(0x40)) {
    // Data mode
    goto done;
  }
  for (uint8_t row = 0; row < MatrixRows; ++row) {
    for (uint8_t col = 0; col < DisplayWidth; ++col) {
      i2c_master_write(0);
    }
  }

  display.dirty = false;

done:
  i2c_master_stop();
}

#if DEBUG_TO_SCREEN
#undef sendchar
static int8_t capture_sendchar(uint8_t c) {
  sendchar(c);
  iota_gfx_write_char(c);

  if (!displaying) {
    iota_gfx_flush();
  }
  return 0;
}
#endif

bool iota_gfx_init(bool rotate) {
  bool success = false;

  i2c_master_init();
  send_cmd1(DisplayOff);
  send_cmd2(SetDisplayClockDiv, 0x80);
  send_cmd2(SetMultiPlex, DisplayHeight - 1);

  send_cmd2(SetDisplayOffset, 0);


  send_cmd1(SetStartLine | 0x0);
  send_cmd2(SetChargePump, 0x14 /* Enable */);
  send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);

  if(rotate){
    // the following Flip the display orientation 180 degrees
    send_cmd1(SegRemap);
    send_cmd1(ComScanInc);
  }else{
    // Flips the display orientation 0 degrees
    send_cmd1(SegRemap | 0x1);
    send_cmd1(ComScanDec);
  }

  send_cmd2(SetComPins, 0x2);
  send_cmd2(SetContrast, 0x8f);
  send_cmd2(SetPreCharge, 0xf1);
  send_cmd2(SetVComDetect, 0x40);
  send_cmd1(DisplayAllOnResume);
  send_cmd1(NormalDisplay);
  send_cmd1(DeActivateScroll);
  send_cmd1(DisplayOn);

  send_cmd2(SetContrast, 0); // Dim

  clear_display();

  success = true;

  iota_gfx_flush();

#if DEBUG_TO_SCREEN
  print_set_sendchar(capture_sendchar);
#endif

done:
  return success;
}

bool iota_gfx_off(void) {
  bool success = false;

  send_cmd1(DisplayOff);
  success = true;

done:
  return success;
}

bool iota_gfx_on(void) {
  bool success = false;

  send_cmd1(DisplayOn);
  success = true;

done:
  return success;
}

void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
  *matrix->cursor = c;
  ++matrix->cursor;

  if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
    // We went off the end; scroll the display upwards by one line
    memmove(&matrix->display[0], &matrix->display[1],
            MatrixCols * (MatrixRows - 1));
    matrix->cursor = &matrix->display[MatrixRows - 1][0];
    memset(matrix->cursor, ' ', MatrixCols);
  }
}

void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
  matrix->dirty = true;

  if (c == '\n') {
    // Clear to end of line from the cursor and then move to the
    // start of the next line
    uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;

    while (cursor_col++ < MatrixCols) {
      matrix_write_char_inner(matrix, ' ');
    }
    return;
  }

  matrix_write_char_inner(matrix, c);
}

void iota_gfx_write_char(uint8_t c) {
  matrix_write_char(&display, c);
}

void matrix_write(struct CharacterMatrix *matrix, const char *data) {
  const char *end = data + strlen(data);
  while (data < end) {
    matrix_write_char(matrix, *data);
    ++data;
  }
}

void iota_gfx_write(const char *data) {
  matrix_write(&display, data);
}

void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
  while (true) {
    uint8_t c = pgm_read_byte(data);
    if (c == 0) {
      return;
    }
    matrix_write_char(matrix, c);
    ++data;
  }
}

void iota_gfx_write_P(const char *data) {
  matrix_write_P(&display, data);
}

void matrix_clear(struct CharacterMatrix *matrix) {
  memset(matrix->display, ' ', sizeof(matrix->display));
  matrix->cursor = &matrix->display[0][0];
  matrix->dirty = true;
}

void iota_gfx_clear_screen(void) {
  matrix_clear(&display);
}

void matrix_render(struct CharacterMatrix *matrix) {
  last_flush = timer_read();
  iota_gfx_on();
#if DEBUG_TO_SCREEN
  ++displaying;
#endif

  // Move to the home position
  send_cmd3(PageAddr, 0, MatrixRows - 1);
  send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);

  if (i2c_start_write(SSD1306_ADDRESS)) {
    goto done;
  }
  if (i2c_master_write(0x40)) {
    // Data mode
    goto done;
  }

  for (uint8_t row = 0; row < MatrixRows; ++row) {
    for (uint8_t col = 0; col < MatrixCols; ++col) {
      const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);

      for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
        uint8_t colBits = pgm_read_byte(glyph + glyphCol);
        i2c_master_write(colBits);
      }

      // 1 column of space between chars (it's not included in the glyph)
      //i2c_master_write(0);
    }
  }

  matrix->dirty = false;

done:
  i2c_master_stop();
#if DEBUG_TO_SCREEN
  --displaying;
#endif
}

void iota_gfx_flush(void) {
  matrix_render(&display);
}

__attribute__ ((weak))
void iota_gfx_task_user(void) {
}

void iota_gfx_task(void) {
  iota_gfx_task_user();

  if (display.dirty) {
    iota_gfx_flush();
  }

  if (timer_elapsed(last_flush) > ScreenOffInterval) {
    iota_gfx_off();
  }
}
#endif