ThinkGearStreamParser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. @class: ThinkGearStreamParser
  3. @description: This class was developed using the code freely available at
  4. www.neurosky.com. It is an adpatation from the C files: ThinkGearStreamparser.h and
  5. ThinkGearStreamParser.c into a Qt C++ class.
  6. @author: Mirko Perkusich
  7. Taciana Rached
  8. */
  9. /*
  10. Copyright (c) 2010 Embedded Systems and Pervasive Laboratory, Federal
  11. University of Campina Grande, Brazil, Angelo Perkusich, Mirko Perkusich,
  12. Taciana Rached
  13. Permission is hereby granted, free of charge, to any person obtaining a
  14. copy of this software and associated documentation files (the
  15. "Software"), to deal in the Software without restriction, including
  16. without limitation the rights to use, copy, modify, merge, publish,
  17. distribute, sublicense, and/or sell copies of the Software, and to
  18. permit persons to whom the Software is furnished to do so, subject to
  19. the following conditions:
  20. The above copyright notice and this permission notice shall be included
  21. in all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  26. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  27. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  28. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. /*
  31. * @(#)ThinkGearStreamParser.h 2.0 Mar 04, 2008
  32. *
  33. * Copyright (c) 2008 NeuroSky, Inc. All Rights Reserved
  34. * NEUROSKY PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  35. */
  36. /* Ensure this header is only included once */
  37. #ifndef THINKGEAR_STREAM_PARSER_H_
  38. #define THINKGEAR_STREAM_PARSER_H_
  39. /**
  40. * @file ThinkGearStreamParser.h
  41. *
  42. * The ThinkGear Stream Parser is used to parse bytes of ThinkGear
  43. * data streamed from any source. Each arriving byte is fed into
  44. * the Parser using the THINKGEAR_parseByte() function, and a user
  45. * supplied callback function is called whenever DataRows are decoded
  46. * from complete ThinkGear Packets. Each Parser can be initialized
  47. * to recognize either: streams of ThinkGear Packets, or streams
  48. * of 2-byte raw values (the old 5V ThinkGear stream format).
  49. *
  50. * @author Kelvin Soo
  51. * @version 2.0 Mar 04, 2008 Kelvin Soo
  52. * - Renamed to ThinkGearStreamParser from ThinkGearStreamDecoder.
  53. * - Revised to call a callback function instead of stuffing arriving
  54. * data values into a ThinkGearData object.
  55. * - Renamed symbols according to updated Packet vocabulary.
  56. * @version 1.0 Nov 27, 2007 Kelvin Soo
  57. * - Initial version (ThinkGearStreamDecoder).
  58. */
  59. /* Include all external libraries required by this header */
  60. #include <stdlib.h>
  61. #include <QObject>
  62. #include <QDebug>
  63. /* Parser types */
  64. #define PARSER_TYPE_NULL 0x00
  65. #define PARSER_TYPE_PACKETS 0x01 /* Stream bytes as ThinkGear Packets */
  66. #define PARSER_TYPE_2BYTERAW 0x02 /* Stream bytes as 2-byte raw data */
  67. /* Data CODE definitions */
  68. #define PARSER_CODE_BATTERY 0x01
  69. #define PARSER_CODE_POOR_QUALITY 0x02
  70. #define PARSER_CODE_ATTENTION 0x04
  71. #define PARSER_CODE_MEDITATION 0x05
  72. #define PARSER_CODE_8BITRAW_SIGNAL 0x06
  73. #define PARSER_CODE_RAW_MARKER 0x07
  74. #define PARSER_CODE_RAW_SIGNAL 0x80
  75. #define PARSER_CODE_EEG_POWERS 0x81
  76. #define PARSER_CODE_ASIC_EEG_POWER_INT 0x83
  77. /**
  78. * The Parser is a state machine that manages the parsing state.
  79. */
  80. class ThinkGearStreamParser : public QObject
  81. {
  82. Q_OBJECT;
  83. public:
  84. /**
  85. * Constructor
  86. */
  87. explicit ThinkGearStreamParser();
  88. /**
  89. * Feeds the @c byte into the @c parser. If the @c byte completes a
  90. * complete, valid parser, then the @c parser's handleDataValue()
  91. * function is automatically called on each DataRow in the Packet.
  92. * The return value provides an indication of the state of the
  93. * @c parser after parsing the byte.
  94. *
  95. * @param parser Pointer to an initialized ThinkGearDataParser object.
  96. * @param byte The next byte of the data stream.
  97. *
  98. * @return -1 if @c parser is NULL.
  99. * @return -2 if a complete Packet was received, but the checksum failed.
  100. * @return -3 if an invalid Packet with PLENGTH > 170 was detected.
  101. * @return -4 if an invalid Packet with PLENGTH == 170 was detected.
  102. * @return -5 if the @c parser is somehow in an unrecognized state.
  103. * @return 0 if the @c byte did not yet complete a Packet.
  104. * @return 1 if a Packet was received and parsed successfully.
  105. *
  106. */
  107. int parseByte( unsigned char byte );
  108. private:
  109. unsigned char type;
  110. unsigned char state;
  111. unsigned char lastByte;
  112. unsigned char payloadLength;
  113. unsigned char payloadBytesReceived;
  114. unsigned char payload[256];
  115. unsigned char payloadSum;
  116. unsigned char chksum;
  117. /**
  118. * Parses each row of data from the @c packet's Data[] block,
  119. * updating the fields of @c data as appropriate.
  120. */
  121. int parsePacketPayload();
  122. /**
  123. * Identifies if the data represents meditation level or attention level and
  124. * sens the corresponding signal.
  125. */
  126. void handleDataValue( unsigned char extendedCodeLevel,
  127. unsigned char code, unsigned char numBytes,
  128. const unsigned char *value);
  129. signals:
  130. /** Sends the attention level */
  131. void attentionLevel(int aLevel);
  132. /** Sends the meditation level */
  133. void meditationLevel(int medLevel);
  134. };
  135. #endif /* THINKGEAR_STREAM_DECODER_H_ */