Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RESIZE.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (c) 2010 Andy Kirkham
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. 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
  17. THE SOFTWARE.
  18. */
  19. #include "MODSERIAL.h"
  20. #include "MACROS.h"
  21. namespace AjK {
  22. int
  23. MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check)
  24. {
  25. int rval = Ok;
  26. // If the requested size is the same as the current size there's nothing to do,
  27. // just continue to use the same buffer as it's fine as it is.
  28. if (buffer_size[type] == size) return rval;
  29. // Make sure the ISR cannot use the buffers while we are manipulating them.
  30. disableIrq();
  31. // If the requested buffer size is larger than the current size,
  32. // attempt to create a new buffer and use it.
  33. if (buffer_size[type] < size) {
  34. rval = upSizeBuffer(size, type, memory_check);
  35. }
  36. else if (buffer_size[type] > size) {
  37. rval = downSizeBuffer(size, type, memory_check);
  38. }
  39. // Start the ISR system again with the new buffers.
  40. enableIrq();
  41. return rval;
  42. }
  43. int
  44. MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check)
  45. {
  46. if (size >= buffer_count[type]) {
  47. return BufferOversize;
  48. }
  49. char *s = (char *)malloc(size);
  50. if (s == (char *)NULL) {
  51. if (memory_check) {
  52. error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
  53. }
  54. return NoMemory;
  55. }
  56. int c, new_in = 0;
  57. do {
  58. c = __getc(false);
  59. if (c != -1) s[new_in++] = (char)c;
  60. if (new_in >= size) new_in = 0;
  61. }
  62. while (c != -1);
  63. free((char *)buffer[type]);
  64. buffer[type] = s;
  65. buffer_in[type] = new_in;
  66. buffer_out[type] = 0;
  67. return Ok;
  68. }
  69. int
  70. MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check)
  71. {
  72. char *s = (char *)malloc(size);
  73. if (s == (char *)NULL) {
  74. if (memory_check) {
  75. error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
  76. }
  77. return NoMemory;
  78. }
  79. if (buffer_count[type] == 0) { // Current buffer empty?
  80. free((char *)buffer[type]);
  81. buffer[type] = s;
  82. buffer_in[type] = 0;
  83. buffer_out[type] = 0;
  84. }
  85. else { // Copy the current contents into the new buffer.
  86. int c, new_in = 0;
  87. do {
  88. c = __getc(false);
  89. if (c != -1) s[new_in++] = (char)c;
  90. if (new_in >= size) new_in = 0; // Shouldn't happen, but be sure.
  91. }
  92. while (c != -1);
  93. free((char *)buffer[type]);
  94. buffer[type] = s;
  95. buffer_in[type] = new_in;
  96. buffer_out[type] = 0;
  97. }
  98. buffer_size[type] = size;
  99. return Ok;
  100. }
  101. } // namespace AjK ends