Browse Source

improved stability

subDesTagesMitExtraKaese 4 years ago
parent
commit
9d5e4a726a
10 changed files with 135 additions and 52 deletions
  1. 5 1
      .gitignore
  2. 2 1
      connection.py
  3. 67 11
      graphic-eq.py
  4. 9 4
      include/config.hpp
  5. 0 2
      include/matrix.hpp
  6. 3 1
      platformio.ini
  7. 11 4
      src/config.cpp
  8. 22 21
      src/main.cpp
  9. 9 2
      src/matrix.cpp
  10. 7 5
      src/serialClient.cpp

+ 5 - 1
.gitignore

@@ -4,4 +4,8 @@
 .vscode/launch.json
 .vscode/ipch
 
-*.gif
+__pycache__
+*.gif
+/build
+/dist
+*.spec

+ 2 - 1
connection.py

@@ -61,7 +61,8 @@ class SerialConnection:
   def read(self, n=64):
     if self._ser:
       try:
-        return self._ser.read(n)
+        if self._ser.in_waiting:
+          return self._ser.read(n)
       except serial.SerialException:
         self._ser = None
     if not self._ser:

+ 67 - 11
graphic-eq.py

@@ -2,28 +2,84 @@ from connection import *
 from audioHandler import Listener
 import numpy as np
 import time
+import math
+import sys, getopt
+
+framerate = 60 #Hz
+dataTime = 1/30
+graphs = True
+outDev = True
+port = None
+
+try:
+  opts, args = getopt.getopt(sys.argv[1:],"hsif:t:b:w:",["shadow=","input=","framerate=","time=","port="])
+except getopt.GetoptError:
+  print('main.py -s (shadow) -i (use input) -f --framerate <n=40>\n        -t --time <t=0.033> -p --port <COM3>')
+  sys.exit(2)
+for opt, arg in opts:
+  if opt == '-h':
+     print('main.py -s (shadow) -i (use input) -f --framerate <n=40>\n        -t --time <t=0.033> -p --port <COM3>')
+     sys.exit()
+  elif opt in ("-s", "--shadow"):
+    graphs = False
+  elif opt in ("-i", "--input"):
+    outDev = False
+  elif opt in ("-f", "--framerate"):
+    framerate = float(arg)
+  elif opt in ("-t", "--time"):
+    dataTime = float(arg)
+  elif opt in ("-p", "--port"):
+    port = arg
 
 matrix = SerialConnection()
-matrix.open()
+matrix.open(port)
 
-pitches = [(2**(1/12))**(n/11*12*7-24) * 220 for n in range(11)]
-audio = Listener(1/30, 30)
+pitches = [(2**(1/12))**(n/11*12*7-30) * 220 for n in range(11)]
+audio = Listener(dataTime, 30, not outDev)
 audio.start()
+
 nFFT = audio.buffersize
 audio.fftSetLimits(nFFT, min(pitches), max(pitches))
 audio.agcFftSetLimits(60/60, 300/60)
 
+maxVolRow = [0] * N_COLS
+maxVolAge = [0] * N_COLS
+intervalTime = time.time()
+
 while True:
   if audio.hasNewData():
-    left, right, fft = np.copy(audio.left), np.copy(audio.right), np.copy(audio.fft)
-    
+    fft = np.copy(audio.fft)
     sums = audio.fftGroup(fft, pitches)
     data = [0] * 50
-    for row in range(N_ROWS):
-      for col in range(N_COLS):
-        val = 15 if sums[col] > ((N_ROWS-row)/N_COLS/2) else 0
-        data[row*N_COLS+col]= val
-    print(data)
+    for col in range(N_COLS):
+      curVal = sums[col] * len(sums) * 1.1**col
+      curRow = max(N_ROWS - int(curVal), 0)
+
+      if curRow <= maxVolRow[col]:
+        maxVolRow[col] = curRow
+        maxVolAge[col] = 30
+      elif maxVolAge[col] > 0:
+        maxVolAge[col] -= 1
+      else:
+        maxVolRow[col] = N_ROWS
+
+      if maxVolRow[col] < N_ROWS:
+        data[maxVolRow[col]*N_COLS+col] = int(maxVolAge[col]/10)
+
+      if curRow < N_ROWS:
+        for row in range(curRow, N_ROWS):
+          data[row*N_COLS+col] = 15-curRow-row
+    data[49] = int(data[49]/2)
+    #print(data)
     matrix.send(b'A' + bytes(data))
 
-    time.sleep(1/40)
+  time.sleep(1/30)
+
+  if time.time() - intervalTime > 60:
+    if not matrix.isConnected():
+      print("lost serial connection, retrying to connect...")
+      matrix.connect()
+      
+    audio.stop()
+    audio.start()
+    intervalTime = time.time()

+ 9 - 4
include/config.hpp

@@ -7,10 +7,15 @@
 #include <EEPROM.h>
 
 struct config_t {
-  char ssid[16];
-  char pass[16];
-  char host[24];
-  int port;
+  union {
+    struct{
+      char ssid[16];
+      char pass[16];
+      char host[24];
+      int port;
+    };
+    uint8_t bytes[16+16+24];
+  };
 };
 
 extern config_t config;

+ 0 - 2
include/matrix.hpp

@@ -24,6 +24,4 @@ void matrix_init();
 void writeRow(const uint8_t& nRow, const uint16_t& values);
 void nextRow();
 
-extern uint16_t n;
-
 #endif

+ 3 - 1
platformio.ini

@@ -17,4 +17,6 @@ build_flags = -O3
 
 #upload_port = /dev/ttyUSB1
 #monitor_port = /dev/ttyUSB1
-monitor_speed = 38400
+monitor_speed = 38400
+
+board_build.f_cpu = 16000000L

+ 11 - 4
src/config.cpp

@@ -3,11 +3,18 @@
 config_t config;
 
 void config_init() {
-  for(byte i=0; i<sizeof(config); i++)
-    *(config.ssid+i) = EEPROM.read(i);
+  if(EEPROM.read(1023) == 42) {
+    for(size_t i=0; i<sizeof(config_t); i++)
+      *(config.ssid+i) = EEPROM.read(i+128);
+  } else {
+    config.ssid[0] = 0;
+  }
 }
 
 void config_save() {
-  for(byte i=0; i<sizeof(config); i++)
-    EEPROM.write(i, *(config.ssid+i));
+  noInterrupts();
+  for(size_t i=0; i<sizeof(config_t); i++)
+    EEPROM.update(i+128, *(config.ssid+i));
+  EEPROM.update(1023, 42);
+  interrupts();
 }

+ 22 - 21
src/main.cpp

@@ -12,28 +12,46 @@ char mode = 0;
 unsigned long startS = 0, startW = 0;
 
 void setup() {
+  matrix_init();
   pinMode(3, OUTPUT);
   digitalWrite(3, HIGH);
+  delay(10);
 
   Serial.begin(38400);
   printf("boot\n");
+  delay(10);
   
   wdt_enable(WDTO_8S);
+  delay(10);
   config_init();
-  matrix_init();
   
   delay(100);
-
+  
   if(RedFly.init(38400, HIGH_POWER)) {
     debugout("INIT ERR\n"); //there are problems with the communication between the Arduino and the RedFly
     memset(matrix.bytes, 0xf, sizeof(matrix_t));
     for(;;);
   }
-
-  connect();
+  delay(200);
 }
 
 void loop() {
+  if(mode == 0 || mode == 'C'){
+    RedFly.disable();
+    startS = millis();
+    wdt_reset();
+
+    while(millis()-startS < 5000) {
+      int8_t rc = readSerial();
+      if(rc == 0) {
+        mode = 'S';
+        return;
+      }
+    }
+    Serial.println("NO DATA");
+    matrix.bytes[30] ^= 0xF;
+    RedFly.enable();
+  }
   if(mode == 0) {
     uint8_t rssi = RedFly.getrssi();
     if(rssi == 0) {
@@ -64,21 +82,4 @@ void loop() {
   if(mode == 'S') {
     readSerial();
   }
-  if(mode == 0 || mode == 'C'){
-    RedFly.disable();
-    Serial.println("NO DATA");
-    matrix.bytes[30] ^= 0xF;
-    startS = millis();
-    wdt_reset();
-
-    while(millis()-startS < 5000) {
-      int8_t rc = readSerial();
-      if(rc == 0) {
-        mode = 'S';
-        RedFly.disable();
-        return;
-      }
-    }
-    RedFly.enable();
-  }
 }

+ 9 - 2
src/matrix.cpp

@@ -13,7 +13,7 @@ matrix_t matrix = {
  15, 0, 0,15, 0, 0, 0,15, 0,15,
  13,15, 1,10,15,15, 0,15,15, 2
 };
-uint16_t n = 0;
+uint8_t n = 0;
 
 void matrix_init() {
 
@@ -21,6 +21,7 @@ void matrix_init() {
     pinMode(i, OUTPUT);
     digitalWrite(i, 0);
   }
+  n = 1;
 
   noInterrupts(); // disable all interrupts
   // initialize Timer1
@@ -43,6 +44,13 @@ ISR(TIMER1_COMPA_vect) {
     
     if(++currentVal >= 15) {
       currentVal = 0;
+
+      //stop on reboot
+      if(n == 0) {
+        noInterrupts();
+        TIMSK1 &= ~(1 << OCIE1A);
+        interrupts();
+      }
     }
 
     OCR1A = cieCycles[currentVal];
@@ -62,7 +70,6 @@ ISR(TIMER1_COMPA_vect) {
     (row.bytes[9] > currentVal ? 1 << (N_COLS - 10) : 0);
 
   writeRow(currentRow, pixels);
-  n++;
 }
 
 

+ 7 - 5
src/serialClient.cpp

@@ -1,8 +1,9 @@
 #include "serialClient.hpp"
+uint16_t len;
+uint8_t buf[6];
 
 int8_t readSerial() {
-  uint16_t len = Serial.available();
-  uint8_t buf[6];
+  len = Serial.available();  
 
   if(len < N_ROWS*N_COLS+1) {
     return -1;
@@ -24,8 +25,8 @@ int8_t readSerial() {
       config.port = Serial.parseInt();
       if(config.port > 0) {
         wdt_reset();
-        memset(matrix.bytes, 0xF, sizeof(matrix_t));
-        matrix.bytes[14] = 0x0F;
+        memset(matrix.bytes, 0xa, sizeof(matrix_t));
+        matrix.bytes[14] = 0x00;
         config_save();
         printf("Config saved.\n");
         for(;;);
@@ -45,8 +46,9 @@ int8_t readSerial() {
   }
 
   len = Serial.readBytes(matrix.bytes, N_ROWS*N_COLS);
+  
   sprintf((char*)buf, "%d\n", len);
   Serial.write(buf, 3);
-
+  
   return 0;
 }