readline.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "ets_sys.h"
  2. #include "os_type.h"
  3. #include "osapi.h"
  4. #include "driver/uart.h"
  5. #include "c_types.h"
  6. LOCAL os_timer_t readline_timer;
  7. // UartDev is defined and initialized in rom code.
  8. extern UartDevice UartDev;
  9. #define uart_putc uart0_putc
  10. bool uart_getc(char *c){
  11. RcvMsgBuff *pRxBuff = &(UartDev.rcv_buff);
  12. if(pRxBuff->pWritePos == pRxBuff->pReadPos){ // empty
  13. return false;
  14. }
  15. // ETS_UART_INTR_DISABLE();
  16. ETS_INTR_LOCK();
  17. *c = (char)*(pRxBuff->pReadPos);
  18. if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
  19. pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ;
  20. } else {
  21. pRxBuff->pReadPos++;
  22. }
  23. // ETS_UART_INTR_ENABLE();
  24. ETS_INTR_UNLOCK();
  25. return true;
  26. }
  27. #if 0
  28. int readline4lua(const char *prompt, char *buffer, int length){
  29. char ch;
  30. int line_position;
  31. start:
  32. /* show prompt */
  33. uart0_sendStr(prompt);
  34. line_position = 0;
  35. os_memset(buffer, 0, length);
  36. while (1)
  37. {
  38. while (uart_getc(&ch))
  39. {
  40. /* handle CR key */
  41. if (ch == '\r')
  42. {
  43. char next;
  44. if (uart_getc(&next))
  45. ch = next;
  46. }
  47. /* backspace key */
  48. else if (ch == 0x7f || ch == 0x08)
  49. {
  50. if (line_position > 0)
  51. {
  52. uart_putc(0x08);
  53. uart_putc(' ');
  54. uart_putc(0x08);
  55. line_position--;
  56. }
  57. buffer[line_position] = 0;
  58. continue;
  59. }
  60. /* EOF(ctrl+d) */
  61. else if (ch == 0x04)
  62. {
  63. if (line_position == 0)
  64. /* No input which makes lua interpreter close */
  65. return 0;
  66. else
  67. continue;
  68. }
  69. /* end of line */
  70. if (ch == '\r' || ch == '\n')
  71. {
  72. buffer[line_position] = 0;
  73. uart_putc('\n');
  74. if (line_position == 0)
  75. {
  76. /* Get a empty line, then go to get a new line */
  77. goto start;
  78. }
  79. else
  80. {
  81. return line_position;
  82. }
  83. }
  84. /* other control character or not an acsii character */
  85. if (ch < 0x20 || ch >= 0x80)
  86. {
  87. continue;
  88. }
  89. /* echo */
  90. uart_putc(ch);
  91. buffer[line_position] = ch;
  92. ch = 0;
  93. line_position++;
  94. /* it's a large line, discard it */
  95. if (line_position >= length)
  96. line_position = 0;
  97. }
  98. }
  99. }
  100. #endif