display.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "sd1306.h"
  2. #include "display.h"
  3. #include <stdlib.h>
  4. #include "pico/stdlib.h"
  5. #include "pico/binary_info.h"
  6. #include "hardware/i2c.h"
  7. #include "pico/util/queue.h"
  8. #include "pico/multicore.h"
  9. #include "font8x8.h"
  10. uint8_t display_buffer[OLED_BUF_LEN];
  11. void display_putchar(char c, uint8_t col_x, uint8_t col_y, bool reverse)
  12. {
  13. uint32_t offset = col_x * FONT_WIDTH + OLED_WIDTH * col_y + FONT_WIDTH;
  14. if( c < 0 ) return;
  15. if(col_x > 15 || col_y > 3) return;
  16. // フォントの縦横変換
  17. for(int i = 0; i < 8 ; i++ ) {
  18. volatile uint8_t f;
  19. f = ((font8x8[(int)c][reverse ? 0 : 7] << (reverse ? 7 - i : i)) & 0x80) >> 0;
  20. f |= ((font8x8[(int)c][reverse ? 1 : 6] << (reverse ? 7 - i : i)) & 0x80) >> 1;
  21. f |= ((font8x8[(int)c][reverse ? 2 : 5] << (reverse ? 7 - i : i)) & 0x80) >> 2;
  22. f |= ((font8x8[(int)c][reverse ? 3 : 4] << (reverse ? 7 - i : i)) & 0x80) >> 3;
  23. f |= ((font8x8[(int)c][reverse ? 4 : 3] << (reverse ? 7 - i : i)) & 0x80) >> 4;
  24. f |= ((font8x8[(int)c][reverse ? 5 : 2] << (reverse ? 7 - i : i)) & 0x80) >> 5;
  25. f |= ((font8x8[(int)c][reverse ? 6 : 1] << (reverse ? 7 - i : i)) & 0x80) >> 6;
  26. f |= ((font8x8[(int)c][reverse ? 7 : 0] << (reverse ? 7 - i : i)) & 0x80) >> 7;
  27. display_buffer[offset - i] = f;
  28. }
  29. }
  30. void display_putstr(char *str, uint8_t col_x, uint8_t col_y, bool reverse)
  31. {
  32. int i = 0;
  33. while(str[i] != '\0') {
  34. display_putchar(str[i++], col_x++, col_y, reverse);
  35. }
  36. }
  37. void display(void)
  38. {
  39. // I2C初期化
  40. i2c_init(i2c_default, 800 * 1000);
  41. // I2Cピン初期化
  42. gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
  43. gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
  44. gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
  45. gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
  46. // OLED初期化
  47. oled_init();
  48. struct render_area frame_area = {start_col: 0,
  49. end_col : OLED_WIDTH - 1,
  50. start_page : 0,
  51. end_page : OLED_NUM_PAGES - 1,
  52. buflen : 0 };
  53. calc_render_area_buflen(&frame_area);
  54. // ディスプレイクリア
  55. fill(display_buffer, 0x00);
  56. while(1) {
  57. render(display_buffer, &frame_area);
  58. sleep_us(6*1000);
  59. }
  60. }