Tuesday, January 6, 2015

Arduino Interfaced with 8Segment Display


Got a 8segment display ordered from ebay
"MAX7219 EWG 8-Digit Digital Tube Display Control Module Red for arduino"

Now connected it to arduino over SPI interface. The connectivity is as follows.


Arduino                            Display
Vcc(5v)          ----->        VCC
GND              ------->      GND
Pin 10             ------>        CS(chip Select)
Pin 11             ------>        DIN(data in)
Pin 13               ------>       CLK


The Arduino Code is as follows. The code prints numbers from 0 to 100 on the display in a loop.




#include
const int slaveSelectPin = 10;
int num = 0;

void setup() {

  Serial.begin(57600); //
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  //Serial.println("Goodnight moon!");
  
  // set the slaveSelectPin as an output:
  //pinMode (slaveSelectPin, OUTPUT);
  // initialize SPI:
  pinMode (slaveSelectPin, OUTPUT);

  SPI.begin(); 
  initMax();
}

void pulseCS(void)   {
  digitalWrite(slaveSelectPin, 1);
  delay(1);
  digitalWrite(slaveSelectPin, 0);
}

void initMax()
{
  digitalWrite(slaveSelectPin, 0);
  
  
  SPI.transfer(0x09);//Decode Mode
//  SPI.transfer(0xFF);//BCD decode for all digits
  SPI.transfer(0x00); // No decode
  pulseCS();
  
  
  SPI.transfer(0x0B);//Scan Limit
  SPI.transfer(0x07);
  pulseCS();
  
  SPI.transfer(0x0A);//Intensity
  SPI.transfer(0x01);
  pulseCS();
  
  for(int i=1; i<=8; i++)   {
    SPI.transfer(i);
    SPI.transfer(0x00);
    pulseCS();  
  }
  
  SPI.transfer(0x0C);
  SPI.transfer(0x01);
  pulseCS();


 //Serial.println("Display Init"); 
 //numDigits(7);
}

int numDigits(int i)
{
  int count = 1;
  while(1)
  {
     i = i /10;
     if (i != 0)
       count ++;
     else
        break;
  }
  
  Serial.print("Num Digits :"); 
  Serial.println(count);
  return count;
  
}
void displayNum(int i)
{
  
  for(int i=1; i<=8; i++)   {
    SPI.transfer(i);
    SPI.transfer(0x00);
    pulseCS();  
  }
  
  //Find how many digits
  int numDig = numDigits(i);
  int pos = 1;
  while (1)
  {
    int k = i % 10;
    SPI.transfer(pos); 
    switch(k)
    {
      case 0:
      SPI.transfer(B01111110);
      pulseCS();
      break;
      case 1:
      SPI.transfer(B00110000);
      pulseCS();
      break;
      case 2:
      SPI.transfer(B01101101);
      pulseCS();
      break;
      case 3:
      SPI.transfer(B01111001);
      pulseCS();
      break;
      case 4:
      SPI.transfer(B00110011);
      pulseCS();
      break;
      case 5:
      SPI.transfer(B01011011);
      pulseCS();
      break;
      case 6:
      SPI.transfer(B01011111);
      pulseCS();
      break;
      case 7:
      SPI.transfer(B01110000);
      pulseCS();
      break;
      case 8:
      SPI.transfer(B01111111);
      pulseCS();
      break;
      case 9:
      SPI.transfer(B01111011);
      pulseCS();
      break;     
    }
    if (pos == numDig)
      break;
    pos ++;
    i = i/10;
  }
}

void loop() {  
  
  if (num <= 100)
  {
    displayNum(num);
    delay(1000);
    num++;
  }
  else
  {
    num = 0;
  }
   //SPI.transfer(1);  
   //SPI.transfer(0x09);
   //pulseCS();
   
   //delay(1000);
   
   
   //SPI.transfer(2);  
   //SPI.transfer(0x08);
   //pulseCS();

}

Thursday, January 30, 2014

Smart Car Project

smart Car 

Connection diagram











Arduino code


#define led 13

//for Fwd and Backward
#define pin1 2
#define pin2 4

//for Right and left
#define LR1  7
#define LR2  8

int inByte = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Hello World");

  pinMode(led, OUTPUT);

  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);

  pinMode(LR1,  OUTPUT);
  pinMode(LR2, OUTPUT);

  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
  digitalWrite(LR1, LOW);
  digitalWrite(LR2, LOW);
}

void loop()
{
  if (Serial.available() > 0)
  {
    inByte = Serial.read();
    int stp = 0; //Stop wheels
    int fwd = 1; //run in fwd direction
    int bw = 2;  //backward dir
    int right = 3; //right
    int left = 4;  //left
 
    Serial.println(inByte, DEC);
    if (inByte == stp)
    {
       digitalWrite(pin1, LOW);
       digitalWrite(pin2, LOW);
       Serial.println(stp, DEC);
    }
    if (inByte == fwd)
    {
       digitalWrite(pin1, HIGH);
       digitalWrite(pin2, LOW);
       Serial.println(fwd ,DEC);
    }
    if (inByte == bw)
    {
      digitalWrite(pin1, LOW);
      digitalWrite(pin2, HIGH);
      Serial.println(bw, DEC);
    }
    if (inByte == right)
    {
      digitalWrite(LR1, LOW);
      digitalWrite(LR2, HIGH);
      Serial.println(right, DEC);
    }
    if (inByte == left)
    {
      digitalWrite(LR1, HIGH);
      digitalWrite(LR2, LOW);
      Serial.println(left, DEC);
    }
 
    Serial.println("done");
  }
}


udp_server.c

Thursday, May 24, 2012

My experiments with tic tac toe


Was writing a tic tac toe app for android recently as was familiar with the rules of the game :-).
The last time I wrote a tic tac toe game was in college time, and then most focus was on the gui design. The lousiest algorithm in the background.

One of the algorithm was to check the state of the game i.e win or draw etc. Gave it a thought this time and  made some optimization in the row, col, diagonal checks. Its mainly decided in the first nested loop if we need to check a particular column or diagonal. So, we avoid checking of columns or diagonals saving time. This makes big impact when the board size is more and a significant number of the cells are not filled.


Next time I will post the nextMove implementation, for the computer to decide its next move.

Here is the java code for the gamestate algorithm.

  int gameState(int values[][], int boardSz) {


boolean colCheckNotRequired[] = new boolean[ boardSz ];
boolean diag1CheckNotRequired = false;
boolean diag2CheckNotRequired = false;
boolean allFilled = true;


int x_count = 0;
int o_count = 0;
/* Check rows */
for (int i = 0; i < boardSz; i++) {
x_count = o_count = 0;
for (int j = 0; j < boardSz; j++) {
if(values[i][j] == x_val)x_count++;
if(values[i][j] == o_val)o_count++;
if(values[i][j] == 0)
{
colCheckNotRequired[j] = true;
if(i==j)diag1CheckNotRequired = true;
if(i + j == boardSz - 1)diag2CheckNotRequired = true;
allFilled = false;
//No need check further
break;
}
}
if(x_count == boardSz)return X_WIN;
if(o_count == boardSz)return O_WIN;
}


/* check cols */
for (int i = 0; i < boardSz; i++) {
x_count = o_count = 0;
if(colCheckNotRequired[i] == false)
{
for (int j = 0; j < boardSz; j++) {
if(values[j][i] == x_val)x_count++;
if(values[j][i] == o_val)o_count++;
//No need check further
if(values[i][j] == 0)break;
}
if(x_count == boardSz)return X_WIN;
if(o_count == boardSz)return O_WIN;
}
}

x_count = o_count = 0;
/* check diagonal 1 */
if(diag1CheckNotRequired == false)
{
for (int i = 0; i < boardSz; i++) {
if(values[i][i] == x_val)x_count++;
if(values[i][i] == o_val)o_count++;
if(values[i][i] == 0)break;
}
if(x_count == boardSz)return X_WIN;
if(o_count == boardSz)return O_WIN;
}

x_count = o_count = 0;
/* check diagonal 2 */
if( diag2CheckNotRequired == false)
{
for (int i = boardSz - 1,j = 0; i >= 0 && j < boardSz; i--,j++) {
if(values[j][i] == x_val)x_count++;
if(values[j][i] == o_val)o_count++;
if(values[j][i] == 0)break;
}
if(x_count == boardSz)return X_WIN;
if(o_count == boardSz)return O_WIN;
x_count = o_count = 0;
}


if( allFilled == true)
{
for (int i = 0; i < boardSz; i++) {
for (int j = 0; j < boardSz; j++) {
if (values[i][j] == 0) {
allFilled = false;
break;
}
}
if (allFilled == false) {
break;
}
}
}


if (allFilled)
return DRAW;


return INPROGRESS;
}


Thursday, May 17, 2012

Yet Another HashMap ...

There are several implementations of hashmaps available, this is just another implementation that I did when I  was too lazy to google for existing implementations.

This implementation is having 2 basic traits:
1. A strong hash function.
2. An efficient tree

To implement this I have used the hash implementation provided by Bob Jenkins http://burtleburtle.net/bob/hash/doobs.html

And the tree chosen is a basic red black tree implementation. 

A very simple API which enables insertion/deletion of the entries.

To insert entries:
hash_insert(void* key, uint32 key_len, void* data, uint32 data_len);

To Check if the entry is present in the table
hash_find(void* key, uint32 key_len);

To retrieve the data for a key
hash_getData(void* key, uint32 key_len, void* data, uint16* data_len)

To delete an entry
hash_deleteEntry(void* key, uint32 key_len);

Here is a sample code, to show how to use it.

#include "hashmap.h"#include
#include
#include
#define MAX_KEY_LEN 128
int main(){
      char key[MAX_KEY_LEN];
      uint16_t data;
      uint16_t dataLen;
      int i = 10000000;
      // First insert
      strcpy(key, "hello");
      data = 123;
      hash_insert((void*)key, strlen(key), &data, sizeof(data));
   
      //Lets retrieve it
      strcpy(key, "hello");
      uint16_t temp;
      hash_getData((void*)key, strlen(key), &temp, &dataLen);
      //Remove the entries
      strcpy(key, "hello");
      hash_deleteEntry((void*)key, strlen(key));
}

The default number of entries in the hashmap is defined in hashmap.h as follows

#define HASH_MAX_CAPACITY 10000000

This can be modified as per requirement.

How to Use

Just hit make in the extracted package. This generates a  library libQuickHash.so and libQuickHash.a.  There is a sample program testHash.c which can be of help. 

The compressed package is here

enjoy ...

Wednesday, November 9, 2011

How smart are the smart pointers ?

Untitled document

smart_pointer is a blessing for a carefree programmer who wants to allocate memory in heap and forget about deleting it. Sounds great. Its really tough to track a pointer and carefully release the heap memory captured.

Also it relies on the fact that its easy to clear stack than heap. So, lets see how it works.

First lets see, how can we use a smart pointer class in our program and then we will go ahead to implement it. Please follow the comments for understanding.

We take an example class

class A

{

        public:

        //This variable is meant to track the

        // number of instances of the class that are existing.

        static int count;

        //Constructor

        A()

        {

                //New instance is created so increment.

                count++;

        }

        //Copy contstructor

        A(A& obj)

        {

                //New instance is created so increment

                count ++;

        }

        //Assignment operator

        A& operator =(A& obj)

        {

                //New instance is created so increment

                count ++;

        }

        ~A()

        {

                //Instance removed, so decrement the value.

                count --;

        }

        void display()

        {

                cout << "Its me \n";

        }

};

Now we look at the main function where we will be using the smart_ptr class.

int main()

{

        //This is how we create a smart_ptr object

        // see the pointer of type A is stored inside the

        // smart_ptr object

        smart_ptr<A> ptr(new A());

        smart_ptr<A> ptr1;

        //Testing assignment operator

        ptr1 = ptr;

        //Testing self assignment

        ptr = ptr;

        // Verifying how many instances of A are there.

        cout << "A::count = " << A::count << endl;

        //Using overloaded -> operator to call

        // member functions of class A

        ptr->display();

}

Now we implement the smart_ptr class, as per the usage. So, here it is.

template <class T>

class smart_ptr

{

        private:

               // The pointer which is managed

                // by the smart_ptr class

                T* m_ptr;

                //Keeps track of the number of references

                //the allocated memmory.

int ref_count;

        public:

                smart_ptr()

                {

                        m_ptr = 0;

                        ref_count = 0;

                }

                smart_ptr(T* ptr)

                {

                        m_ptr = ptr;

                        //increment the ref count

                        //as this is the first reference

                        ref_count = 1;                

                }

                smart_ptr(smart_ptr& obj)

                {

                        cout <<  "Copy Contstructor\n";

                        ref_count++;

                }

                //Assignment  operator overload

                smart_ptr* operator =(smart_ptr& obj)

                {

                        if(&obj != this)

                        {

                                ref_count++;

                        }

                        else

                        {

                                cout << "Self assignment\n";

                        }

                }

                // Destructor

                ~smart_ptr()

                {

                        cout << "Deleteing the smart_ptr instance\n";

                        // Decrement the reference count as

                        // the instance is released

                        ref_count --;

                        if(ref_count == 0)

                        {

                                // Delete the pointer

                                // as there are no references to it.

                                delete m_ptr;

                        }

                }

                T* operator ->()

                {

                        return m_ptr;

                }

};

Tuesday, October 11, 2011

A Gram of Anagram


Last week attended an interview, and this was the question the interviewer came up with to test my problem solving.

" Give a logic to generate valid anagrams of a word, provided you have a dictionary to confirm the validity"

After a while of scribbling on the blank paper I came up with the following answer.

If I take a dictionary as a Hash Map as every word is unique and the Key is a binary(or Hex) representation of the word. Then if I have a word I can easily find the meaning of it with O(1) complexity.

Now, if we have to generate all the valid anagrams, we need to verify if the generated anagram is in the dictionary, if it is present in dictionary, its a valid one else we need to ignore that.

I will assume that there can be a word of max 100 characters(or more but there is a limit).

So any word we take it as a sequence of indexes like a word "hello" can be represented like "1234". Now the anagrams of "1234" are "1243", "1242" ..etc

The only thing we need to do is to store all such combinations of indexes for a particular number of characters. This is an one time task. And then words can be generated from the combinations by picking the characters from the index.Hence we get the anagrams.

To verify if the anagrams are valid or not, just index into the dictionary and validate.

The only thing need to be handled is the duplicates.That can be done easily. As an when we need to compare with the previous ones that has been searched in dictionary.

And I said the solution emphasizes on performance.

I don't know if this is the answer the interviewer was expecting or something else but I can guess it now as I did not get any response for further rounds.

Really need to work on my problem solving skills.

Tuesday, December 14, 2010

Multi Key Maps.. myth or reality


What the heck ? Never heard of these, is it possible to access the same entry with multiple keys? Just somedays back thought of this, and said y not .

Our good old friend Map allows us to have an unique key which is mapped to a value. To access a value we must have an index which is unique. But what if we want to access the same entry but using multiple indices we need a slightly different mechanism.

Ok , so here I would discuss a way to achieve it. And we will put into use something we learned in school and that is Prime Number. These are basically the numbers that are divisible by themselves except 1.

Euclid’s Fundamental Theorem of Arithmetic states that Every integer can be written as a product of primes in an essentially unique way. And Euclid's second theorem states that the number of prime numbers is infinite.

Now, as per the first rule we conclude that prime numbers are the ones upon multiplication of which we can generate any number. So, can we say they are the basic components of any number.

Based on this we try to create a key of a map. Lets say we want to create a map in which a value can be accessed using 2 keys.
Here are the various keys that we are going to use and the index values they generate.
2*3 = 6
5*7 = 35
11*13 = 143 .. and so on. Note that each of the index has only 2 factors except 1.

Now we create a map from integer to a string with the above indices
6 --> “Hello”
35 --> “ World”
143 ---> “Sanjiv”

Say in c++, the search code will look something like this.

map my_map.begin ;
string get(int key)
{
for(map::iterator iter = my_map.begin(); iter != my_map.end(); iter++)

{

if(iter->first % key == 0) //Key matches with the index

return iter->second;

}

}

It looks so dumb that we are have to traverse the whole map to find the match. This completely denies the whole purpose of an associated storage.

I hope I will be able to come up with a better way to do the same.