Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote this code for the Raspberry Pi board, any constructive copmments or suggestion

What the code does is wait for serial input then plays an arrow with a sound and message

 #include <termios.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/signal.h>
 #include <sys/types.h>
 #include <signal.h>
 #include <bits/siginfo.h>

 #include <iostream>
 #include <cv.h>
 #include <highgui.h>
 #include <stdlib.h>

 #include <queue>       


 #define BAUDRATE B9600
 #define MODEMDEVICE "/dev/ttyUSB0"
 #define _POSIX_SOURCE 1 /* POSIX compliant source */
 #define FALSE 0
 #define TRUE 1

 volatile int STOP=FALSE; 

 void signal_handler_IO (int status);   /* definition of signal handler */
 int wait_flag=TRUE;                    /* TRUE while no signal received */

 int fd,c, res;
 struct termios oldtio,newtio;
 struct sigaction saio;           /* definition of signal action */
 char buf[255];

 using namespace cv;
 using namespace std;

#define w 400 //width
#define l 400 //length
#define mp 3 //multiplier - size of arrow
#define ts 4 //time scale - time to show arrow

//message
 void Arrow( Mat img, int i );
 int CreateMessage( String tln, int i ); // (Teller number, Arrow 1 left 2   right 3 down) 

//images       
 int showImages(int a);
 void loadImages(); 
 int nImages = 8;
 IplImage *images[0];

typedef queue<char*> CHARQUEUE;

//initilaize serial port        
 void iSerial(){
 fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
 if (fd <0) {perror(MODEMDEVICE); exit(-1); }

 saio.sa_handler = signal_handler_IO;
 saio.sa_flags=0;
 saio.sa_restorer = NULL;
 sigaction(SIGIO,&saio,NULL);

 fcntl(fd, F_SETOWN, getpid());
 fcntl(fd, F_SETFL, FASYNC);

 tcgetattr(fd,&oldtio); /* save current port settings */
 newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
 newtio.c_iflag = IGNPAR | ICRNL;
 newtio.c_oflag = 0;
 newtio.c_lflag = ICANON;
 newtio.c_cc[VMIN]=1;
 newtio.c_cc[VTIME]=0;
 tcflush(fd, TCIFLUSH);
 tcsetattr(fd,TCSANOW,&newtio);

 tcsetattr(fd,TCSANOW,&oldtio);
   } 

int main()
      {
   CHARQUEUE q;
   loadImages();  
   iSerial();
   while(1){
      for (int i = 0; i < nImages; ++i){ 
             showImages(i);

      /* after receiving SIGIO, wait_flag = FALSE, input is available
         and can be read */
      if (wait_flag==FALSE) { 
        res = read(fd,buf,255);
        buf[res]=0;
        q.push(buf);
        printf(":%s:%d\n", buf, res);
        if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
        wait_flag = TRUE;      /* wait for new input */
     }
        while (!q.empty())
        {
         CreateMessage( q.front(), 2 );
         q.pop();
        }
    }
   }
}


  void signal_handler_IO (int status)
  {
    printf("received SIGIO signal.\n");
    wait_flag = FALSE;
  }

void loadImages() {

images[0] = cvLoadImage("images/001.jpg");
images[1] = cvLoadImage("images/002.jpg");
images[2] = cvLoadImage("images/003.jpg");
images[3] = cvLoadImage("images/004.jpg");
images[4] = cvLoadImage("images/005.jpg");
images[5] = cvLoadImage("images/006.jpg");
images[6] = cvLoadImage("images/007.jpg");
images[7] = cvLoadImage("images/008.jpg");
images[8] = cvLoadImage("images/009.jpg");

}

int showImages(int a){
    cvNamedWindow("pic");
    cvShowImage("pic",images[a]);
    cvMoveWindow("pic", 0, 0);
    cvWaitKey(2000);
}



int CreateMessage( String tln, int i )
 {
 Mat arrow_image = Mat::zeros( w, l, CV_8UC3 );
 putText(arrow_image, "Teller "+tln, cvPoint(100,30), FONT_HERSHEY_COMPLEX_SMALL, 1.8, cvScalar(200,200,250), 1, CV_AA);
Arrow( arrow_image, i );
namedWindow("Drawing arrow");
system("aplay a_12.wav&"); /*play sound*/
imshow("Drawing arrow", arrow_image);
cvWaitKey(1000*ts); /*this is the delay */
destroyWindow("Drawing arrow");
 }

void Arrow( Mat img, int i ) 
{  
 int lineType = 8;
 Point arrow_points[1][7];
if (i == 1) { //left  
  arrow_points[0][0] = Point( 90*mp, 60*mp );
  arrow_points[0][1] = Point( 90*mp, 40*mp );
  arrow_points[0][2] = Point( 50*mp, 40*mp );
  arrow_points[0][3] = Point( 50*mp, 30*mp );
  arrow_points[0][4] = Point( 10*mp, 50*mp );
  arrow_points[0][5] = Point( 50*mp, 70*mp );
  arrow_points[0][6] = Point( 50*mp, 60*mp );
};
if (i == 2) { //right 
  arrow_points[0][0] = Point( 20*mp, 60*mp );
  arrow_points[0][1] = Point( 20*mp, 40*mp );
  arrow_points[0][2] = Point( 60*mp, 40*mp );
  arrow_points[0][3] = Point( 60*mp, 30*mp );
  arrow_points[0][4] = Point( 100*mp, 50*mp );
  arrow_points[0][5] = Point( 60*mp, 70*mp );
  arrow_points[0][6] = Point( 60*mp, 60*mp );
};
if (i == 3) { //down
 arrow_points[0][0] = Point( 60*mp, 20*mp );
 arrow_points[0][1] = Point( 40*mp, 20*mp );
 arrow_points[0][2] = Point( 40*mp, 60*mp );
 arrow_points[0][3] = Point( 30*mp, 60*mp );
 arrow_points[0][4] = Point( 50*mp, 100*mp );
 arrow_points[0][5] = Point( 70*mp, 60*mp );
 arrow_points[0][6] = Point( 60*mp, 60*mp );
};
const Point* ppt[1] = { arrow_points[0] };
 int npt[] = { 7 };
 fillPoly( img,
    ppt,
    npt,
        1,
    Scalar( 250, 0, 0 ),
    lineType );
}
share|improve this question
    
You may get better trackaction if you add C as a language. Though you use one C++ class the code for review is basically C no real C++ features used so you may as well ask for comments from C coders. –  Loki Astari Jan 31 '13 at 17:43

1 Answer 1

up vote 1 down vote accepted

Nice. I am working on a similar project. In order to make your code more modular and object oriented, you can write a Message class and a SerialPort class. It makes it much easier to restructure your code when you want to change the behavior. e.g.

SerialPort sp("COM8"); // or "/dev/ttyUSB0" for linux
Message msg("alskadflaskjfd");
Message rx;

sp.write(msg);
sp.read(rx);

You might as well since your using some c++, modularity is always a good thing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.