ssd1306xledtx.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * SSD1306xLED - Library for the SSD1306 based OLED/PLED 128x64 displays
  3. *
  4. * @author Neven Boyanov
  5. *
  6. * This is part of the Tinusaur/SSD1306xLED project.
  7. *
  8. * Copyright (c) 2018 Neven Boyanov, The Tinusaur Team. All Rights Reserved.
  9. * Distributed as open source software under MIT License, see LICENSE.txt file.
  10. * Retain in your source code the link http://tinusaur.org to the Tinusaur project.
  11. *
  12. * Source code available at: https://bitbucket.org/tinusaur/ssd1306xled
  13. *
  14. */
  15. // ============================================================================
  16. #if defined(__GNUC__)
  17. #include <stdlib.h>
  18. #include <avr/io.h>
  19. #include <avr/pgmspace.h>
  20. #elif defined(__ICCAVR__)
  21. # include <ioavr.h>
  22. # include <intrinsics.h>
  23. # include <stdint.h>
  24. #define PROGMEM __flash
  25. #define pgm_read_byte(a) (*(unsigned char __flash *)(a))
  26. #define pgm_read_word(a) (*(unsigned __flash *)(a))
  27. #endif
  28. #include "tinyavrlib/num2str.h"
  29. #include "ssd1306xled.h"
  30. #include "ssd1306xledtx.h"
  31. // ----------------------------------------------------------------------------
  32. extern void ssd1306_start_data(void); // Initiate transmission of data
  33. extern void ssd1306_data_byte(uint8_t); // Transmission 1 byte of data
  34. extern void ssd1306_stop(void); // Finish transmission
  35. // ----------------------------------------------------------------------------
  36. const uint8_t *ssd1306tx_font_src;
  37. uint8_t ssd1306tx_font_char_base;
  38. // ----------------------------------------------------------------------------
  39. void ssd1306tx_init(const uint8_t *fron_src, uint8_t char_base)
  40. {
  41. ssd1306tx_font_src = fron_src;
  42. ssd1306tx_font_char_base = char_base;
  43. }
  44. // ----------------------------------------------------------------------------
  45. void ssd1306tx_char(char ch)
  46. {
  47. uint16_t j = (ch << 2) + (ch << 1) - 192; // Equiv.: j=(ch-32)*6 <== Convert ASCII code to font data index.
  48. ssd1306_start_data();
  49. for (uint8_t i = 0; i < 6; i++) {
  50. ssd1306_data_byte(pgm_read_byte(&ssd1306tx_font_src[j + i]));
  51. }
  52. ssd1306_stop();
  53. }
  54. void ssd1306tx_string(char *s)
  55. {
  56. while (*s) {
  57. ssd1306tx_char(*s++);
  58. }
  59. }
  60. // ----------------------------------------------------------------------------
  61. char ssd1306_numdec_buffer[USINT2DECASCII_MAX_DIGITS + 1];
  62. void ssd1306tx_numdec(uint16_t num)
  63. {
  64. ssd1306_numdec_buffer[USINT2DECASCII_MAX_DIGITS] = '\0'; // Terminate the string.
  65. uint8_t digits = usint2decascii(num, ssd1306_numdec_buffer);
  66. ssd1306tx_string(ssd1306_numdec_buffer + digits);
  67. }
  68. void ssd1306tx_numdecp(uint16_t num)
  69. {
  70. ssd1306_numdec_buffer[USINT2DECASCII_MAX_DIGITS] = '\0'; // Terminate the string.
  71. usint2decascii(num, ssd1306_numdec_buffer);
  72. ssd1306tx_string(ssd1306_numdec_buffer);
  73. }
  74. // ----------------------------------------------------------------------------
  75. void ssd1306tx_stringxy(const uint8_t *fron_src, uint8_t x, uint8_t y, const char s[])
  76. {
  77. uint16_t j, k = 0;
  78. while (s[k] != '\0') {
  79. j = s[k] * 16 - (32 * 16); // Convert ASCII code to font data index. NOTE: (x*16) already optimized to (x<<4).
  80. if (x > 120) {
  81. x = 0; // Go to the next line.
  82. y++;
  83. }
  84. ssd1306_setpos(x, y);
  85. ssd1306_start_data();
  86. for (uint8_t i = 0; i < 8; i++) {
  87. ssd1306_data_byte(pgm_read_byte(&fron_src[j + i]));
  88. }
  89. ssd1306_stop();
  90. ssd1306_setpos(x, y + 1);
  91. ssd1306_start_data();
  92. for (uint8_t i = 0; i < 8; i++) {
  93. ssd1306_data_byte(pgm_read_byte(&fron_src[j + i + 8]));
  94. }
  95. ssd1306_stop();
  96. x += 8;
  97. k++;
  98. }
  99. }
  100. // ============================================================================