Permalink
Browse files

Transmitting binary switches

  • Loading branch information...
1 parent a618095 commit 5caede8a68110d2dc65f0d611cfe97c4f779714f @technocake technocake committed Mar 25, 2013
@@ -96,6 +96,8 @@ def list_com_ports():
statusmap = [ re.split("\s", string.rstrip(u)) for u in urllib2.urlopen(URL).readlines() ]
+ print ("Pushing state")
+ print (statusmap)
api.pushState(statusmap)
api.clean_the_mess_up_after_you()
View
@@ -7,6 +7,7 @@
import serial
from serial.tools import list_ports
import time
+import math
@@ -22,17 +23,21 @@
class 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):
try:
# 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")
- self.arduino.write("hello\n")
+ # The arduino will now reset. Lets wait for it to
+ # finish its boot sequence :).
+ print ("Pizza in owen...")
+ time.sleep(3)
+ print ("The Pizza is alive!")
+ #self.arduino.write("hello\n")
self.arduino.flush()
#waiting for the reset of the arduino to finish.
@@ -118,16 +123,57 @@ def pushState(self, statemap):
0 on success -1 on error.
"""
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:
- 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):
View
Binary file not shown.
@@ -58,9 +58,6 @@ int TX[number_of_cores] = {TX1_Pin, TX2_Pin};
int active_cores, bits_shifted, tot_nodes, nodes = 0;
char cb; //buffer
-String inputString = ""; // a string to hold incoming data
-boolean stringComplete = false; // whether the string is complete
-
@@ -82,8 +79,7 @@ void setup(){
//Reset
pinMode(MR_Pin, OUTPUT);
- // reserve 200 bytes for the inputString:
- inputString.reserve(200);
+
// For the API to work :).
Serial.begin(9600);
@@ -120,25 +116,14 @@ void setup(){
registers = (boolean *) malloc( tot_nodes * sizeof(boolean) );
if (registers != NULL) {
// Setting default value on all leds.
- memset( registers, LOW, sizeof(boolean) * tot_nodes );
+ memset( registers, 1, sizeof(boolean) * tot_nodes );
}
-
-
- /*
- * Debug
- */
- 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;
-
+for (int i = 0; i < 8; ++i)
+ {
+ //flipping bits.
+ registers[i] = ! registers[i];
+ }
writeRegisters();
}
@@ -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];
-void writeRegisters(char * data, int len) {
- boolean ninja[sizeof(char)];
-
- for (int i=0; i<len && i < tot_nodes; i++) {
- ninja = (boolean*) data;
- for (int k=0; k<sizeof(boolean); k++ ) {
- registers[i++] = ninja[k];
- }
+
+
+#define PUSHSTATE 0xF0
+#define ENDPUSHSTATE 0xF1
+#define WRITE_LED 0xF2
+
+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') {
-// registers[i] = HIGH;
-// }
-// else
-// registers[i] = LOW;
+ }
+ else if (TRANSMIT) // Receiving chunk after chunk.
+ {
+ bits = cmd;
+
+ 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.