main.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <u8g2.h>
  2. #include <util/delay.h>
  3. #include <u8x8_avr.h>
  4. #include <avr/power.h>
  5. #define CS_DDR DDRB
  6. #define CS_PORT PORTB
  7. #define CS_BIT 2
  8. #define DC_DDR DDRB
  9. #define DC_PORT PORTB
  10. #define DC_BIT 1
  11. #define RESET_DDR DDRB
  12. #define RESET_PORT PORTB
  13. #define RESET_BIT 0
  14. u8g2_t u8g2;
  15. uint8_t
  16. u8x8_gpio_and_delay (u8x8_t * u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  17. // Re-use library for delays
  18. if (u8x8_avr_delay(u8x8, msg, arg_int, arg_ptr))
  19. return 1;
  20. switch (msg) {
  21. // called once during init phase of u8g2/u8x8
  22. // can be used to setup pins
  23. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  24. CS_DDR |= _BV(CS_BIT);
  25. DC_DDR |= _BV(DC_BIT);
  26. RESET_DDR |= _BV(RESET_BIT);
  27. break;
  28. // CS (chip select) pin: Output level in arg_int
  29. case U8X8_MSG_GPIO_CS:
  30. if (arg_int)
  31. CS_PORT |= _BV(CS_BIT);
  32. else
  33. CS_PORT &= ~_BV(CS_BIT);
  34. break;
  35. // DC (data/cmd, A0, register select) pin: Output level in arg_int
  36. case U8X8_MSG_GPIO_DC:
  37. if (arg_int)
  38. DC_PORT |= _BV(DC_BIT);
  39. else
  40. DC_PORT &= ~_BV(DC_BIT);
  41. break;
  42. // Reset pin: Output level in arg_int
  43. case U8X8_MSG_GPIO_RESET:
  44. if (arg_int)
  45. RESET_PORT |= _BV(RESET_BIT);
  46. else
  47. RESET_PORT &= ~_BV(RESET_BIT);
  48. break;
  49. default:
  50. u8x8_SetGPIOResult(u8x8, 1);
  51. break;
  52. }
  53. return 1;
  54. }
  55. int main (void) {
  56. u8g2_Setup_sh1106_128x64_noname_f(
  57. &u8g2, U8G2_R0,
  58. u8x8_byte_avr_hw_spi,
  59. u8x8_gpio_and_delay
  60. );
  61. u8g2_InitDisplay(&u8g2);
  62. u8g2_SetPowerSave(&u8g2, 0);
  63. while (1) {
  64. u8g2_ClearBuffer(&u8g2);
  65. u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
  66. u8g2_DrawStr(&u8g2, 0, 15, "Hello!");
  67. u8g2_SendBuffer(&u8g2);
  68. }
  69. }