OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECT6_CMDLINE/] [util.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sumanta.ch
/*  util.cpp
2
    June 9, 2011
3
    Some helper functions.
4
 
5
    Much of the code below is borrowed from Alastair Smith's program
6
    from the 2010 FPT Othello competition
7
 
8
    By Kevin Nam
9
*/
10
 
11
 
12
#include <string.h>
13
#include <assert.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <iostream>
17
#include <sys/time.h>
18
#include "util.h"
19
 
20
#define IA 1103515245u
21
#define IC 12345u
22
#define IM 2147483648u
23
 
24
using namespace std;
25
 
26
static unsigned int current_random = 0;
27
 
28
 
29
char select_AI_colour (int argc, char **argv){
30
    char ai_colour;
31
    int i;
32
    //cout<<"Please enter referee AI's colour. L or D"<<endl;
33
    //cin >> ai_colour;
34
        for(i=0;i<argc; i++){
35
          if((strcmp(argv[i],"-player")==0) && (i< (argc+1)) ){
36
                   ai_colour= *argv[i+1];
37
          }
38
        }
39
 
40
    while (ai_colour != 'L' && ai_colour != 'D'){
41
        cout<<"Invalid colour. Single character L or D"<<endl;
42
        cin >> ai_colour;
43
    }
44
 
45
    cout<<"AI is playing as "<<ai_colour<<endl;
46
    return ai_colour;
47
}
48
 
49
 
50
int select_com_port(int argc, char **argv)
51
{
52
  string com_port;
53
  int i, port;
54
  bool cmd_line_port_set = false;
55
 
56
  for(i=0;i<argc; i++){
57
          if((strcmp(argv[i],"-port")==0) && (i< (argc+1)) ){
58
                  com_port = argv[i+1];
59
                  cmd_line_port_set = true;
60
          }
61
  }
62
  if( !cmd_line_port_set ){
63
    cout << "Please enter serial port name. Ex. /dev/comx (windows) or /dev/ttyx (linux)\n";
64
    cin >> com_port;
65
  }
66
 
67
 
68
  port = open(com_port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
69
  while(port < 0) // if open is unsucessful keep trying until the user specifies a good port
70
  {
71
    cout << "Unable to open port " << com_port << ", try again, should be: (windows) /dev/comx or (linux) /dev/ttyx ?\n";
72
    cin >> com_port;
73
    port = open(com_port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
74
  }
75
  setup_port(port);
76
 
77
  cout << "COM port has been set up at a baud rate of 115200\n";
78
  return port;
79
}
80
 
81
void setup_port(int fd) {
82
    struct termios options;
83
    fcntl(fd, F_SETFL, 0);
84
    tcgetattr(fd, &options);
85
    cfsetispeed(&options, B115200);
86
    cfsetospeed(&options, B115200);
87
    options.c_cflag |= (CLOCAL | CREAD);
88
    tcsetattr(fd, TCSANOW, &options);
89
 
90
    // set up non-blocking port, so that we can time out
91
        int opts;
92
        opts = fcntl(fd,F_GETFL);
93
        if (opts < 0) {
94
                perror("fcntl(F_GETFL)");
95
                exit(EXIT_FAILURE);
96
        }
97
        opts = (opts | O_NONBLOCK);
98
        if (fcntl(fd,F_SETFL,opts) < 0) {
99
                perror("fcntl(F_SETFL)");
100
                exit(EXIT_FAILURE);
101
        }
102
        return;
103
}
104
 
105
int char_to_int (char c){
106
    if (c == '0') return 0;
107
    else if (c == '1') return 1;
108
    else if (c == '2') return 2;
109
    else if (c == '3') return 3;
110
    else if (c == '4') return 4;
111
    else if (c == '5') return 5;
112
    else if (c == '6') return 6;
113
    else if (c == '7') return 7;
114
    else if (c == '8') return 8;
115
    else if (c == '9') return 9;
116
 
117
    return 0;
118
}
119
 
120
void wait(double seconds){
121
    timeval tim;
122
    gettimeofday(&tim, NULL);
123
    double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
124
    while (1){
125
        gettimeofday(&tim, NULL);
126
        double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
127
        if (t2-t1 >= seconds)
128
            break;
129
    }
130
}
131
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.