C conversion of weather example WIP

This commit is contained in:
equant 2021-09-18 22:50:44 -07:00
parent e127ebf816
commit 8224a04545
27 changed files with 188 additions and 785 deletions

View File

@ -0,0 +1,4 @@
obj/apple2/apple2idiot.o: src/apple2idiot.c src/globals.h src/apple2idiot.h
src/apple2idiot.c src/globals.h src/apple2idiot.h:

Binary file not shown.

View File

@ -0,0 +1,4 @@
obj/apple2/foo.o: src/foo.c src/globals.h src/foo.h
src/foo.c src/globals.h src/foo.h:

Binary file not shown.

View File

@ -0,0 +1,4 @@
obj/apple2/inputline.o: src/inputline.c src/screen.h src/menu.h
src/inputline.c src/screen.h src/menu.h:

Binary file not shown.

View File

@ -0,0 +1,4 @@
obj/apple2/main.o: src/main.c src/globals.h src/menu.h src/apple2idiot.h
src/main.c src/globals.h src/menu.h src/apple2idiot.h:

Binary file not shown.

View File

@ -0,0 +1,4 @@
obj/apple2/menu.o: src/menu.c src/globals.h src/menu.h
src/menu.c src/globals.h src/menu.h:

Binary file not shown.

View File

@ -0,0 +1,4 @@
obj/apple2/screen.o: src/screen.c
src/screen.c:

Binary file not shown.

View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <peekpoke.h>
#include "globals.h"
#include "apple2idiot.h"
unsigned char read_data(unsigned char address) {
gotoxy(0,2);
printf("read_data()");
}
unsigned char write_data(unsigned char address, unsigned char byte_to_write) {
printf("%d [%c]\n", address, byte_to_write);
POKE(address, byte_to_write);
//for (i=0;i<8;i++)
//{
//p = PEEK(0xC200+i);
//printf("(%u) -> %d, %c\n", 0xC200+i, p, p);
//}
//cursor(1);
//p = cgetc();
//gotoxy(0, SIZE_Y-5);
//printf("You said: %c\n", p);
}
unsigned char* write_string_to_ram(char* string_to_send, unsigned char address) {
unsigned char i, p, c;
unsigned char size = strlen(string_to_send);
gotoxy(0,2);
if (string_to_send[size-1] == '\n') {
string_to_send[size-1] = 0;
}
//printf("write_string_to_ram()");
for (i=0; i<size; i++) {
write_data(address+i, string_to_send[i]);
}
if (string_to_send[size-1] != 0) {
write_data(address+i, 0);
}
gotoxy(0,9);
p = cgetc();
}
unsigned char* read_string_from_ram(char* string_to_send, unsigned char address) {
gotoxy(0,2);
printf("read_string_from_ram()");
}

View File

@ -0,0 +1,10 @@
#ifndef _A2I_H
#define _A2I_H
unsigned char read_data(unsigned char address);
unsigned char write_data(unsigned char address, unsigned char byte_to_write);
unsigned char* write_string_to_ram(char* string_to_send, unsigned char address);
unsigned char* read_string_from_ram(char* string_to_send, unsigned char address);
#endif

Binary file not shown.

View File

@ -0,0 +1,17 @@
#ifndef _GLOBALS_H
#define _GLOBALS_H
#define ASCII_0 48
#define ASCII_1 49
#define ASCII_2 50
#define ASCII_3 51
#define ASCII_4 52
#define SIZE_X 40
#define SIZE_Y 24
#define COMMAND_SET_COUNTRY 200
#define COMMAND_SET_CITY 201
#define COMMAND_FETCH_WEATHER 205
#endif

View File

@ -1,148 +0,0 @@
/*
* This code is not original to slapple2, I took it from cvote.
*/
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <apple2.h>
#include "screen.h"
#include "menu.h"
unsigned char ibuf[100];
static unsigned char allowme[256];
unsigned char ptoa(unsigned char p) {
unsigned char a = p;
if (p >= 192) {
if (p >= 224) {
if (p == 255) {
a = 126;
} else {
a -= 64;
}
} else {
a -= 96;
}
}
return(a);
}
void printbuf(unsigned char x, unsigned char y, unsigned char width, unsigned char *buffer) {
gotoxy(x, y);
PrintLeftString(buffer, width);
sprintf("%c", buffer);
}
unsigned char inputline(unsigned char x, unsigned char y, unsigned char width,
unsigned char maxlen, unsigned char *buffer, unsigned char *allowed) {
unsigned char offset = 0, pos = 0;
unsigned char len, i, c;
pos = len = strlen(buffer);
if (len > maxlen) {
pos = len = maxlen;
buffer[pos] = 0;
}
if (len >= width) {
offset += len - width + 1;
pos -= offset;
}
printbuf(x, y, width, buffer + offset);
cursor(1);
while (1) {
gotoxy(0, 1);
cprintf("pos = %2d, offset = %2d, len = %2d", pos, offset, len);
gotoxy(x + pos, y);
switch (c = cgetc()) {
default:
c = ptoa(c);
if (allowme[c] && len < maxlen) {
++len;
memmove(buffer + pos + offset + 1, buffer + pos + offset, strlen(buffer + pos + offset) + 1);
buffer[pos + offset] = c;
buffer[len] = 0;
printbuf(x, y, width, buffer + offset);
}
// fall through
case CH_CURS_RIGHT:
if (pos + offset < len) {
if (pos < width - 1) {
pos += 1;
} else if (len > offset + pos) {
offset += 1;
if (offset + pos == len) {
cputsxy(x + width - 1, y, " ");
}
printbuf(x, y, width, buffer + offset);
}
}
break;
case CH_CURS_LEFT:
if (pos > 0) {
pos -= 1;
} else if (offset > 0) {
offset -= 1;
printbuf(x, y, width, buffer + offset);
}
break;
case 3: // stop
len = 0;
// fall through
case 13: // return
ClearBox(width, 1, x, y);
cursor(0);
return(len);
break;
case 20: // del
if (pos || offset) {
if (pos) {
pos -= 1;
} else {
offset -= 1;
}
i = offset + pos;
while (buffer[i]) {
buffer[i] = buffer[i+1];
++i;
}
--len;
if (offset + pos == len) {
cputsxy(x + pos, y, " ");
} else if (len - offset < width) {
cputsxy(x + len - offset, y, " ");
}
printbuf(x, y, width, buffer + offset);
}
break;
case 19: // home
if (pos || offset) {
pos = offset = 0;
printbuf(x, y, width, buffer + offset);
}
break;
case 147: // clear
pos = offset = len = 0;
buffer[0] = 0;
ClearBox(width, 1, x, y);
break;
}
}
}

View File

@ -1,6 +0,0 @@
extern unsigned char ibuf[];
unsigned char inputline(unsigned char x, unsigned char y, unsigned char width,
unsigned char maxlen, unsigned char *buffer, unsigned char *allowed);

View File

@ -1,81 +1,60 @@
#include <stdio.h>
#include <conio.h>
#include <peekpoke.h>
#include <ctype.h>
#include <apple2.h>
//#include <stdlib.h>
//#include <stdbool.h>
//#include <stdlib.h>
//#include <stdio.h>
//#include <string.h>
//#include <peekpoke.h>
#include "main.h"
#include "inputline.h"
#include "globals.h"
#include "menu.h"
#include "apple2idiot.h"
unsigned char i = 0;
char p;
char text[20];
//unsigned char menuEntries[8][4] = {"HLP", "END", "SEL", "REF", "CPY", "REN", "MDR", "DEL"};
//unsigned char menuEntries[MENU_LENGTH][MENU_WORD_LENGTH] = {"CHANNEL", "MISC"};
// Writes the menu bar at the top of the screen
// which is scaled to the current screen size.
void writeMenuBar(void)
{
static unsigned char i, x;
//cclearxy(0, bottom, size_x);
cclearxy(0, 0, SIZE_X);
for(i = 0; i < MENU_LENGTH; i++){
//x = i*5;
x = (MENU_WORD_LENGTH + 1) * i;
revers(1);
//gotoxy(x, bottom); cputc('1' + i);
gotoxy(x, 0); cputc('1' + i);
revers(0);
//cputsxy(x + 1, bottom, menuEntries[i]);
cputsxy(x + 1, 0, menuEntries[i]);
}
revers(0);
}
void main(void)
{
//screensize(&size_x, &size_y);
clrscr();
writeMenuBar();
//cvlinexy(0,1,size_x);
gotoxy(0, 1);
chline(SIZE_X);
gotoxy(0, 3);
//printf("Screensize is %d by %d\n", size_x, size_y);
printf("Hello, world!\n");
for (i=0;i<8;i++)
{
p = PEEK(0xC200+i);
printf("(%u) -> %d, %c\n", 0xC200+i, p, p);
unsigned char key;
while (key != ASCII_0) { // Last menu item should be Quit
clrscr();
drawMenuBar();
//cvlinexy(0,1,size_x);
//fancy_hline(0,2);
//fputs("enter some text: ", stdout);
//fflush(stdout); /* http://c-faq.com/stdio/fflush.html */
//fgets(text, sizeof text, stdin);
//printf("\ntext = \"%s\"", text);
//revers(1);
gotoxy(0,SIZE_Y-1);
printf("Selection:");
cursor(1);
//revers(0);
//p = cgetc();
key = toupper(cgetc());
gotoxy(0,SIZE_Y-3);
printf("cgetc() -> \"%c\"", key);
switch((int)key) {
case ASCII_0:
printf("QUIT!");
break;
case ASCII_1:
printf("COUNTRY!");
write_string_to_ram("US", 0xC202);
break;
case ASCII_2:
printf("City: ");
fgets(text, sizeof text, stdin);
write_string_to_ram(text, 0xC202);
break;
case ASCII_3:
printf("UPDATE!");
break;
default:
printf("DEFAULT!");
break;
}
}
cursor(1);
p = cgetc();
gotoxy(0, SIZE_Y-5);
printf("You said: %c\n", p);
//inputline(1, 17, 38, 53, ibuf, NULL);
//gotoxy(1, 17);
//printf("YO: %c\n", ibuf);
//cursor(1);
//p = cgetc();
fputs("enter some text: ", stdout);
fflush(stdout); /* http://c-faq.com/stdio/fflush.html */
fgets(text, sizeof text, stdin);
printf("\ntext = \"%s\"\n", text);
//printf("YO: %c\n", ibuf);
printf("Press any key to exit.");
cursor(1);
p = cgetc();
}

View File

@ -1,10 +0,0 @@
#define MENU_LENGTH 3
#define MENU_WORD_LENGTH 7
#define SIZE_X 40
#define SIZE_Y 24
//unsigned char menuEntries[MENU_LENGTH][MENU_WORD_LENGTH] = {"HLP", "END", "SEL", "REF", "CPY", "REN", "MDR", "DEL"};
unsigned char menuEntries[MENU_LENGTH][MENU_WORD_LENGTH] = {"CHANNEL", "MESSAGE", "END"};
//unsigned char ibuf[100];

Binary file not shown.

View File

@ -1,354 +1,35 @@
/*
Menu handling
*/
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <apple2.h>
#include "menu.h"
#include "screen.h"
unsigned char menu_bc = 1; /* border color */
unsigned char menu_tc = 7; /* title color */
unsigned char menu_nc = 13; /* normal item color */
unsigned char menu_ic = 5; /* inactive item color */
unsigned char menu_xpos = 5; /* default menu position */
unsigned char menu_ypos = 2;
void DrawItem(Item *item, unsigned char xpos, unsigned char ypos, unsigned char current);
Menu *NewMenu(void) {
register Menu *menu;
if ((menu = malloc(sizeof(*menu))) == NULL) {
return(NULL);
}
menu->visible = 0;
menu->numitems = 0;
menu->current = 0;
menu->xpos = menu_xpos;
menu->ypos = menu_ypos;
menu->width = 0;
menu->height = 0;
menu->title = NULL;
menu->firstitem = NULL;
return(menu);
}
void SetMenuTitle(Menu *menu, unsigned char *title) {
unsigned char l;
menu->title = title;
if ((l = strlen(title)) > menu->width) {
menu->width = l;
}
}
signed char AddMenuItem(Menu *menu, unsigned char id, ItemState state, unsigned char *name) {
register Item *item;
int l;
if (id == MENU_CANCEL) { /* 0 is reserved for cancelling */
return(-1);
}
if (menu->numitems == 0) {
if ((menu->firstitem = malloc(sizeof(*item))) == NULL) {
return(-1);
}
item = menu->firstitem;
item->prev = NULL;
} else {
item = menu->firstitem;
while (item->next) {
item = item->next;
}
if ((item->next = malloc(sizeof(*item))) == NULL) {
return(-1);
}
item->next->prev = item;
item = item->next;
}
item->next = NULL;
item->id = id;
item->name = name;
item->state = state;
++(menu->numitems);
if ((l = strlen(name)) > menu->width) {
menu->width = l;
}
if (menu->numitems > menu->height) {
menu->height = menu->numitems;
}
return(0);
}
signed char DelMenuItem(Menu *menu, unsigned char id) {
register Item *item;
if (menu->numitems) {
item = menu->firstitem;
while (item) {
if (item->id == id) {
if (menu->firstitem == item) {
menu->firstitem = item->next;
item->next->prev = NULL;
free(item);
} else {
item->prev->next = item->next;
item->next->prev = item->prev;
free(item);
}
--(menu->numitems);
return(0);
}
item = item->next;
}
}
return(-1);
}
signed char SetItemState(Menu *menu, unsigned char id, ItemState state) {
register Item *item;
if (menu->numitems) {
item = menu->firstitem;
while (item) {
if (item->id == id) {
item->state = state;
return(0);
}
item = item->next;
}
}
return(-1);
}
void ShowMenu(Menu *menu) {
if (menu->visible == 0) {
menu->visible = 1;
DrawMenu(menu);
}
}
void HideMenu(Menu *menu) {
if (menu->visible) {
menu->visible = 0;
DrawMenu(menu);
}
}
Item *FindItemNumber(Menu *menu, unsigned char itemnum) {
Item *item;
item = menu->firstitem;
while (itemnum) {
if (item->next) {
item = item->next;
--itemnum;
} else {
return(NULL);
}
}
return(item);
}
Item *FindItem(Menu *menu, unsigned char id) {
Item *item;
item = menu->firstitem;
while (item->id != id) {
if (item->next) {
item = item->next;
} else {
return(NULL);
}
}
return(item);
}
signed char SelectMenu(register Menu *menu) {
unsigned char c;
register Item *item;
if ((item = FindItemNumber(menu, menu->current)) == NULL) {
return(-1);
}
if (menu->visible == 0) {
return(-1);
}
while(c = cgetc()) {
switch (c) {
case 27:
case 3:
return(MENU_CANCEL);
case '\r':
case '\n':
if (item->state == ISTATE_NORMAL) {
return(item->id);
}
break;
/*
case CH_CURS_DOWN:
if (menu->current + 1 < menu->numitems) {
DrawItem(item, menu->xpos + 1, menu->ypos + menu->current + 3, 0);
item = item->next;
++(menu->current);
DrawItem(item, menu->xpos + 1, menu->ypos + menu->current + 3, 1);
}
break;
case CH_CURS_UP:
if (menu->current > 0) {
DrawItem(item, menu->xpos + 1, menu->ypos + menu->current + 3, 0);
item = item->prev;
--(menu->current);
DrawItem(item, menu->xpos + 1, menu->ypos + menu->current + 3, 1);
}
break;
*/
}
}
}
void DestroyMenu(Menu *menu) {
Item *item;
Item *next;
if (menu->numitems) {
item = menu->firstitem;
while (item) {
next = item->next;
free(item);
item = next;
}
}
free(menu);
}
void DrawItem(Item *item, unsigned char xpos, unsigned char ypos, unsigned char current) {
unsigned char c;
gotoxy(xpos, ypos);
if (current) {
revers(1);
}
if (item->state == ISTATE_NORMAL) {
c = textcolor(menu_nc);
cprintf(item->name);
} else {
c = textcolor(menu_ic);
cprintf(item->name);
}
textcolor(c);
revers(0);
}
signed char DrawMenu(register Menu *menu) {
unsigned char l = 0;
Item *item;
if (menu->numitems == 0) {
return(-1);
} else {
if (menu->title == NULL) {
return(-1);
} else {
if (menu->visible) {
textcolor(menu_bc);
DrawBox(menu->width+2,menu->height+4,menu->xpos,menu->ypos);
gotoxy(menu->xpos + 1, menu->ypos + 1);
textcolor(menu_tc);
cprintf(menu->title);
item = menu->firstitem;
while (item) {
DrawItem(item, menu->xpos + 1, menu->ypos + l + 3, menu->current == l ? 1 : 0);
item = item->next;
++l;
}
} else {
ClearBox(menu->width+2,menu->height+4,menu->xpos,menu->ypos);
}
}
}
}
/* Ask if user is sure */
unsigned char AskAreYouSure(Menu *menu) {
unsigned char x, y, sx, sy;
screensize(&sx, &sy);
if ((x = menu->xpos + 3) + 15 > sx) {
x = sx - 16;
}
if ((y = menu->ypos + menu->current + 4) + 3 > sy) {
y = sy - 3;
}
textcolor(menu_bc);
DrawBox(17, 3, x, y);
ClearBox(15, 1, x + 1, y + 1);
textcolor(menu_nc);
cputsxy(x + 1, y + 1, "Are you sure? ");
while (1) {
cursor(1);
sx = cgetc();
cursor(0);
switch (sx) {
case 'y':
case 'Y':
ClearBox(17, 3, x, y);
return(1);
break;
case 'n':
case 'N':
ClearBox(17, 3, x, y);
return(0);
break;
default:
break;
}
}
}
/* Display message, wait for enter */
void DisplayMessage(unsigned char *message) {
unsigned char l = strlen(message);
textcolor(menu_bc);
DrawBox(l + 2, 5, 20 - (l>>1) - (l & 1), 10);
ClearBox(l, 3, 21 - (l>>1) - (l & 1), 11);
textcolor(menu_nc);
cputsxy(21 - (l>>1) - (l & 1), 11, message);
revers(1);
cputsxy(18, 13, " OK ");
revers(0);
while (cgetc() != 13);
ClearBox(l + 2, 5, 20 - (l>>1) - (l & 1), 10);
}
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <apple2.h>
#include "globals.h"
#include "menu.h"
#define MENU_LENGTH 4
#define MENU_WORD_LENGTH 8
unsigned char menuEntries[MENU_LENGTH][MENU_WORD_LENGTH] = {"Country", "City", "Update", "Quit"};
void drawMenuBar() {
static unsigned char i;
static unsigned char x_offset;
x_offset = 0;
cclearxy(0, 0, SIZE_X);
gotoxy(0,0);
for(i = 0; i < MENU_LENGTH; i++){
//printf("%d, %d, %d\n", i, strlen(menuEntries[i]), x_offset);
revers(1);
gotoxy(x_offset, 0); cputc('1' + i);
revers(0);
cputsxy(x_offset + 1, 0, menuEntries[i]);
x_offset += strlen(menuEntries[i]) + 1;
}
revers(0);
}
void fancy_hline(unsigned char x, unsigned char y) {
gotoxy(x,y);
printf("<><>-------------------------------<><>\n");
}

View File

@ -1,94 +1,7 @@
/*
Menu handling
*/
#define CH_CURS_UP 0x0B
#define CH_CURS_DOWN 0x0A
#define MENU_CANCEL 0
#define MENU_YES 1
#define MENU_NO 0
extern unsigned char menu_bc; /* border color */
extern unsigned char menu_tc; /* title color */
extern unsigned char menu_nc; /* normal item color */
extern unsigned char menu_ic; /* inactive item color */
extern unsigned char menu_xpos; /* default menu position */
extern unsigned char menu_ypos;
typedef enum itemstates {
ISTATE_NORMAL,
ISTATE_INACTIVE
} ItemState;
typedef struct item {
struct item *next;
struct item *prev;
unsigned char id;
unsigned char *name;
ItemState state;
} Item;
typedef struct menu {
unsigned char visible;
unsigned char numitems;
unsigned char current;
unsigned char xpos;
unsigned char ypos;
unsigned char width;
unsigned char height;
unsigned char *title;
Item *firstitem;
} Menu;
/* Set menu colors */
#define SetMenuColors(BC, TC, NC, IC) \
menu_bc = (BC); \
menu_tc = (TC); \
menu_nc = (NC); \
menu_ic = (IC);
/* Set default menu position */
#define SetMenuDefaultPosition(XPOS, YPOS) \
menu_xpos = (XPOS); \
menu_ypos = (YPOS);
/* Initialize an empty menu. */
Menu *NewMenu(void);
/* Deallocate menu resources. */
void DestroyMenu(Menu *menu);
/* Set the title. */
void SetMenuTitle(Menu *menu, unsigned char *title);
/* Add an item to the menu. id is the value that SelectMenu will return, which must not be 0. */
signed char AddMenuItem(Menu *menu, unsigned char id, ItemState state, unsigned char *name);
/* Delete an item from the menu. */
signed char DelMenuItem(Menu *menu, unsigned char id);
/* Set item state to normal or inactive. */
signed char SetItemState(Menu *menu, unsigned char id, ItemState state);
/* Display the menu on the screen. */
void ShowMenu(Menu *menu);
signed char DrawMenu(Menu *menu);
/* Erase the menu from the screen. */
void HideMenu(Menu *menu);
/* Let the user choose a menu item. ShowMenu() first. */
signed char SelectMenu(Menu *menu);
/* Ask if user is sure */
unsigned char AskAreYouSure(Menu *menu);
/* Display message, wait for enter */
void DisplayMessage(unsigned char *message);
#ifndef _FOO_H
#define _FOO_H
void fancy_hline(unsigned char x, unsigned char y);
void drawMenuBar(void);
#endif

View File

@ -1,86 +0,0 @@
/*
Screen handling
*/
#include <conio.h>
#include <string.h>
static unsigned char screen_bgc;
static unsigned char screen_borderc;
static unsigned char screen_textc;
static unsigned char *spaces = " ";
/* initialize screen mode */
void InitScreen(unsigned char *title, unsigned char border, unsigned char bg, unsigned char text) {
screen_borderc = bordercolor(border);
screen_bgc = bgcolor(bg);
screen_textc = textcolor(text);
clrscr();
revers(1);
cprintf(title);
revers(0);
}
/* restore basic screen mode */
void ExitScreen(void) {
bordercolor(screen_borderc);
bgcolor(screen_bgc);
textcolor(screen_textc);
clrscr();
}
/* Draw petscii box */
void DrawBox(unsigned char xsize, unsigned char ysize, unsigned char xpos, unsigned char ypos) {
/* Top line */
gotoxy(xpos,ypos);
cputc(CH_ULCORNER);
chline(xsize - 2);
cputc(CH_URCORNER);
/* Vertical line, left side */
cvlinexy(xpos, ypos + 1, ysize - 2);
/* Bottom line */
cputc(CH_LLCORNER);
chline(xsize - 2);
cputc(CH_LRCORNER);
/* Vertical line, right side */
cvlinexy(xpos + xsize - 1, ypos + 1, ysize - 2);
}
/* Clear screen area */
void ClearBox(unsigned char xsize, unsigned char ysize, unsigned char xpos, unsigned char ypos) {
unsigned char *s;
unsigned char line;
s = spaces + 80 - xsize;
for (line = 0; line < ysize; ++line) {
gotoxy(xpos, ypos + line);
cprintf(s);
}
}
/* print maxlen leftmost chars of string */
void PrintLeftString(unsigned char *string, unsigned char maxlen) {
unsigned char c;
if ((c = strlen(string)) <= maxlen) {
cputs(string);
cputs(spaces + 80 + c - maxlen);
} else {
c = string[maxlen];
string[maxlen] = 0;
cputs(string);
string[maxlen] = c;
}
}

View File

@ -1,20 +0,0 @@
/*
Screen handling
*/
/* Initialize screen mode, set title and colors */
void InitScreen(unsigned char *title, unsigned char border, unsigned char bg, unsigned char text);
/* Restore basic screen mode */
void ExitScreen(void);
/* Draw petscii box */
void DrawBox(unsigned char xsize, unsigned char ysize, unsigned char xpos, unsigned char ypos);
/* Clear screen area */
void ClearBox(unsigned char xsize, unsigned char ysize, unsigned char xpos, unsigned char ypos);
/* print maxlen leftmost chars of string */
void PrintLeftString(unsigned char *string, unsigned char maxlen);

Binary file not shown.

Binary file not shown.