Solarbox Liquid V.006

Aus Open Source Ecology - Germany
Zur Navigation springen Zur Suche springen

This is still under development and acts as a kind of working horse, so please ignore all the redundant comments

/*
 * Liquid V.006
 * 
 * Graphical visualization of sampling- and logging-data
 * from an energy-management-system lika a SolarBox Charger
 * 
 * 
 * This project is part of the OpenSourceEcology Germany
 * Project SolarBox.  See http://opensourceecology.de
 * 
 * 15.09.2014 by OS/case
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 
import processing.serial.*;
 
Serial port;  // Create object from Serial class
int val;      // Data received from the serial port
// int[] values;
int[] a_values;
int[] v_values;
float zoom;

float bat_volt = 0.0;  // volt
float show_bat_volt = 0.0;
int bat_mvolt = 0;  // millivolt

float bat_amp = 0.0;   // ampere
float show_bat_amp = 0.0;
int bat_mamp = 0;   // milliampere

float solar_volt = 0.0;  // volt
float show_solar_volt = 0.0;
int solar_mvolt = 0;  // millivolt

int chargingflag = 0;
int duty = 0;
long timestamp;


int yrange = 10000;    // Display Range from +20V until -20V

float yfaktor =   (height*0.5) / (yrange * 1000);  // yfaktor tells how many millivolt is representet by one pixel 
 
// int winx = 1024;
// int winy = 480;

int winx = 1280;
int winy = 1024;


int offset = winy / 2; 
int vert_adjust = winy / 2;
int showflag = 0;
 
void setup() 
{
  // size(1024, 480);
  size(winx, winy);

  // print all available serial ports
  println(Serial.list());
  
  // Open the port that the board is connected to and use the same speed (9600 bps)
  // port = new Serial(this, Serial.list()[4], 9600);
  port = new Serial(this, Serial.list()[Serial.list().length-1], 9600); // Für PC-User
  port.bufferUntil('\n');
 
  a_values = new int[width];
  v_values = new int[width];
  zoom = 1.0f;
  smooth();
}
 
 
 
int getY(int val) {
  int newval = 0;
  
  // newval = (int)(height - val / 1023.0f * (height - 1));
  
  
  newval = (int)((val * offset)/yrange) + offset;
  // println("getY: " + val + ", " + newval);
  
  // newval = 240 - newval + 240;
  // newval = 240 - newval + vert_adjust;
  newval = offset - newval + vert_adjust;
  return newval;
  
  // return (int)(height - val / 1023.0f * (height - 1));
}
 
int getValue() {
  // int value = -1;
  int value = 30000;
  /*
  while (port.available() >= 3) {
    if (port.read() == 0xff) {
      value = (port.read() << 8) | (port.read());
    }
  }
  */
  
  // Beispiel: 0.00,-8.80

  bat_volt = 0.0;
  bat_amp = 0.0;

  String dataString = port.readStringUntil('\n');
  // value = port.read();
  
  if (dataString != null) 
  {
    float[] data = float(split(dataString, ","));
    
    bat_volt = data[0];
    bat_amp  = data[1];
    solar_volt = data[2];
    chargingflag = (int)data[3];
    duty = (int)data[4];
    // timestamp = data[5];
    
    // print(bat_volt);
    // print(", ");
    // println(bat_amp);

    show_bat_amp = bat_amp;
    show_bat_volt = bat_volt;
    show_solar_volt = solar_volt;
   
    bat_amp = bat_amp * 1000;
    bat_volt = bat_volt * 1000/10;
    solar_volt = solar_volt * 1000;
  
    bat_mamp = (int)bat_amp;
    bat_mvolt = (int)bat_volt;
    solar_mvolt = (int)solar_volt;
    // if(bat_mamp != 0) println("GetValue: " + bat_mvolt + ", " + bat_mamp);
 
    value = bat_mamp;

  }
  else showflag = 0;
  
  // if(value != 0)  println(value);
  return value;
}
 
/*
void pushValue(int value) {
  for (int i=0; i<width-1; i++)
    values[i] = values[i+1];
  values[width-1] = value;
}
*/


void pushValue(int a_value, int v_value) 
{
  
  for (int i=0; i<width-1; i++)
    a_values[i] = a_values[i+1];
  a_values[width-1] = a_value;

  for (int i=0; i<width-1; i++)
    v_values[i] = v_values[i+1];
  v_values[width-1] = v_value;
}



 
/* void drawLines() {
  stroke(255);
  
  int displayWidth = (int) (width / zoom);
  
  int k = values.length - displayWidth;
  
  int x0 = 0;
  int y0 = getY(values[k]);
  for (int i=1; i<displayWidth; i++) {
    k++;
    int x1 = (int) (i * (width-1) / (displayWidth-1));
    int y1 = getY(values[k]);
    line(x0, y0, x1, y1);
    x0 = x1;
    y0 = y1;
  }
}
*/ 

 
void drawLines() 
{
  // stroke(255);
  stroke(255,204, 0); // yellow
  int displayWidth = (int) (width / zoom);
  
  int k = a_values.length - displayWidth;
  int x0 = 0;
  int y0 = getY(a_values[k]);
  int x1 = 0;
  int y1 = 0;
  
  for (int i=1; i<displayWidth; i++) 
  {
    k++;
    x1 = (int) (i * (width-1) / (displayWidth-1));
    y1 = getY(a_values[k]);
    line(x0, y0, x1, y1);
    x0 = x1;
    y0 = y1;
  }
  
  // stroke(46,139,87);
  stroke(0,255,204); // cyan
  k = v_values.length - displayWidth;
  x0 = 0;
  y0 = getY(v_values[k]);
  for (int i=1; i<displayWidth; i++) 
  {
    k++;
    x1 = (int) (i * (width-1) / (displayWidth-1));
    y1 = getY(v_values[k]);
    line(x0, y0, x1, y1);
    x0 = x1;
    y0 = y1;
  }
}
 


 
void drawGrid() {
  
  int x1 = 0;
  int y1 = 0;
  int x2 = width;
  int y2 = 0;
    
  // Mittellinie
  stroke(10, 10, 250);
  line(0, height/2, width, height/2);
  
  // 10er Linie
  stroke(0, 120, 0);
  y1 = getY(10 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(9 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(8 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(7 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(6 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(5 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(4 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(3 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(2 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(1 * 1000);  line(x1, y1, x2, y1);
  // y1 = getY(0);  line(x1, y1, x2, y1);
  
  y1 = getY(-10 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-9 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-8 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-7 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-6 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-5 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-4 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-3 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-2 * 1000);  line(x1, y1, x2, y1);
  y1 = getY(-1 * 1000);  line(x1, y1, x2, y1);
  
  
        
}
 
void keyReleased() {
  switch (key) {
    case '+':
      zoom *= 2.0f;
      println(zoom);
      if ( (int) (width / zoom) <= 1 )
        zoom /= 2.0f;
      break;
    case '-':
      zoom /= 2.0f;
      if (zoom < 1.0f)
        zoom *= 2.0f;
      break;
      
   case 'a':
     yrange = yrange / 2;
      break;
            
   case 'b':
     yrange = yrange * 2;
      break;

   case 'k':
     vert_adjust = vert_adjust + 1;
      break;
            
   case 'l':
     vert_adjust = vert_adjust - 1;
      break;


  }
}
 
void draw()
{
  background(0);
  drawGrid();
  val = getValue();
  // if (val != -1) {
  if (val != 30000) 
  {
    // pushValue(val);
    pushValue(bat_mamp, bat_mvolt);
  }
  drawLines();
  
  textSize(16);
  
  fill(255, 204, 0);
  text(show_bat_amp, 10, 20);
  
  fill(0, 255, 204);
  text(show_bat_volt, 10, 40);
  
  fill(0, 255, 204);
  text(show_solar_volt, 10, 60);
  
  fill(0, 255, 204);
  text(chargingflag, 10, 80);
  
  fill(0, 255, 204);
  text(duty, 10, 100);
  
  
}