Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

[size=16][highlight][color=#ff0000][b]This forum is now [u]READ-ONLY[/u]! Please visit [url]http://forum.wiring.co/[/url] for the new forum.[/b][/color][/highlight][/size]

Wiring ForumProgramming Questions & HelpSyntax › bit shift

Page Index Toggle Pages: 1
bit shift (Read 2770 times)
03/03/07 at 06:40:31

stephanschulz   Offline
YaBB Newbies
Berlin, Halifax, Montreal

Posts: 14
*
 
I though it would be good to share this example on how to bit shift.

I have 12 leds connected to my wiring i/o board and control each through a key on my keyboard through the processing application.

I am storing the information in two bytes using the first bit as identifier.

Here is the code for the wiring board and the code for processing.

please note that i added some strange FTP stuff to work around a lock file RXTX problem. (see more about that here: http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1136158702/10#10)


-----------------------------------------------wiring---------------------------
-------------
byte val;

int leds [] = {
  0,1,2,3,4,5,6,7,8,9,10,11};

void setup()
{
Serial.begin(9600);       // start serial communication at 9600bps

  for(int i = 0; i < 12 ; i++){
    pinMode(leds[i], OUTPUT);
  }

  pinMode(48, OUTPUT);
  digitalWrite(48, HIGH);
}

void loop() {
  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }
 

  if( ((val >> 0) & 1) == 0){ // if first bit of byte starts with 0
    for(int i=1; i<=7;i++){ // go through bit 1 - 7 of byte_A (see processing part)
     int temp = ((val >> i) & 1); // look at bit i
     digitalWrite(leds[i-1], temp); //set led to 0 or 1
    }
  }
 
if( ((val >> 0) & 1) == 1){ //  if first bit of byte starts with 1
    for(int i=1; i<=5;i++){ // go through bit 1 - 5 of byte_B
     int temp = ((val >> i) & 1);
     digitalWrite(leds[i+6], temp);
    }
  }

  delay(10);                    // wait 10ms for next reading
}

-------------------------------------------------- processing---------------------------------
//lots of info about bit shift at http://www.arduino.cc/playground/Code/BitMath#common

byte byte_A = 0; //-128;
byte byte_B = 0; //-128;

char key_list[] = {
  '1','2','3','4','5','6','7','8','9','0','-','='};

PFont font; //make sure you have some font in data folder
import processing.serial.*;

Serial port;

//// doing this because I have strange RXTX lock file problem
//// this deletes all lock files out of var/lock
////  the lock files look like this LK.002.009.012
//// otherwise i won't be able to connect wiring to i/o board
FTPClient ftp;
String[] files;
/////


void setup()
{
  size(300, 300);
  frameRate(30);

  EmptyFolder(); /// deletes lock files

  ellipseMode(CENTER);
  font = loadFont("CourierNew36.vlw");

  println(Serial.list());
  port = new Serial(this, Serial.list()[2], 9600);

}

void mousePressed() {
  EmptyFolder(); // end sketch by clicking on screen and delete lock files
  exit();
}


void draw() {
  background(222);
  fill(44);
  textFont(font, 12);


  byte_A &= ~(1 << 0);  // set first bit of first byte to 0
  byte_B |= (1 << 0);  // set first bit of second byte to 1

  //(xx >> n) & 1  reads  nth bit

  if(keyPressed) { // change bites
 
    for(int i=1; i<=7;i++){ // go through 1-7th bit of first byte
     if(key == key_list[i-1]) {
       byte_A ^= (1 << i);       // toggles nth bit of x.  all other bits left alone.
     }
    }
    for(int i=8; i<=12;i++){ // go through 1-5th bit of second byte
     if(key == key_list[i-1]) {
       byte_B ^= (1 << i-7);       // toggles nth bit of x.  all other bits left alone.
     }
    }

  }

  // x &= ~(1 << n);      // forces nth bit of x to be 0.  all other bits left alone.
  // x |= (1 << n);       // forces nth bit of x to be 1.  all other bits left alone.

  text(binary(byte_A), 150, 90);
  text(binary(byte_B), 150, 100);
  text(byte_A, 150, 110);
  text(byte_B, 150, 120);

  port.write(byte_A);   
delay(10); 
  port.write(byte_B);
delay(10);
}

/////////////////////////////////////********************????????????????
//more info at http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1131152633
//make sure your system prefs are set to ftp access under sharing

void EmptyFolder() {

  try
  {
    // set up a new ftp client
    ftp = new FTPClient();
    ftp.setRemoteHost("pieria.local"); // ie. ftp.site.com

    // set up listener
    FTPMessageCollector listener = new FTPMessageCollector();
    ftp.setMessageListener(listener);

    // connect to the ftp client
    println ("Connecting");
    ftp.connect();

    // login to the ftp client
    println ("Logging in");
    //ftp.login("user name", "password");
    ftp.login("pieria", "w4ss3rf4ll");

    // set up in passive mode
    //println ("Setting up passive, ASCII transfers");
    ftp.setConnectMode(FTPConnectMode.PASV);

    // set up for ASCII transfers
    ftp.setType(FTPTransferType.ASCII);


    // get directory and print it to console
    //println ("Directory listing after move");
    files = ftp.dir("/var/lock/", true);
    for (int i = 1; i < files.length; i++)
    {
     //println (files[i]);
     int t1 = files[i].indexOf("LK");
     if(t1 != -1){
       int t2 = files[i].length();
       //println(t1 + " " + t2);
       String ss = files[i].substring(t1, t2);
       println(ss);
       ftp.delete("/var/lock/"+ss);
     }
    }

    // Shut down client
    //println ("Quitting client");
    ftp.quit();

    println ("files out of var/lock/ are delete");

  }
  catch (Exception e)
  {

    //Print out the type of error
    println("Error "+e);

  }
}

good luck.
stephan schulz.
www.maybevideodoes.de
 
IP Logged
 
Page Index Toggle Pages: 1