mirror of
https://github.com/equant/apple2idiot.git
synced 2024-05-28 09:41:33 +00:00
basic chess implemented. From whaleshark
This commit is contained in:
parent
6c4dc88bab
commit
fb6f2ff38b
|
@ -72,20 +72,18 @@ byte Apple2Idiot::read_data(int address) {
|
|||
|
||||
|
||||
boolean Apple2Idiot::write_data(byte address, byte byte_to_write) {
|
||||
Serial.print("WRITE: ");
|
||||
Serial.print(byte_to_write);
|
||||
Serial.print(" -> ");
|
||||
Serial.println(address);
|
||||
if (address == 0) {
|
||||
Serial.print("WRITE: ");
|
||||
Serial.print(byte_to_write);
|
||||
Serial.print(" -> ");
|
||||
Serial.println(address);
|
||||
}
|
||||
if (set_address(address)) {
|
||||
//set_address(address);
|
||||
//Serial.print(" D:");
|
||||
for (byte i=0; i<DATA_BUS_SIZE; i++) {
|
||||
byte bit_to_write = (byte_to_write >> i) & 0b00000001;
|
||||
pinMode(data_pins[i], OUTPUT);
|
||||
digitalWrite(data_pins[i], bit_to_write);
|
||||
//Serial.print(bit_to_write);
|
||||
}
|
||||
//Serial.println();
|
||||
digitalWrite(RW_PIN, RW_WRITE);
|
||||
delay(1);
|
||||
digitalWrite(RW_PIN, RW_READ);
|
||||
|
@ -102,18 +100,14 @@ boolean Apple2Idiot::write_data(byte address, byte byte_to_write) {
|
|||
|
||||
unsigned int Apple2Idiot::write_string_to_shared_ram(String string_to_send, unsigned int address) {
|
||||
//if (string_to_send.length() > 15 - 1) { // - 1 because of null character at end of string.
|
||||
write_data(ESP_COMMAND_ADDRESS, 12);
|
||||
//write_data(ESP_COMMAND_ADDRESS, 12);
|
||||
unsigned int c = 2;
|
||||
for (c=0; c < string_to_send.length(); c++) {
|
||||
//Serial.print("A(");
|
||||
//Serial.print(c);
|
||||
//Serial.print("): ");
|
||||
//Serial.println(string_to_send[c]);
|
||||
write_data(address+c, string_to_send[c]);
|
||||
}
|
||||
write_data(address+c, EOS);
|
||||
//write_data(15, COMMAND_FROM_ESP + command_message + COMMAND_NO_DATA_WAITING);
|
||||
write_data(ESP_COMMAND_ADDRESS, 27);
|
||||
//write_data(ESP_COMMAND_ADDRESS, 27);
|
||||
return address+c;
|
||||
}
|
||||
|
||||
|
@ -123,7 +117,7 @@ String Apple2Idiot::read_string_from_ram(int address) {
|
|||
String read_string = "";
|
||||
while ( (i<MAX_STR_LEN) && (c!=EOS) ) {
|
||||
c = read_data(address+i);
|
||||
Serial.print("["); Serial.print(c); Serial.println("]");
|
||||
//Serial.print("["); Serial.print(c); Serial.println("]");
|
||||
if (c!=EOS) {
|
||||
read_string = read_string + char(c);
|
||||
}
|
||||
|
@ -137,7 +131,7 @@ String Apple2Idiot::read_string_from_ram(int address) {
|
|||
void Apple2Idiot::read_ram(int size_to_read) {
|
||||
for (int i=0; i < size_to_read; i++) {
|
||||
unsigned int foo = read_data(i);
|
||||
ram[i] = foo;
|
||||
//ram[i] = foo;
|
||||
Serial.print(i);
|
||||
Serial.print(" ");
|
||||
Serial.println(foo);
|
||||
|
|
|
@ -5,13 +5,16 @@
|
|||
|
||||
#include "globals.h"
|
||||
#include "apple2idiot.h"
|
||||
#include "../../../../arduino-lib/Apple2Idiot/A2I_commands.h"
|
||||
|
||||
#define MAX_STR_LEN 250
|
||||
|
||||
|
||||
unsigned char read_byte(unsigned int address) {
|
||||
gotoxy(0,2);
|
||||
unsigned int read_byte(unsigned int address) {
|
||||
unsigned int b = 0;
|
||||
b = PEEK(address);
|
||||
//printf("read_data(%u)", address);
|
||||
return b;
|
||||
}
|
||||
|
||||
unsigned char write_byte(unsigned int address, unsigned char byte_to_write) {
|
||||
|
@ -19,6 +22,31 @@ unsigned char write_byte(unsigned int address, unsigned char byte_to_write) {
|
|||
POKE(address, byte_to_write);
|
||||
}
|
||||
|
||||
unsigned char write_byte_wait_for_ack(unsigned int address, unsigned char byte_to_write) {
|
||||
unsigned char received_esp_response = 0;
|
||||
int timeout_count = 0;
|
||||
unsigned char timeout_happened = 0;
|
||||
int delay_count = 0;
|
||||
unsigned char read_char;
|
||||
write_byte(address, byte_to_write);
|
||||
while ((received_esp_response==0) || (timeout_happened==0)) {
|
||||
timeout_count++;
|
||||
if (timeout_count > ESP_TIMEOUT) {
|
||||
timeout_happened = 1;
|
||||
return 0;
|
||||
}
|
||||
//read_char = read_byte(ESP_COMMAND_ADDRESS);
|
||||
read_char = read_byte(APPLE_COMMAND_ADDRESS);
|
||||
if (read_char == ACK) {
|
||||
received_esp_response = 1;
|
||||
return 1;
|
||||
}
|
||||
for (delay_count=0; delay_count < 1111; ++delay_count) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char* write_string_to_ram(unsigned int address, char* string_to_send) {
|
||||
unsigned char i;
|
||||
unsigned char size = strlen(string_to_send);
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
#ifndef _A2I_H
|
||||
#define _A2I_H
|
||||
|
||||
unsigned char read_byte(unsigned int address);
|
||||
unsigned int read_byte(unsigned int address);
|
||||
unsigned char write_byte(unsigned int address, unsigned char byte_to_write);
|
||||
unsigned char* write_string_to_ram(unsigned int address, char* string_to_send);
|
||||
void read_string_from_ram(unsigned int address, char *data, char length);
|
||||
unsigned char write_byte_wait_for_ack(unsigned int address, unsigned char byte_to_write);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -23,25 +23,21 @@ byte Chess::handleCommand(byte command) {
|
|||
move_string = a2i->read_string_from_ram(SHARED_RAM_START_ADDRESS);
|
||||
Serial.println("Received move: ["+move_string+"]");
|
||||
byte result = makeMove(move_string);
|
||||
a2i->write_data(result, ESP_COMMAND_ADDRESS);
|
||||
int address_counter = a2i->write_string_to_shared_ram(last_ai_move, SHARED_RAM_START_ADDRESS);
|
||||
getBoard();
|
||||
for (int i=0; i<9; i++) {
|
||||
address_counter = a2i->write_string_to_shared_ram(game_board[i], address_counter + 1);
|
||||
}
|
||||
a2i->write_data(ESP_COMMAND_ADDRESS, result);
|
||||
a2i->read_ram(11);
|
||||
break;
|
||||
}
|
||||
case CHESS_NEW_GAME: {
|
||||
a2i->write_data(ESP_COMMAND_ADDRESS, ACK); // notify Apple IIe we are processing command byte
|
||||
Serial.println("CHESS_NEW_GAME");
|
||||
strcpy(game_string, "");
|
||||
break;
|
||||
}
|
||||
//case COMMAND_SET_CITY:
|
||||
//Serial.println("COMMAND_SET_CITY");
|
||||
//a2i.write_data(ESP_COMMAND_ADDRESS, ACK); // notify Apple IIe we are processing command byte
|
||||
//city_name = a2i.read_string_from_ram(SHARED_RAM_START_ADDRESS);
|
||||
//Serial.println("Received: ["+city_name+"]");
|
||||
//break;
|
||||
//case COMMAND_FETCH_WEATHER:
|
||||
//Serial.println("COMMAND_FETCH_WEATHER");
|
||||
//a2i.write_data(ESP_COMMAND_ADDRESS, ACK); // notify Apple IIe we are processing command byte
|
||||
//result = fetch_weather();
|
||||
//break;
|
||||
default: {
|
||||
return COMMAND_NOT_FOUND;
|
||||
}
|
||||
|
@ -265,8 +261,10 @@ void Chess::getBoard() {
|
|||
removeSubstr(board_line, "[37m");
|
||||
removeSubstr(board_line, "[0m");
|
||||
removeSubstr(board_line, "\n");
|
||||
Serial.print("(");Serial.print(row_count);Serial.print(")");
|
||||
Serial.print(board_line); Serial.println("|");
|
||||
removeSubstr(board_line, "\e"); // remove escape (ASCII 27)
|
||||
removeSubstr(board_line, "\e"); // remove escape (ASCII 27)
|
||||
//Serial.print("(");Serial.print(row_count);Serial.print(")");
|
||||
//Serial.print(board_line); Serial.println("|");
|
||||
strcpy(game_board[row_count], board_line); // valid
|
||||
pch = strtok(NULL, "\n");
|
||||
row_count++;
|
||||
|
|
|
@ -5,5 +5,5 @@ This is how I compile, make a disk and then stage the disk for transfer to my //
|
|||
```
|
||||
make
|
||||
make TARGETS=apple2 dsk
|
||||
cp weather.dsk ~/bin/ADTPro-2.1.0/disks/
|
||||
cp chess.dsk ~/bin/ADTPro-2.1.0/disks/
|
||||
```
|
||||
|
|
Binary file not shown.
BIN
examples/chess/apple2-cc65/chess.apple2enh
Normal file
BIN
examples/chess/apple2-cc65/chess.apple2enh
Normal file
Binary file not shown.
BIN
examples/chess/apple2-cc65/chess.dsk
Normal file
BIN
examples/chess/apple2-cc65/chess.dsk
Normal file
Binary file not shown.
|
@ -1,4 +1,4 @@
|
|||
obj/apple2/apple2idiot.o: src/apple2idiot.c src/globals.h src/apple2idiot.h
|
||||
obj/apple2/apple2idiot.o: src/apple2idiot.c src/globals.h src/apple2idiot.h src/../../../../arduino-lib/Apple2Idiot/A2I_commands.h
|
||||
|
||||
src/apple2idiot.c src/globals.h src/apple2idiot.h:
|
||||
src/apple2idiot.c src/globals.h src/apple2idiot.h src/../../../../arduino-lib/Apple2Idiot/A2I_commands.h:
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
4
examples/chess/apple2-cc65/obj/apple2enh/apple2idiot.d
Normal file
4
examples/chess/apple2-cc65/obj/apple2enh/apple2idiot.d
Normal file
|
@ -0,0 +1,4 @@
|
|||
obj/apple2enh/apple2idiot.o: src/apple2idiot.c src/globals.h src/apple2idiot.h src/../../../../arduino-lib/Apple2Idiot/A2I_commands.h
|
||||
|
||||
src/apple2idiot.c src/globals.h src/apple2idiot.h src/../../../../arduino-lib/Apple2Idiot/A2I_commands.h:
|
||||
|
4
examples/chess/apple2-cc65/obj/apple2enh/main.d
Normal file
4
examples/chess/apple2-cc65/obj/apple2enh/main.d
Normal file
|
@ -0,0 +1,4 @@
|
|||
obj/apple2enh/main.o: src/main.c src/globals.h src/menu.h src/apple2idiot.h src/../../chess_commands.h src/../../../../arduino-lib/Apple2Idiot/A2I_commands.h
|
||||
|
||||
src/main.c src/globals.h src/menu.h src/apple2idiot.h src/../../chess_commands.h src/../../../../arduino-lib/Apple2Idiot/A2I_commands.h:
|
||||
|
4
examples/chess/apple2-cc65/obj/apple2enh/menu.d
Normal file
4
examples/chess/apple2-cc65/obj/apple2enh/menu.d
Normal file
|
@ -0,0 +1,4 @@
|
|||
obj/apple2enh/menu.o: src/menu.c src/globals.h src/menu.h
|
||||
|
||||
src/menu.c src/globals.h src/menu.h:
|
||||
|
331
examples/chess/apple2-cc65/src/;
Normal file
331
examples/chess/apple2-cc65/src/;
Normal file
|
@ -0,0 +1,331 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <conio.h>
|
||||
#include <ctype.h>
|
||||
#include <apple2.h>
|
||||
#include <peekpoke.h>
|
||||
|
||||
#include "globals.h"
|
||||
#include "menu.h"
|
||||
#include "apple2idiot.h"
|
||||
#include "../../chess_commands.h"
|
||||
#include "../../../../arduino-lib/Apple2Idiot/A2I_commands.h"
|
||||
|
||||
unsigned char i = 0;
|
||||
char pause_char;
|
||||
char player_move[5];
|
||||
char ai_move[5];
|
||||
char state = STATE_NONE;
|
||||
unsigned int address_offset = 0;
|
||||
|
||||
char game_board[9][24] = {
|
||||
"8 r n b q k b n r",
|
||||
"7 p p p p p p p p",
|
||||
"6 . . . . . . . .",
|
||||
"5 . . . . . . . .",
|
||||
"4 . . . . . . . .",
|
||||
"3 . . . . . . . .",
|
||||
"2 P P P P P P P P",
|
||||
"1 R N B Q K B N R",
|
||||
" a b c d e f g h"
|
||||
};
|
||||
|
||||
|
||||
void displayCurrentBoard(void);
|
||||
void createNewBoard(void); // deprecated
|
||||
void spinner(void);
|
||||
void awaitMoveResponse(void);
|
||||
void popup(unsigned char* message);
|
||||
void createNewGame(void);
|
||||
|
||||
void popup(unsigned char* message) {
|
||||
//cclearxy(x, y, SIZE_X);
|
||||
//gotoxy(x,y);
|
||||
cclearxy(0, 17, SIZE_X);
|
||||
cclearxy(0, 18, SIZE_X);
|
||||
cclearxy(0, 19, SIZE_X);
|
||||
cclearxy(0, 20, SIZE_X);
|
||||
gotoxy(0,17);
|
||||
printf("+-------------------------------------+\n");
|
||||
printf("! %s \n", message);
|
||||
printf("+-------------------------------------+\n");
|
||||
printf("(Press a key)\n");
|
||||
if (kbhit()) {
|
||||
cgetc();
|
||||
}
|
||||
pause_char = cgetc();
|
||||
cclearxy(0, 17, SIZE_X);
|
||||
cclearxy(0, 18, SIZE_X);
|
||||
cclearxy(0, 19, SIZE_X);
|
||||
cclearxy(0, 20, SIZE_X);
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
unsigned char key;
|
||||
while (state != STATE_QUIT) {
|
||||
switch(state) {
|
||||
case STATE_LOCAL_GAME:
|
||||
displayCurrentBoard();
|
||||
cclearxy(0, 23, SIZE_X);
|
||||
gotoxy(0,23);
|
||||
printf("Your Move:");
|
||||
fgets(player_move, sizeof player_move, stdin);
|
||||
player_move[strcspn(player_move, "\n")] = 0;
|
||||
if (strcmp(player_move, "quit") == 0) {
|
||||
state = STATE_NONE;
|
||||
} else {
|
||||
write_string_to_ram(RAM_DATA_START_ADDRESS, player_move);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, CHESS_MAKE_MOVE);
|
||||
awaitMoveResponse();
|
||||
//displayCurrentBoard();
|
||||
}
|
||||
break;
|
||||
case STATE_NONE:
|
||||
clrscr();
|
||||
drawMenuBar();
|
||||
gotoxy(0,23);
|
||||
printf("Menu Selection:");
|
||||
cursor(1);
|
||||
key = toupper(cgetc());
|
||||
switch(key) {
|
||||
case ASCII_W:
|
||||
cclearxy(0, 11, SIZE_X);
|
||||
gotoxy(0,11);
|
||||
printf("+--------------------------------------+\n");
|
||||
printf("! Not implemented yet. !\n");
|
||||
printf("+--------------------------------------+\n");
|
||||
printf("(Press a key)\n");
|
||||
pause_char = cgetc();
|
||||
break;
|
||||
case ASCII_L:
|
||||
//createNewBoard();
|
||||
state = STATE_LOCAL_GAME;
|
||||
write_byte_wait_for_ack(APPLE_COMMAND_ADDRESS, APP_CHESS);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, CHESS_NEW_GAME);
|
||||
createNewGame();
|
||||
break;
|
||||
case ASCII_Q:
|
||||
state = STATE_QUIT;
|
||||
break;
|
||||
default:
|
||||
popup("Not implemented yet.");
|
||||
//cclearxy(0, 11, SIZE_X);
|
||||
//gotoxy(0,11);
|
||||
//printf("+--------------------------------------+\n");
|
||||
//printf("! Not implemented yet. !\n");
|
||||
//printf("+--------------------------------------+\n");
|
||||
//printf("(Press a key)\n");
|
||||
//pause_char = cgetc();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void spinner(void) {
|
||||
// From https://github.com/bobbimanners/emailler
|
||||
static char chars[] = "|/-\\";
|
||||
//static uint8_t i = 0;
|
||||
static char i = 0;
|
||||
//putchar(BACKSPACE);
|
||||
//putchar(chars[(i++) % 4]);
|
||||
gotoxy(10+4+2,23);
|
||||
cputc(chars[(i++) % 4]);
|
||||
}
|
||||
|
||||
|
||||
void awaitMoveResponse(void) {
|
||||
int delay_count = 0;
|
||||
int timeout_count = 0;
|
||||
unsigned char read_char;
|
||||
int received_esp_response = 0;
|
||||
gotoxy(10+4+2,23);
|
||||
//spinner();
|
||||
|
||||
//while (received_esp_response==0 || timeout_count < ESP_TIMEOUT) {
|
||||
while (received_esp_response==0) {
|
||||
timeout_count++;
|
||||
read_char = read_byte(ESP_COMMAND_ADDRESS);
|
||||
switch((int)read_char) {
|
||||
case STATUS_IN_PROGRESS:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_BLACK_WON:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_WHITE_WON:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_STALEMATE:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_FIFTY_RULE_MOVE:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_UNKNOWN:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_UNHANDLED:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_ERROR:
|
||||
//received_esp_response = 1;
|
||||
break;
|
||||
case CHESS_INVALID_MOVE:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// delay
|
||||
for (delay_count=0; delay_count < 1111; ++delay_count) {
|
||||
spinner();
|
||||
}
|
||||
}
|
||||
if (timeout_count == ESP_TIMEOUT) {
|
||||
popup("ESP TIMEOUT");
|
||||
return;
|
||||
}
|
||||
|
||||
//getBoard();
|
||||
switch(read_char) {
|
||||
case STATUS_IN_PROGRESS:
|
||||
//popup("STATUS_IN_PROGRESS");
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS, ai_move, sizeof ai_move-1);
|
||||
popup(ai_move);
|
||||
//gotoxy(0,0);
|
||||
//printf("AI Move: %s\n", ai_move);
|
||||
address_offset = 5;
|
||||
for (i=0; i<8; i++) {
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, game_board[i], sizeof game_board[i]-1);
|
||||
//printf("[%u][%s]\n", RAM_DATA_START_ADDRESS + address_offset, game_board[i]);
|
||||
address_offset += strlen(game_board[i]) + 1;
|
||||
}
|
||||
break;
|
||||
case STATUS_BLACK_WON:
|
||||
popup("You lose.");
|
||||
break;
|
||||
case STATUS_WHITE_WON:
|
||||
popup("You win.");
|
||||
break;
|
||||
case STATUS_STALEMATE:
|
||||
popup("Stalemate");
|
||||
break;
|
||||
case STATUS_FIFTY_RULE_MOVE:
|
||||
popup("Fifty move rule.");
|
||||
break;
|
||||
case STATUS_UNKNOWN:
|
||||
popup("STATUS_UNKNOWN");
|
||||
break;
|
||||
case STATUS_UNHANDLED:
|
||||
popup("STATUS_UNHANDLED");
|
||||
break;
|
||||
case STATUS_ERROR:
|
||||
popup("STATUS_ERROR");
|
||||
break;
|
||||
case CHESS_INVALID_MOVE:
|
||||
popup("CHESS_INVALID_MOVE");
|
||||
break;
|
||||
default:
|
||||
popup("Unrecognised response from ESP.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void displayCurrentBoard(void) {
|
||||
unsigned char i;
|
||||
for (i=0; i<9; i++) {
|
||||
cclearxy(0, i+7, SIZE_X);
|
||||
gotoxy(10,i+7);
|
||||
printf("%s", game_board[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void createNewGame(void) {
|
||||
|
||||
strcpy(game_board[0], "8 r n b q k b n r");
|
||||
strcpy(game_board[1], "7 p p p p p p p p");
|
||||
strcpy(game_board[2], "6 . . . . . . . .");
|
||||
strcpy(game_board[3], "5 . . . . . . . .");
|
||||
strcpy(game_board[4], "4 . . . . . . . .");
|
||||
strcpy(game_board[5], "3 . . . . . . . .");
|
||||
strcpy(game_board[6], "2 P P P P P P P P");
|
||||
strcpy(game_board[7], "1 R N B Q K B N R");
|
||||
strcpy(game_board[8], " a b c d e f g h");
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
while (key != ASCII_5) { // Last menu item should be Quit
|
||||
drawMenuBar();
|
||||
gotoxy(0,SIZE_Y-3);
|
||||
//printf("cgetc() -> \"%c\"", key);
|
||||
|
||||
switch((int)key) {
|
||||
case ASCII_0:
|
||||
break;
|
||||
case ASCII_5:
|
||||
break;
|
||||
case ASCII_1:
|
||||
clrscr();
|
||||
drawMenuBar();
|
||||
gotoxy(0,SIZE_Y-1);
|
||||
printf("Enter Country Code:");
|
||||
fgets(country_code, sizeof country_code, stdin);
|
||||
strncpy(country_code, country_code, strlen(country_code));
|
||||
//country_code[strcspn(country_code, "\n")] = 0;
|
||||
write_string_to_ram(RAM_DATA_START_ADDRESS, country_code);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, COMMAND_SET_COUNTRY);
|
||||
//printf("C:[%s]", country_code);
|
||||
//pause_char = cgetc();
|
||||
break;
|
||||
case ASCII_2:
|
||||
clrscr();
|
||||
drawMenuBar();
|
||||
gotoxy(0,SIZE_Y-1);
|
||||
printf("Enter City: ");
|
||||
fgets(city, sizeof city, stdin);
|
||||
city[strcspn(city, "\n")] = 0;
|
||||
write_string_to_ram(RAM_DATA_START_ADDRESS, city);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, COMMAND_SET_CITY);
|
||||
//printf("C:[%s]", city);
|
||||
//pause_char = cgetc();
|
||||
break;
|
||||
case ASCII_3:
|
||||
clrscr();
|
||||
drawMenuBar();
|
||||
write_byte(APPLE_COMMAND_ADDRESS, COMMAND_FETCH_WEATHER);
|
||||
break;
|
||||
case ASCII_4:
|
||||
clrscr();
|
||||
drawMenuBar();
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, temperature, sizeof temperature-1);
|
||||
address_offset += strlen(temperature) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, humidity, sizeof humidity-1);
|
||||
address_offset += strlen(humidity) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, wind_speed, sizeof wind_speed-1);
|
||||
address_offset += strlen(wind_speed) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, wind_direction, sizeof wind_direction-1);
|
||||
address_offset += strlen(wind_direction) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, weather1, sizeof weather1-1);
|
||||
address_offset += strlen(weather1) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, weather2, sizeof weather2-1);
|
||||
gotoxy(0,SIZE_Y-11);
|
||||
printf("%s (%s)\n", city, country_code);
|
||||
printf("-------------------------------------\n");
|
||||
printf("Temp: %s K\n", temperature);
|
||||
printf("Humidity: %s%%\n", humidity);
|
||||
printf("Wind Speed: %s m/s\n", wind_speed);
|
||||
printf("Wind Dir: %s \n", wind_direction);
|
||||
printf("Summary: %s, %s \n", weather1, weather2);
|
||||
printf("-------------------------------------\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
|
@ -18,17 +18,16 @@ char ai_move[5];
|
|||
char state = STATE_NONE;
|
||||
unsigned int address_offset = 0;
|
||||
|
||||
char game_board[10][24] = {
|
||||
"8: r n b q k b n r",
|
||||
"7: p p p p p p p p",
|
||||
"6: . . . . . . . .",
|
||||
"5: . . . . . . . .",
|
||||
"4: . . . . . . . .",
|
||||
"3: . . . . . . . .",
|
||||
"2: P P P P P P P P",
|
||||
"1: R N B Q K B N R",
|
||||
" ---------------",
|
||||
" a b c d e f g h"
|
||||
char game_board[9][24] = {
|
||||
"8 r n b q k b n r",
|
||||
"7 p p p p p p p p",
|
||||
"6 . . . . . . . .",
|
||||
"5 . . . . . . . .",
|
||||
"4 . . . . . . . .",
|
||||
"3 . . . . . . . .",
|
||||
"2 P P P P P P P P",
|
||||
"1 R N B Q K B N R",
|
||||
" a b c d e f g h"
|
||||
};
|
||||
|
||||
|
||||
|
@ -37,15 +36,16 @@ void createNewBoard(void); // deprecated
|
|||
void spinner(void);
|
||||
void awaitMoveResponse(void);
|
||||
void popup(unsigned char* message);
|
||||
void createNewGame(void);
|
||||
|
||||
void popup(unsigned char* message) {
|
||||
//cclearxy(x, y, SIZE_X);
|
||||
//gotoxy(x,y);
|
||||
cclearxy(0, 7, SIZE_X);
|
||||
cclearxy(0, 8, SIZE_X);
|
||||
cclearxy(0, 9, SIZE_X);
|
||||
cclearxy(0, 10, SIZE_X);
|
||||
gotoxy(0,7);
|
||||
cclearxy(0, 17, SIZE_X);
|
||||
cclearxy(0, 18, SIZE_X);
|
||||
cclearxy(0, 19, SIZE_X);
|
||||
cclearxy(0, 20, SIZE_X);
|
||||
gotoxy(0,17);
|
||||
printf("+-------------------------------------+\n");
|
||||
printf("! %s \n", message);
|
||||
printf("+-------------------------------------+\n");
|
||||
|
@ -54,6 +54,10 @@ void popup(unsigned char* message) {
|
|||
cgetc();
|
||||
}
|
||||
pause_char = cgetc();
|
||||
cclearxy(0, 17, SIZE_X);
|
||||
cclearxy(0, 18, SIZE_X);
|
||||
cclearxy(0, 19, SIZE_X);
|
||||
cclearxy(0, 20, SIZE_X);
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
|
@ -67,9 +71,15 @@ void main(void) {
|
|||
printf("Your Move:");
|
||||
fgets(player_move, sizeof player_move, stdin);
|
||||
player_move[strcspn(player_move, "\n")] = 0;
|
||||
write_string_to_ram(RAM_DATA_START_ADDRESS, player_move);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, CHESS_MAKE_MOVE);
|
||||
awaitMoveResponse();
|
||||
//if (strcmp(player_move, "quit") == 0) {
|
||||
if (player_move[0] == 'q') {
|
||||
state = STATE_NONE;
|
||||
} else {
|
||||
write_string_to_ram(RAM_DATA_START_ADDRESS, player_move);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, CHESS_MAKE_MOVE);
|
||||
awaitMoveResponse();
|
||||
//displayCurrentBoard();
|
||||
}
|
||||
break;
|
||||
case STATE_NONE:
|
||||
clrscr();
|
||||
|
@ -91,6 +101,9 @@ void main(void) {
|
|||
case ASCII_L:
|
||||
//createNewBoard();
|
||||
state = STATE_LOCAL_GAME;
|
||||
write_byte_wait_for_ack(APPLE_COMMAND_ADDRESS, APP_CHESS);
|
||||
write_byte(APPLE_COMMAND_ADDRESS, CHESS_NEW_GAME);
|
||||
createNewGame();
|
||||
break;
|
||||
case ASCII_Q:
|
||||
state = STATE_QUIT;
|
||||
|
@ -130,44 +143,17 @@ void awaitMoveResponse(void) {
|
|||
unsigned char read_char;
|
||||
int received_esp_response = 0;
|
||||
gotoxy(10+4+2,23);
|
||||
spinner();
|
||||
//spinner();
|
||||
|
||||
while (received_esp_response==0 && timeout_count < ESP_TIMEOUT) {
|
||||
//while (received_esp_response==0 || timeout_count < ESP_TIMEOUT) {
|
||||
while (received_esp_response==0) {
|
||||
timeout_count++;
|
||||
read_char = read_byte(ESP_COMMAND_ADDRESS);
|
||||
switch(read_char) {
|
||||
case STATUS_IN_PROGRESS:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_BLACK_WON:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_WHITE_WON:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_STALEMATE:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_FIFTY_RULE_MOVE:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_UNKNOWN:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_UNHANDLED:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case STATUS_ERROR:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
case CHESS_INVALID_MOVE:
|
||||
received_esp_response = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
read_char = read_byte(APPLE_COMMAND_ADDRESS);
|
||||
if (read_char == ACK) {
|
||||
received_esp_response = 1;
|
||||
}
|
||||
// delay
|
||||
for (i=0; delay_count < 5555; ++delay_count) {
|
||||
for (delay_count=0; delay_count < 1111; ++delay_count) {
|
||||
spinner();
|
||||
}
|
||||
}
|
||||
|
@ -176,22 +162,23 @@ void awaitMoveResponse(void) {
|
|||
return;
|
||||
}
|
||||
|
||||
read_char = read_byte(ESP_COMMAND_ADDRESS);
|
||||
//gotoxy(0,0);
|
||||
//printf("FROMESP: %s\n", read_char);
|
||||
|
||||
//getBoard();
|
||||
switch(read_char) {
|
||||
switch((int)read_char) {
|
||||
case STATUS_IN_PROGRESS:
|
||||
popup("STATUS_IN_PROGRESS");
|
||||
//popup("STATUS_IN_PROGRESS");
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS, ai_move, sizeof ai_move-1);
|
||||
popup(sprintf("AI Move: %s", ai_move));
|
||||
//popup(&ai_move);
|
||||
gotoxy(0,SIZE_Y-2);
|
||||
printf("AI Move: %s\n", ai_move);
|
||||
address_offset = 5;
|
||||
for (i=0; i<9; i++) {
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, game_board[0], sizeof game_board[0]-1);
|
||||
address_offset += strlen(game_board[0]) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, game_board[1], sizeof game_board[1]-1);
|
||||
address_offset += strlen(game_board[1]) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, game_board[2], sizeof game_board[2]-1);
|
||||
address_offset += strlen(game_board[2]) + 1;
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, game_board[3], sizeof game_board[3]-1);
|
||||
address_offset += strlen(game_board[3]) + 1;
|
||||
for (i=0; i<8; i++) {
|
||||
read_string_from_ram(RAM_DATA_START_ADDRESS + address_offset, game_board[i], sizeof game_board[i]-1);
|
||||
//printf("[%u][%s]\n", RAM_DATA_START_ADDRESS + address_offset, game_board[i]);
|
||||
address_offset += strlen(game_board[i]) + 1;
|
||||
}
|
||||
break;
|
||||
case STATUS_BLACK_WON:
|
||||
|
@ -226,39 +213,25 @@ void awaitMoveResponse(void) {
|
|||
|
||||
void displayCurrentBoard(void) {
|
||||
unsigned char i;
|
||||
for (i=0; i<10; i++) {
|
||||
for (i=0; i<9; i++) {
|
||||
cclearxy(0, i+7, SIZE_X);
|
||||
gotoxy(10,i+7);
|
||||
printf("%s", game_board[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void createNewBoard(void) {
|
||||
void createNewGame(void) {
|
||||
|
||||
cclearxy(0, 7, SIZE_X);
|
||||
gotoxy(10,7);
|
||||
printf("8: r n b q k b n r");
|
||||
strcpy(game_board[0], "8 r n b q k b n r");
|
||||
strcpy(game_board[1], "7 p p p p p p p p");
|
||||
strcpy(game_board[2], "6 . . . . . . . .");
|
||||
strcpy(game_board[3], "5 . . . . . . . .");
|
||||
strcpy(game_board[4], "4 . . . . . . . .");
|
||||
strcpy(game_board[5], "3 . . . . . . . .");
|
||||
strcpy(game_board[6], "2 P P P P P P P P");
|
||||
strcpy(game_board[7], "1 R N B Q K B N R");
|
||||
strcpy(game_board[8], " a b c d e f g h");
|
||||
|
||||
cclearxy(0, 8, SIZE_X);
|
||||
gotoxy(10,8);
|
||||
printf("7: p p p p p p p p");
|
||||
|
||||
gotoxy(10,9);
|
||||
printf("6: . . . . . . . .");
|
||||
gotoxy(10,10);
|
||||
printf("5: . . . . . . . .");
|
||||
gotoxy(10,11);
|
||||
printf("4: . . . . . . . .");
|
||||
gotoxy(10,12);
|
||||
printf("3: . . . . . . . .");
|
||||
gotoxy(10,13);
|
||||
printf("2: P P P P P P P P");
|
||||
gotoxy(10,14);
|
||||
printf("1: R N B Q K B N R");
|
||||
gotoxy(10,15);
|
||||
printf(" ---------------");
|
||||
gotoxy(10,16);
|
||||
printf(" a b c d e f g h");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -33,8 +33,8 @@ byte app_ids[N_APPS] = {APP_CHESS};
|
|||
|
||||
byte current_app_id;
|
||||
|
||||
//const long mainLoopInterval = 100; // millis
|
||||
const long mainLoopInterval = 10000; // millis
|
||||
const long mainLoopInterval = 100; // millis
|
||||
//const long mainLoopInterval = 10000; // millis
|
||||
unsigned long lastMainLoopTime = 0;
|
||||
byte lastAppleCommand = 0;
|
||||
|
||||
|
@ -101,17 +101,17 @@ void loop() {
|
|||
Serial.println("We are talking to a new app");
|
||||
a2i.write_data(ESP_COMMAND_ADDRESS, ACK);
|
||||
current_app_id = command_byte;
|
||||
a2i.write_data(APPLE_COMMAND_ADDRESS, ACK);
|
||||
a2i.write_data(ESP_COMMAND_ADDRESS, EOT);
|
||||
lastMainLoopTime = millis();
|
||||
return;
|
||||
}
|
||||
a2i.write_data(APPLE_COMMAND_ADDRESS, ACK);
|
||||
a2i.write_data(ESP_COMMAND_ADDRESS, EOT);
|
||||
lastMainLoopTime = millis();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we got here, we need to pass the command to app's class to be handled */
|
||||
|
||||
if (command_byte == chess_app.appId) {
|
||||
if (current_app_id == chess_app.appId) {
|
||||
Serial.println("Received a command for Chess()");
|
||||
chess_app.handleCommand(command_byte);
|
||||
a2i.write_data(APPLE_COMMAND_ADDRESS, ACK);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define A2I_CHESS_COMMANDS_H
|
||||
|
||||
/* Apple II <-> ESP Commands */
|
||||
#define CHESS_NEW_GAME 10
|
||||
#define CHESS_GET_AI_MOVE 20
|
||||
#define CHESS_GET_GAME_STATUS 22
|
||||
#define CHESS_GET_BOARD 23
|
||||
|
|
Loading…
Reference in New Issue
Block a user