Week 16 Interface and Application Programming

 

For this assignment at Kuwait Fab Lab I made an interface to the “Synchronous light reflection board” input data.I used the Processing language and Openprocessing library.

 
First I tried to understand the basics and principles with hello reflect codes.

The Hello.reflect.c code :


Basically, the hello.reflect.c code ´s core is:
 for (count = 0; count < nloop; ++count) {
//
// LED off
//
set(led_port, led_pin);
//
// initiate conversion
//
ADCSRA |= (1 << ADSC);
//
// wait for completion
//
while (ADCSRA & (1 << ADSC))
;
//
// save result
//
off += ADC;
//
// LED on
//
clear(led_port, led_pin);
//
// initiate conversion
//
ADCSRA |= (1 << ADSC);
//
// wait for completion
//
while (ADCSRA & (1 << ADSC))
;
//
// save result
//
on += ADC;
}
//
// send framing
//
put_char(&serial_port, serial_pin_out, 1);
char_delay();
put_char(&serial_port, serial_pin_out, 2);
char_delay();
put_char(&serial_port, serial_pin_out, 3);
char_delay();
put_char(&serial_port, serial_pin_out, 4);
char_delay();
//
// send result
//
put_char(&serial_port, serial_pin_out, (on & 255));
char_delay();
put_char(&serial_port, serial_pin_out, ((on >> 8) & 255));
char_delay();
put_char(&serial_port, serial_pin_out, (off & 255));
char_delay();
put_char(&serial_port, serial_pin_out, ((off >> 8) & 255));
char_delay();
}

This code´s core shows us that the board will send to USB : 1,2,3,4 numbers after this four large numbers as two “on” numbers and two “off” numbers. These “on” and “off” numbers are the lecture data collected by the board.


The Hello.reflect.py code :


The hello.reflect.py is a script that will draw the interface to boards input data. this script will read the 1,2,3,4 numbers respectively and the “on” (two numbers) and “off (two numbers) , After reading form the USB serial the two “on” numbers, the script will execute:
on_value = (256*on_high + on_low)/nloop
That is the final “on” value, that will be shown. The same step happens with the “off” value. The third data is the “filter” that is calculated through this equation:
filter = (1-eps)*filter + eps*amp*(on_value-off_value)


The Processing code :


With the provided informations above , I start to work with processing language like :
First I import: processing.serial and Serial my Port to read the USB data. then a void serialEvent () that will read the 1,2,3,4 respectively after this , the two “on” numbers and two “off” numbers. I used the Switch command with 7 cases finally the script do this command (same from Neil´s hello.reflect.py) :
on = (float) (on_L + on_H*256)/nloop;
off = (float) (off_L + off_H*256)/nloop;

This is the “on” and “off” final numbers, then next script I calculate the “filter” ( same from Neil´s hello.reflect.py):
filtr = (1-eps)*filtr + eps*amp*(on-off).

(I used filtr instead of filter because filter is a command in Processing).

 

The Interface using Processing :


With these inputs :( on, off and filtr) , I designed my interface with this code from http://www.openprocessing.org/.
My core´s code is:

 

### AHMAD ALENEZI
void draw()
{
background(255);
fill(200,90,11,100);
float w_on =map(on, 0, 1024, 0, width);
for (int i=0; i < x.length; i++) {
float dia = dist(y[i], x[i], w_on, w_on);
rect(x[i], y[i], dia, dia);
}
fill(200,90,11,100);
float w_off =map(off, 0, 1024, 0, width);
stroke(255)
for (int i=0; i < x.length; i++) {
float dia = dist(w_off, y[i], w_off, x[i]);
rect(x[i], y[i], dia, dia);
}
filtr = (1-eps)*filtr + eps*amp*(on-off);


fill(200,90,11,63);
float w_filtr =map(filtr, 0, 4024, 0, width);
for (int i=0; i < x.length; i++) {
float dia = dist(x[i], filtr, filtr, y[i]);
rect(x[i], y[i], dia, dia);
}

 

This script designed many rectangles that reacts by , on, off and filtr data collected from the board.