Skip to content

Commit

Permalink
Transmitting binary switches
Browse files Browse the repository at this point in the history
  • Loading branch information
technocake committed Mar 25, 2013
1 parent a618095 commit 5caede8
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 80 deletions.
2 changes: 2 additions & 0 deletions pizzadaemon/pizzadaemon.py
Expand Up @@ -96,6 +96,8 @@ def list_com_ports():


statusmap = [ re.split("\s", string.rstrip(u)) for u in urllib2.urlopen(URL).readlines() ] statusmap = [ re.split("\s", string.rstrip(u)) for u in urllib2.urlopen(URL).readlines() ]


print ("Pushing state")
print (statusmap)
api.pushState(statusmap) api.pushState(statusmap)


api.clean_the_mess_up_after_you() api.clean_the_mess_up_after_you()
66 changes: 56 additions & 10 deletions pizzadaemon/pnmp.py
Expand Up @@ -7,6 +7,7 @@
import serial import serial
from serial.tools import list_ports from serial.tools import list_ports
import time import time
import math






Expand All @@ -22,17 +23,21 @@
class api(): class api():
""" The Pizza-Netmap Network Monitoring Protocol api """ """ The Pizza-Netmap Network Monitoring Protocol api """


PUSHSTATE = 0xD PUSHSTATE = [0xF0, 0xF1] #1 bit signaling command and not data (first)

WRITE_LED = [0xF2]
def __init__(self, COM_PORT, BAUD_RATE=9600): def __init__(self, COM_PORT, BAUD_RATE=9600):




try: try:
# Opening a serial connection to the arduino :). # Opening a serial connection to the arduino :).
self.arduino = serial.Serial(COM_PORT, BAUD_RATE, timeout=0 ) self.arduino = serial.Serial(COM_PORT, BAUD_RATE, bytesize=8, timeout=2 )


print ("Done sleeping") # The arduino will now reset. Lets wait for it to
self.arduino.write("hello\n") # finish its boot sequence :).
print ("Pizza in owen...")
time.sleep(3)
print ("The Pizza is alive!")
#self.arduino.write("hello\n")
self.arduino.flush() self.arduino.flush()
#waiting for the reset of the arduino to finish. #waiting for the reset of the arduino to finish.


Expand Down Expand Up @@ -118,16 +123,57 @@ def pushState(self, statemap):
0 on success -1 on error. 0 on success -1 on error.
""" """
bits = '' bits = ''
b = 0




# Start pushstate
self.arduino.write(chr( self.PUSHSTATE [0] ) )

#import pdb; pdb.set_trace()

# Sending 7bits at a time.
chunks = int( len(statemap) / 7 ) + (1 if len(statemap)%7 > 0 else 0);
c = chunks
data = '0'
bits_sent = 0


for switch in statemap: for switch in statemap:
bits += '0' if switch[1] == "off" else '1' # Translating on/off to 1/0
bit = '0' if switch[1].lower() == "off" else '1'
# Adding the bit to the chunk to be sent
data += bit
b+=1

# Checks if we have a chunk to send
if (b==7):
self.arduino.write( chr(int(data, 2)))
print ( "Data to arduino: %s" % data )
b=0
data = '0'
c -=1
bits_sent += 7


#last chunk:
if (bits_sent < len(statemap)):
bits_sent += len(data) - 1
pad = '0' * ( 8 - ( len(statemap)-bits_sent) )
self.arduino.write(
chr( int( pad + data, 2 ) )
)
print ( "Data to arduino: %s" % data )


binarydata = int(bits[:8],2)
print "Data to arduino: %s" % bin(binarydata) print ("Sent %d chunks and %d bits" % (chunks, bits_sent))


#self.arduino.write(chr(self.PUSHSTATE))
# End of transmit
self.arduino.write(chr( self.PUSHSTATE [1] ) )


self.arduino.write( chr(binarydata) )





def getState(self): def getState(self):
Expand Down
Binary file modified pizzadaemon/pnmp.pyc
Binary file not shown.
127 changes: 57 additions & 70 deletions pizzanetmap_firmware/pizzanetmap_firmware.ino
Expand Up @@ -58,9 +58,6 @@ int TX[number_of_cores] = {TX1_Pin, TX2_Pin};
int active_cores, bits_shifted, tot_nodes, nodes = 0; int active_cores, bits_shifted, tot_nodes, nodes = 0;
char cb; //buffer char cb; //buffer


String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete







Expand All @@ -82,8 +79,7 @@ void setup(){
//Reset //Reset
pinMode(MR_Pin, OUTPUT); pinMode(MR_Pin, OUTPUT);


// reserve 200 bytes for the inputString:
inputString.reserve(200);


// For the API to work :). // For the API to work :).
Serial.begin(9600); Serial.begin(9600);
Expand Down Expand Up @@ -120,25 +116,14 @@ void setup(){
registers = (boolean *) malloc( tot_nodes * sizeof(boolean) ); registers = (boolean *) malloc( tot_nodes * sizeof(boolean) );
if (registers != NULL) { if (registers != NULL) {
// Setting default value on all leds. // Setting default value on all leds.
memset( registers, LOW, sizeof(boolean) * tot_nodes ); memset( registers, 1, sizeof(boolean) * tot_nodes );
} }



for (int i = 0; i < 8; ++i)

{
/* //flipping bits.
* Debug registers[i] = ! registers[i];
*/ }
Serial.print(core_stat[0].n_distros);
Serial.print(active_cores);
Serial.print("\nTotalt:");
Serial.print(tot_nodes);
Serial.print(" random-pick led: ");
Serial.print(registers[5]);

registers[7] = 1;
registers[3] = 1;
registers[15] = 1;



writeRegisters(); writeRegisters();
} }
Expand Down Expand Up @@ -205,69 +190,71 @@ void writeRegisters() {


} }


Serial.print("ninja: ");
Serial.print(ninja);
Serial.print("\n");
Serial.print(core_stat[0].n_pins);
Serial.print("\n");


} }








char buffer[50]; char buffer[50];
void writeRegisters(char * data, int len) {
boolean ninja[sizeof(char)];

#define PUSHSTATE 0xF0
for (int i=0; i<len && i < tot_nodes; i++) { #define ENDPUSHSTATE 0xF1
ninja = (boolean*) data; #define WRITE_LED 0xF2
for (int k=0; k<sizeof(boolean); k++ ) {
registers[i++] = ninja[k]; boolean TRANSMIT = false;
}
int bits;
unsigned int rb = 0; // Received bits

void loop(){
int cmd;
int bytes;
char led, * ledp;
int led_int;
int c;


if (Serial.available() > 0) {
cmd = Serial.read();

if (!TRANSMIT && cmd == PUSHSTATE)
{
TRANSMIT = true;
rb = 0;
c=0;
}
else if (TRANSMIT && cmd == ENDPUSHSTATE)
{
TRANSMIT = false;
writeRegisters();



}
// if (data[i] == (char) '1') { else if (TRANSMIT) // Receiving chunk after chunk.
// registers[i] = HIGH; {
// } bits = cmd;
// else
// registers[i] = LOW; for (int i = 0; i < 7; ++i)
{
if ( rb < core_stat[c].n_pins ) // Filling up one core at a time.
registers[rb++] = (bits & (0x01 << i ) != 0);
else if (c < number_of_cores)
{
c++;
rb = 0;
}

}
} }
}




void loop(){
delay(100);
// print the string when a newline arrives:
if (stringComplete) {


// clear the string:

writeRegisters(inputString.toCharArray(), inputstring.length());
inputString = "";
stringComplete = false;
} }




} }


#define WRITE_LED 13
#define PUSHSTATE 13


void serialEvent() {
int cmd, bits = 0;
while ( Serial.available() ) {
cmd = Serial.parseInt();

if (cmd == PUSHSTATE) {

while (Serial.available() ) {
inputString += (char)Serial.read();
if( inputString.length() >= tot_nodes ) {
stringComplete = true;
break;
}
}
}
}
}

0 comments on commit 5caede8

Please sign in to comment.