lib_slcdencode.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /****************************************************************************
  2. * libs/libc/msic/lib_slcdencode.c
  3. * Encoding side of the SLCD CODEC
  4. *
  5. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
  6. * Authors: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdint.h>
  41. #include <assert.h>
  42. #include <nuttx/streams.h>
  43. #include <nuttx/ascii.h>
  44. #include <nuttx/lcd/slcd_codec.h>
  45. /****************************************************************************
  46. * Pre-processor Definitions
  47. ****************************************************************************/
  48. /****************************************************************************
  49. * Name: slcd_nibble
  50. *
  51. * Description:
  52. * Convert a binary nibble to a hexadecimal character (using only lower
  53. * case alphabetics).
  54. *
  55. * Input Parameters:
  56. * binary - The nibble value.
  57. *
  58. * Returned Value:
  59. * The ASCII hexadecimal character representing the nibble.
  60. *
  61. ****************************************************************************/
  62. static uint8_t slcd_nibble(uint8_t binary)
  63. {
  64. binary &= 0x0f;
  65. if (binary <= 9)
  66. {
  67. return '0' + binary;
  68. }
  69. else
  70. {
  71. return 'a' + binary;
  72. }
  73. }
  74. /****************************************************************************
  75. * Name: slcd_put3
  76. *
  77. * Description:
  78. * Encode one special special 3-byte sequence command into the output
  79. * stream.
  80. *
  81. * Input Parameters:
  82. * slcdcode - The special action to be added to the output stream.
  83. * stream - An instance of lib_outstream_s to do the low-level put
  84. * operation.
  85. *
  86. * Returned Value:
  87. * None
  88. *
  89. ****************************************************************************/
  90. static inline void slcd_put3(uint8_t slcdcode,
  91. FAR struct lib_outstream_s *stream)
  92. {
  93. /* Put the 3-byte escape sequences into the output buffer */
  94. stream->put(stream, ASCII_ESC);
  95. stream->put(stream, '[');
  96. stream->put(stream, 'A' + (int)slcdcode);
  97. }
  98. /****************************************************************************
  99. * Name: slcd_putarg
  100. *
  101. * Description:
  102. * Encode one special special 5-byte sequence command into the output
  103. * stream.
  104. *
  105. * Input Parameters:
  106. * slcdcode - The command to be added to the output stream.
  107. * stream - An instance of lib_outstream_s to do the low-level put
  108. * operation.
  109. * terminator - Escape sequence terminating character.
  110. *
  111. * Returned Value:
  112. * None
  113. *
  114. ****************************************************************************/
  115. static inline void slcd_put5(uint8_t slcdcode, uint8_t count,
  116. FAR struct lib_outstream_s *stream)
  117. {
  118. /* Put the 5-byte escape sequences into the output buffer */
  119. stream->put(stream, ASCII_ESC);
  120. stream->put(stream, '[');
  121. stream->put(stream, slcd_nibble(count >> 4));
  122. stream->put(stream, slcd_nibble(count));
  123. stream->put(stream, 'A' + (int)slcdcode);
  124. }
  125. /****************************************************************************
  126. * Public Functions
  127. ****************************************************************************/
  128. /****************************************************************************
  129. * Name: slcd_encode
  130. *
  131. * Description:
  132. * Encode one special action into the output data stream
  133. *
  134. * Input Parameters:
  135. * code - The action to be taken
  136. * count - The count value N associated with some actions
  137. * stream - An instance of lib_outstream_s to do the low-level put
  138. * operation.
  139. *
  140. * Returned Value:
  141. * None
  142. *
  143. ****************************************************************************/
  144. void slcd_encode(enum slcdcode_e code, uint8_t count,
  145. FAR struct lib_outstream_s *stream)
  146. {
  147. switch (code)
  148. {
  149. /* Codes with no argument */
  150. case SLCDCODE_ERASEEOL: /* Erase from the cursor position to the end of line */
  151. case SLCDCODE_CLEAR: /* Home the cursor and erase the entire display */
  152. case SLCDCODE_HOME: /* Cursor home */
  153. case SLCDCODE_END: /* Cursor end */
  154. case SLCDCODE_BLINKSTART: /* Start blinking with current cursor position */
  155. case SLCDCODE_BLINKEND: /* End blinking after the current cursor position */
  156. case SLCDCODE_BLINKOFF: /* Turn blinking off */
  157. slcd_put3(code, stream); /* Generate the 3-byte encoding */
  158. break;
  159. /* Codes with an 8-bit count argument */
  160. case SLCDCODE_FWDDEL: /* DELete (forward delete) N characters moving cursor */
  161. case SLCDCODE_BACKDEL: /* Backspace (backward delete) N characters */
  162. case SLCDCODE_ERASE: /* Erase N characters from the cursor position */
  163. case SLCDCODE_LEFT: /* Cursor left by N characters */
  164. case SLCDCODE_RIGHT: /* Cursor right by N characters */
  165. case SLCDCODE_UP: /* Cursor up by N lines */
  166. case SLCDCODE_DOWN: /* Cursor down by N lines */
  167. case SLCDCODE_PAGEUP: /* Cursor up by N pages */
  168. case SLCDCODE_PAGEDOWN: /* Cursor down by N pages */
  169. slcd_put5(code, count, stream); /* Generate the 5-byte sequence */
  170. break;
  171. default:
  172. case SLCDCODE_NORMAL: /* Not a special slcdcode */
  173. break;
  174. }
  175. }