readline.c 2.8 KB

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