Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1.  
  2. #define LED 13
  3. #define BAUDRATE 57600
  4. #define DEBUGOUTPUT 0
  5. // checksum variables
  6. byte generatedChecksum = 0;
  7. byte checksum = 0;
  8. int queue[20]={0};
  9. int payloadLength = 0;
  10. byte payloadData[64] = {0};
  11. byte poorQuality = 0;
  12. byte attention = 0;
  13. byte meditation = 0;
  14. int eyeblink = 0;
  15. int blinkStrength = 0;
  16. int blinkLength = 0;
  17. byte vlen;
  18. int rawData;
  19. int _min, _max;
  20. byte tt = 0;
  21. // system variables
  22. long lastReceivedPacket = 0;
  23. boolean bigPacket = false;
  24. //////////////////////////
  25.  
  26. // Microprocessor Setup //
  27.  
  28. //////////////////////////
  29. void setup()
  30. {
  31.   pinMode(LED, OUTPUT);
  32.   Serial.begin(BAUDRATE);           // USB
  33. }
  34. ////////////////////////////////
  35. // Read data from Serial UART //
  36. ////////////////////////////////
  37. byte ReadOneByte()
  38. {
  39.   int ByteRead;
  40.   while(!Serial.available());
  41.   ByteRead = Serial.read();
  42.   return ByteRead;
  43. }
  44. /////////////
  45. //MAIN LOOP//
  46. /////////////
  47. int read_3byte_int(int i) {
  48.   return ((payloadData[i] << 16) + (payloadData[i+1] << 8) + payloadData[i+2]);
  49. }
  50. void loop()
  51. {
  52.   // Look for sync bytes
  53.   if(ReadOneByte() == 170)
  54.   {
  55.     if(ReadOneByte() == 170)
  56.     {
  57.         payloadLength = ReadOneByte();
  58.         if(payloadLength > 169)          //Payload length can not be greater than 169
  59.           return;
  60.         generatedChecksum = 0;      
  61.  
  62.         for(int i = 0; i < payloadLength; i++)
  63.         {
  64.           payloadData[i] = ReadOneByte();            //Read payload into memory
  65.           generatedChecksum += payloadData[i];
  66.         }  
  67.         checksum = ReadOneByte();         //Read checksum byte from stream    
  68.         generatedChecksum = 255 - generatedChecksum;   //Take one's compliment of generated checksum
  69.  
  70.         if(checksum == generatedChecksum)
  71.         {  
  72.           poorQuality = 200;
  73.           bigPacket = false;
  74.           attention = 0;
  75.           meditation = 0;
  76.  
  77.           for(int i = 0; i < payloadLength; i++)
  78.           {                                          // Parse the payload
  79.             switch (payloadData[i])
  80.             {
  81.             case 2:
  82.                 i++;          
  83.                 poorQuality = payloadData[i];
  84.                 break;
  85.             case 4:
  86.                 i++;
  87.                 attention = payloadData[i];                      
  88.                 break;
  89.             case 5:
  90.                 i++;
  91.                 meditation = payloadData[i];
  92.                 Serial.print("호이");
  93.                 bigPacket = true;
  94.                 break;
  95.            
  96.             case 0x80:
  97.                 digitalWrite(LED, LOW);          
  98.                 _min = 9999;
  99.                 _max = -9999;
  100.                 for(int j=8;j>=0;j--){
  101.                 if(_min >= queue[j]) _min = queue[j];
  102.                 if(_max <= queue[j]) _max = queue[j];
  103.                 queue[j+1] = queue[j];
  104.                 }
  105.                 i++;
  106.                 rawData = (payloadData[i+1] * 256);
  107.                 rawData += payloadData[i+2];
  108.                 queue[0] = rawData;
  109.  
  110.                 i+=2;
  111.                 if(_max - _min > 230){
  112. //                Serial.println("Blink!");
  113.                   blinkStrength ++;
  114.                 }
  115. //                delay(100);
  116.                 break;
  117.             case 0x83:
  118.                 i+=25;
  119.                 break;      
  120.             default:
  121.                 break;
  122.             } // switch
  123.  
  124.         } // for loop
  125.         if(bigPacket == true){
  126.           if(50 < poorQuality && poorQuality < 200){
  127.             Serial.println("기기가 연결되어있지 않음");
  128.             return;    
  129.           }
  130.           else if(10 < poorQuality){
  131.             Serial.println("신호가 너무 약함.");      
  132.             return;    
  133.           }
  134.           digitalWrite(LED, LOW);          
  135.           Serial.print("Attention : ");
  136.           Serial.print(attention, DEC);
  137.           Serial.print(" Mediation : ");
  138.           Serial.print(meditation, DEC);
  139.           Serial.print(" 눈 움직임 강도 : ");
  140.           Serial.print(blinkStrength, DEC);
  141.           Serial.print(" PoorQuality : ");
  142.           Serial.print(poorQuality, DEC);
  143.           if(blinkStrength > 10){
  144.             Serial.print("깜빡!");
  145.             digitalWrite(LED, HIGH);          
  146.           }
  147.           if(0 < blinkLength && blinkLength < 3){
  148.            
  149.             Serial.print("졸림 1차 경보!");          
  150.           }
  151.           else if(3 <= blinkLength && blinkLength < 5){
  152.          
  153.             Serial.print("졸림 2차 경보!");          
  154.           }
  155.           else if(5 <= blinkLength && blinkLength <= 7){
  156.             Serial.print("졸림 3차 경보!");          
  157.          
  158.           }
  159.           // 눈 움직임 강도가 10이하이고 Mediation < Attention 일떄
  160.          
  161.           if(attention < meditation && blinkStrength < 10){
  162.             blinkLength ++;
  163.             // blinkLength 를 증가시키고 조건에 만족안하면 초기화 시킨다.
  164.             // 즉 blinkLength 는 '조건'이 몇초동안 유지되는가를 담는 변수임
  165.           }
  166.           else {
  167.             blinkLength = 0;
  168.           }
  169.            Serial.print("\n");
  170.            blinkStrength = 0;
  171.         }
  172.     }
  173.     }
  174.   } // end if read 0xAA byte
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement