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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [ai/] [serial_port.c] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 sumanta.ch
#include <stdlib.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <unistd.h>
5
#include <fcntl.h>
6
#include <errno.h>
7
#include <termios.h>
8
#include <sys/time.h>
9
#include "../shared.h"
10
 
11
 
12
/* All serial functions work on this board */
13
static Board *myboard = NULL;
14
static port=0;
15
// The AI has as much time as it wants, but moves after 1 second. Default is to wait 2 seconds
16
#define AI_WAIT_TIME 0.5
17
 
18
// FPGA has 1 second to make its move
19
#define MOVE_TIME_LIMIT 0.5
20
 
21
static int char_to_int(short x){
22
                if(x>=48)
23
                        return x-48;
24
                else
25
                        return 0;
26
}
27
//void wait(double seconds){
28
//    timeval tim;
29
//    gettimeofday(&tim, NULL);
30
//    double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
31
//    while (1){
32
//        gettimeofday(&tim, NULL);
33
//        double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
34
//        if (t2-t1 >= seconds)
35
//            break;
36
//    }
37
//}
38
 
39
void setup_port(int fd) {
40
    struct termios options;
41
    fcntl(fd, F_SETFL, 0);
42
    tcgetattr(fd, &options);
43
    cfsetispeed(&options, B115200);
44
    cfsetospeed(&options, B115200);
45
    options.c_cflag |= (CLOCAL | CREAD);
46
    tcsetattr(fd, TCSANOW, &options);
47
 
48
    // set up non-blocking port, so that we can time out
49
        int opts;
50
        opts = fcntl(fd,F_GETFL);
51
        if (opts < 0) {
52
                perror("fcntl(F_GETFL)");
53
                exit(EXIT_FAILURE);
54
        }
55
        opts = (opts | O_NONBLOCK);
56
        if (fcntl(fd,F_SETFL,opts) < 0) {
57
                perror("fcntl(F_SETFL)");
58
                exit(EXIT_FAILURE);
59
        }
60
        return;
61
}
62
 
63
 
64
 
65
void move_to_ascii(int x,int y, char move[4]){
66
    if (y >= 10){
67
        move[0] = '1';
68
        y -= 10;
69
    } else {
70
        move[0] = '0';
71
    }
72
    if      (y == 0) move[1] = '0';
73
    else if (y == 1) move[1] = '1';
74
    else if (y == 2) move[1] = '2';
75
    else if (y == 3) move[1] = '3';
76
    else if (y == 4) move[1] = '4';
77
    else if (y == 5) move[1] = '5';
78
    else if (y == 6) move[1] = '6';
79
    else if (y == 7) move[1] = '7';
80
    else if (y == 8) move[1] = '8';
81
    else if (y == 9) move[1] = '9';
82
 
83
    // Do same for x.
84
    if (x >= 10){
85
        move[2] = '1';
86
        x -= 10;
87
    } else {
88
        move[2] = '0';
89
    }
90
    if      (x == 0) move[3] = '0';
91
    else if (x == 1) move[3] = '1';
92
    else if (x == 2) move[3] = '2';
93
    else if (x == 3) move[3] = '3';
94
    else if (x == 4) move[3] = '4';
95
    else if (x == 5) move[3] = '5';
96
    else if (x == 6) move[3] = '6';
97
    else if (x == 7) move[3] = '7';
98
    else if (x == 8) move[3] = '8';
99
    else if (x == 9) move[3] = '9';
100
 
101
}
102
 
103
 
104
 
105
 
106
 
107
 
108
 
109
 
110
 
111
 
112
 
113
AIMoves *ai_serial(const Board *b){
114
int i,j,firstmove=0;
115
char move[4];
116
 
117
AIMoves *moves = aimoves_new();
118
AIMove fpgamove;
119
 
120
 
121
 
122
 
123
        for(i=0;i<board_size;i++){
124
                for(j=0;j<board_size;j++){
125
                        if (piece_at(b,i,j)!=PIECE_NONE) firstmove++;
126
        }
127
        }
128
        if(firstmove<=1){
129
                port = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
130
                setup_port(port);
131
 
132
                if(b->turn==PIECE_WHITE){
133
                        write(port, "L",1);
134
                }else{
135
                        write(port, "D",1);
136
 
137
                }
138
                printf("firstmove %d\n",firstmove);
139
                if(!myboard){
140
                        myboard=board_new();
141
                }else{
142
                        board_free(myboard);
143
                        myboard=board_new();
144
                }
145
 
146
        }
147
        if(b->moves_left >1 || (firstmove<=1)){
148
        for(i=0;i<board_size;i++){
149
                for(j=0;j<board_size;j++){
150
                        if ((piece_at(myboard,i,j)==PIECE_NONE) && (piece_at(b,i,j)!=PIECE_NONE)){
151
                                printf("%d,%d\n",i+1,j+1);
152
                                move_to_ascii(i+1,j+1,move);
153
                                write(port,&move[0],1);
154
                                write(port,&move[1],1);
155
                                write(port,&move[2],1);
156
                                write(port,&move[3],1);
157
                                printf("AI move %c%c %c%c\n", move[2],move[3],move[0],move[1] );
158
                        }
159
                }
160
 
161
        }
162
 
163
        }
164
        sleep(1);
165
        //move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
166
        ////////// Get Opponent's move
167
        read(port,&move[0],1);
168
        read(port,&move[1],1);
169
        read(port,&move[2],1);
170
        read(port,&move[3],1);
171
        fpgamove.y=char_to_int(move[0])*10 + char_to_int(move[1])-1;
172
        fpgamove.x=char_to_int(move[2])*10 + char_to_int(move[3])-1;
173
        printf("FPGA move %d %d\n", fpgamove.x, fpgamove.y);
174
        aimoves_append(moves, &fpgamove);
175
        //move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
176
        //read(port,&move[0],1);
177
        //read(port,&move[1],1);
178
        //read(port,&move[2],1);
179
        //read(port,&move[3],1);
180
        //                      printf("FPGA move %c%c %c%c\n", move[2],move[3],move[0],move[1] );
181
        //fpgamove.x=char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
182
        //fpgamove.y=char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
183
        //printf("FPGA move %d %d\n", fpgamove.x, fpgamove.y);
184
        //aimoves_append(moves, &fpgamove);
185
        board_copy(b,myboard);
186
        place_piece_type(myboard,fpgamove.x,fpgamove.y,b->turn);
187
        return moves;
188
}

powered by: WebSVN 2.1.0

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