Keyboard firmwares for Atmel AVR and Cortex-M
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

USBSerial.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USBSERIAL_H
  19. #define USBSERIAL_H
  20. #include "USBCDC.h"
  21. #include "Stream.h"
  22. #include "CircBuffer.h"
  23. /**
  24. * USBSerial example
  25. *
  26. * @code
  27. * #include "mbed.h"
  28. * #include "USBSerial.h"
  29. *
  30. * //Virtual serial port over USB
  31. * USBSerial serial;
  32. *
  33. * int main(void) {
  34. *
  35. * while(1)
  36. * {
  37. * serial.printf("I am a virtual serial port\n");
  38. * wait(1);
  39. * }
  40. * }
  41. * @endcode
  42. */
  43. class USBSerial: public USBCDC, public Stream {
  44. public:
  45. /**
  46. * Constructor
  47. *
  48. * @param vendor_id Your vendor_id (default: 0x1f00)
  49. * @param product_id Your product_id (default: 0x2012)
  50. * @param product_release Your preoduct_release (default: 0x0001)
  51. * @param connect_blocking define if the connection must be blocked if USB not plugged in
  52. *
  53. */
  54. USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking){
  55. settingsChangedCallback = 0;
  56. };
  57. /**
  58. * Send a character. You can use puts, printf.
  59. *
  60. * @param c character to be sent
  61. * @returns true if there is no error, false otherwise
  62. */
  63. virtual int _putc(int c);
  64. /**
  65. * Read a character: blocking
  66. *
  67. * @returns character read
  68. */
  69. virtual int _getc();
  70. /**
  71. * Check the number of bytes available.
  72. *
  73. * @returns the number of bytes available
  74. */
  75. uint8_t available();
  76. /** Determine if there is a character available to read
  77. *
  78. * @returns
  79. * 1 if there is a character available to read,
  80. * 0 otherwise
  81. */
  82. int readable() { return available() ? 1 : 0; }
  83. /** Determine if there is space available to write a character
  84. *
  85. * @returns
  86. * 1 if there is space to write a character,
  87. * 0 otherwise
  88. */
  89. int writeable() { return 1; } // always return 1, for write operation is blocking
  90. /**
  91. * Write a block of data.
  92. *
  93. * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.
  94. *
  95. * @param buf pointer on data which will be written
  96. * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)
  97. *
  98. * @returns true if successfull
  99. */
  100. bool writeBlock(uint8_t * buf, uint16_t size);
  101. /**
  102. * Attach a member function to call when a packet is received.
  103. *
  104. * @param tptr pointer to the object to call the member function on
  105. * @param mptr pointer to the member function to be called
  106. */
  107. template<typename T>
  108. void attach(T* tptr, void (T::*mptr)(void)) {
  109. if((mptr != NULL) && (tptr != NULL)) {
  110. rx.attach(tptr, mptr);
  111. }
  112. }
  113. /**
  114. * Attach a callback called when a packet is received
  115. *
  116. * @param fptr function pointer
  117. */
  118. void attach(void (*fptr)(void)) {
  119. if(fptr != NULL) {
  120. rx.attach(fptr);
  121. }
  122. }
  123. /**
  124. * Attach a callback to call when serial's settings are changed.
  125. *
  126. * @param fptr function pointer
  127. */
  128. void attach(void (*fptr)(int baud, int bits, int parity, int stop)) {
  129. settingsChangedCallback = fptr;
  130. }
  131. protected:
  132. virtual bool EPBULK_OUT_callback();
  133. virtual void lineCodingChanged(int baud, int bits, int parity, int stop){
  134. if (settingsChangedCallback) {
  135. settingsChangedCallback(baud, bits, parity, stop);
  136. }
  137. }
  138. private:
  139. FunctionPointer rx;
  140. CircBuffer<uint8_t,128> buf;
  141. void (*settingsChangedCallback)(int baud, int bits, int parity, int stop);
  142. };
  143. #endif