u8g_com_api.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. u8g_com_api.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8g.h"
  28. uint8_t u8g_InitCom(u8g_t *u8g, u8g_dev_t *dev, uint8_t clk_cycle_time)
  29. {
  30. return dev->com_fn(u8g, U8G_COM_MSG_INIT, clk_cycle_time, NULL);
  31. }
  32. void u8g_StopCom(u8g_t *u8g, u8g_dev_t *dev)
  33. {
  34. dev->com_fn(u8g, U8G_COM_MSG_STOP, 0, NULL);
  35. }
  36. /* cs contains the chip number, which should be enabled */
  37. void u8g_SetChipSelect(u8g_t *u8g, u8g_dev_t *dev, uint8_t cs)
  38. {
  39. dev->com_fn(u8g, U8G_COM_MSG_CHIP_SELECT, cs, NULL);
  40. }
  41. void u8g_SetResetLow(u8g_t *u8g, u8g_dev_t *dev)
  42. {
  43. dev->com_fn(u8g, U8G_COM_MSG_RESET, 0, NULL);
  44. }
  45. void u8g_SetResetHigh(u8g_t *u8g, u8g_dev_t *dev)
  46. {
  47. dev->com_fn(u8g, U8G_COM_MSG_RESET, 1, NULL);
  48. }
  49. void u8g_SetAddress(u8g_t *u8g, u8g_dev_t *dev, uint8_t address)
  50. {
  51. dev->com_fn(u8g, U8G_COM_MSG_ADDRESS, address, NULL);
  52. }
  53. uint8_t u8g_WriteByte(u8g_t *u8g, u8g_dev_t *dev, uint8_t val)
  54. {
  55. return dev->com_fn(u8g, U8G_COM_MSG_WRITE_BYTE, val, NULL);
  56. }
  57. uint8_t u8g_WriteSequence(u8g_t *u8g, u8g_dev_t *dev, uint8_t cnt, uint8_t *seq)
  58. {
  59. return dev->com_fn(u8g, U8G_COM_MSG_WRITE_SEQ, cnt, seq);
  60. }
  61. uint8_t u8g_WriteSequenceP(u8g_t *u8g, u8g_dev_t *dev, uint8_t cnt, const uint8_t *seq)
  62. {
  63. return dev->com_fn(u8g, U8G_COM_MSG_WRITE_SEQ_P, cnt, (void *)seq);
  64. }
  65. /*
  66. sequence := { direct_value | escape_sequence }
  67. direct_value := 0..254
  68. escape_sequence := value_255 | sequence_end | delay | adr | cs | not_used
  69. value_255 := 255 255
  70. sequence_end = 255 254
  71. delay := 255 0..127
  72. adr := 255 0x0e0 .. 0x0ef
  73. cs := 255 0x0d0 .. 0x0df
  74. not_used := 255 101..254
  75. #define U8G_ESC_DLY(x) 255, ((x) & 0x7f)
  76. #define U8G_ESC_CS(x) 255, (0xd0 | ((x)&0x0f))
  77. #define U8G_ESC_ADR(x) 255, (0xe0 | ((x)&0x0f))
  78. #define U8G_ESC_VCC(x) 255, (0xbe | ((x)&0x01))
  79. #define U8G_ESC_END 255, 254
  80. #define U8G_ESC_255 255, 255
  81. #define U8G_ESC_RST(x) 255, (0xc0 | ((x)&0x0f))
  82. */
  83. uint8_t u8g_WriteEscSeqP(u8g_t *u8g, u8g_dev_t *dev, const uint8_t *esc_seq)
  84. {
  85. uint8_t is_escape = 0;
  86. uint8_t value;
  87. for(;;)
  88. {
  89. value = u8g_pgm_read(esc_seq);
  90. if ( is_escape == 0 )
  91. {
  92. if ( value != 255 )
  93. {
  94. if ( u8g_WriteByte(u8g, dev, value) == 0 )
  95. return 0;
  96. }
  97. else
  98. {
  99. is_escape = 1;
  100. }
  101. }
  102. else
  103. {
  104. if ( value == 255 )
  105. {
  106. if ( u8g_WriteByte(u8g, dev, value) == 0 )
  107. return 0;
  108. }
  109. else if ( value == 254 )
  110. {
  111. break;
  112. }
  113. else if ( value >= 0x0f0 )
  114. {
  115. /* not yet used, do nothing */
  116. }
  117. else if ( value >= 0xe0 )
  118. {
  119. u8g_SetAddress(u8g, dev, value & 0x0f);
  120. }
  121. else if ( value >= 0xd0 )
  122. {
  123. u8g_SetChipSelect(u8g, dev, value & 0x0f);
  124. }
  125. else if ( value >= 0xc0 )
  126. {
  127. u8g_SetResetLow(u8g, dev);
  128. value &= 0x0f;
  129. value <<= 4;
  130. value+=2;
  131. u8g_Delay(value);
  132. u8g_SetResetHigh(u8g, dev);
  133. u8g_Delay(value);
  134. }
  135. else if ( value >= 0xbe )
  136. {
  137. /* not yet implemented */
  138. /* u8g_SetVCC(u8g, dev, value & 0x01); */
  139. }
  140. else if ( value <= 127 )
  141. {
  142. u8g_Delay(value);
  143. }
  144. is_escape = 0;
  145. }
  146. esc_seq++;
  147. }
  148. return 1;
  149. }