JU Bat Blinker Development Docs: spinwords.c

Gadget Sourcecode: spinwords.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define USE_MORSE   0

typedef struct Phrase_s {
    int num;
    char *phrase;
} Phrase_t;

typedef struct Char_s {
    char *name;
    char *val[6];
    int   pos;
} Char_t;

Phrase_t list[] = {


#if 1
    { 1, "\"What will you do now, Batman?\" asked the Riddler as the dynamic duo dangled over the hostages."},
    { 9, "\"If you stay still, one of the hostages gets showered!\""},
    { 3, "\"And the wetter they get, the more they'll attract the rabid rats!\""},
    { 5, "\"If you move, all the hostages drop into the Bay!\" The Riddler laughed and ran off."},
    { 12,"\"Holy bursting bladders, Batman!\" Robin cried. \"I can't hold it any more! I have to pee!\""},
    { 5, "\"You've got to hold it a little longer, Robin!\" said Batman as he tapped his utility belt."},
    { 4, "\"Once this solvent dries, the rope will be brittle enough for you to break free!\""},

#else
    { 9, "Bruce and Dick were enjoying a drink at Daniel's pub." },
    { 7, "The view of the gotham city nature preserve outside was more breathtaking than la."},
    { 9, "Suddenly the riddler crashed his honda into the center of the bar."},
    { 13, "riddle me this he exclaimed as the honda exploded against the gotham skyline."},
    { 9, "how many russian deer can run along thornwood ridge without falling off?"},
    { 4, "dont you dare open that can of worms exclaimed dick."},
    { 5, "there is not enough space for both of us in this town cried bruce."},
    { 10, "you'll never foil my plan to sell venison preserve jam cackled riddler as he ran off."},
#endif
};
#if USE_MORSE
char *morse_num[10] = {
    "-----",
    ".----",
    "..---",
    "...--",
    "....-",
    ".....",
    "-....",
    "--...",
    "---..",
    "----.",
};
#endif

#define PHRASE_CNT (sizeof(list)/sizeof(list[0]))
#define LETTER_CNT (sizeof(letters)/sizeof(letters[0]))

FILE *out = 0;
int g_position = 0;


Char_t letters[] = {
    { "a", {
        "####.",
        ".#..#",
        ".#..#",
        "####.",},},
    { "b", {
        "#####",
        "#.#.#",
        "#.#.#",
        ".#.#.",},},
    { "c", {
        ".###.",
        "#...#",
        "#...#",
        "#...#",},},
    { "d", {
        "#####",
        "#...#",
        "#...#",
        ".###.",},},

    { "e", {
        "#####",
        "#.#.#",
        "#.#.#",
        "#.#.#",},},
    { "f", {
        "#####",
        "..#.#",
        "..#.#",
        "..#.#",},},
    { "g", {
        ".###.",
        "#...#",
        "##..#",
        "##..#",},},
    { "h", {
        "#####",
        "..#..",
        "..#..",
        "#####",},},
    { "i", {
        "#...#",
        "####.",
        "#...#",
        ".....",},},
    { "j", {
        ".#..#",
        "#...#",
        ".####",
        "....#",},},
    { "k", {
        "#####",
        "..#..",
        ".#.#.",
        "#...#",},},
    { "l", {
        "####",
        "#....",
        "#....",
        "#....",},},
    { "m", {
        "#####",
        "...#.",
        "..#..",
        "...#.",
        "#####",},},
    { "n", {
        "#####",
        "...#.",
        "..#..",
        "#####",},},
    { "o", {
        ".###.",
        "#...#",
        "#...#",
        ".###.",},},
    { "p", {
        "#####",
        "..#.#",
        "..#.#",
        "...#.",},},
    { "q", {
        ".###.",
        "#...#",
        "##..#",
        "####.",},},
    { "r", {
        "#####",
        "..#.#",
        ".##.#",
        "#..#.",},},
    { "s", {
        "#.###",
        "#.#.#",
        "#.#.#",
        "###.#",},},
    { "t", {
        "....#",
        "#####",
        "....#",
        ".....",},},
    { "u", {
        "#####",
        "#....",
        "#....",
        "#####",},},
    { "v", {
        ".####",
        "#....",
        ".####",
        ".....",},},
    { "w", {
        ".####",
        "##...",
        "..##.",
        "##...",
        ".####",},},
    { "x", {
        "##..#",
        "..##.",
        "..##.",
        "##..#",},},
    { "y", {
        "#...#",
        ".###.",
        "...#.",
        "....#",},},
    { "z", {
        "#...#",
        "##..#",
        "#.#.#",
        "#..##",},},
    { "colon", {
        ".....",
        ".#.#.",
        ".#.#.",
        ".....",},},
    { "period", {
        ".....",
        "##...",
        "##...",
        ".....",},},
    { "comma", {
        ".....",
        "#....",
        ".#...",
        ".....",},},
    { "apostrophe", {
        ".....",
        "...##",
        ".....",},},
    { "quote", {
        "...##",
        ".....",
        "...##",},},
    { "qmark", {
        "...#.",
        "....#",
        "##..#",
        "..##.",},},
    { "exclamation", {
        ".....",
        "#.###",
        "#.###",
        ".....",},},

};



#define ASSERT(cond)    { if (!(cond)) { assert_fail(#cond); }}
void assert_fail(char *cond)
{
    fprintf(stderr,"ASSERT FAILED: '%s'\n",cond);
    exit(1);
}

#if USE_MORSE
void morse(int n)
{
    char *s;
    ASSERT(n<=9 && n>=0);

    s = morse_num[n];

    fprintf(out,"morse_%d:\n",n);
    while(*s) {
        if (*s == '.') {
            fprintf(out,"\tcall\tmorse_dot\n");
        } else {
            ASSERT(*s == '-');
            fprintf(out,"\tcall\tmorse_dash\n");
        }
        s++;
    }
    fprintf(out,"\tgoto\tmorse_next\n");
}

/*
 * output number n in morse code (2 decimal digits)
 */
void num_out(int n)
{
    int n0,n1;
    n0 = p->num / 10;
    n1 = p->num - (10 * n0);
    fprintf(out,"\tcall\tmorse_%d\n",n0);
    fprintf(out,"\tcall\tmorse_%d\n",n1);
}

#else
/*
 * blink green LED n times
 */
void num_out(int n)
{
    for(; n>0; n--) {
        fprintf(out,"\tcall\tdot\n");
    }
    fprintf(out,"\tcall\twait_500\n");
}
#endif

void phrase(Phrase_t *p, int id)
{
    char *s;
    fprintf(out,";\n");
    fprintf(out,"; PHRASE %d:\n",id);
    fprintf(out,"; \"%s\"\n",p->phrase);
    fprintf(out,"; %d dots\n",p->num);
    fprintf(out,";\n");
    fprintf(out,"phrase%d:\n",id);

    num_out(p->num);

    fprintf(out,"phrase%d_loop:\n",id);

    fprintf(out,"\tcall\tstart_loop\n");

    s = p->phrase;

    while(*s) {
        int c = tolower(*s);
        if (isalpha(c)) {
            fprintf(out,"\tcall\tl%c\n",c);
        } else if (isspace(c)) {
            fprintf(out,"\tcall\tlspace\n");
        } else if (c=='?') {
            fprintf(out,"\tcall\tlqmark\n");
        } else if (c=='!') {
            fprintf(out,"\tcall\tlexclamation\n");
        } else if (c=='.') {
            fprintf(out,"\tcall\tlperiod\n");
        } else if (c==',') {
            fprintf(out,"\tcall\tlcomma\n");
        } else if (c==':') {
            fprintf(out,"\tcall\tlcolon\n");
        } else if (c=='"') {
            fprintf(out,"\tcall\tlquote\n");
        } else if (c=='\'') {
            fprintf(out,"\tcall\tlapostrophe\n");
        } else {
            fprintf(stderr,"UNRECOGNIZED CHAR: %c (%d == 0x%02x)\n",isprint(c)?c:'?',c,c);
        }
        s++;
    }
    
    fprintf(out,"\tgoto\tphrase%d_loop\n",id);
    fprintf(out,"\n\n");
}

#if 0
#if 0
void letter_out(Char_t *l)
{
    int i;
    fprintf(out,"l%s:\n",l->name);
    for (i=0;;) {
        fprintf(out,"\tmovlw\tB'%s'\n",l->val[i]);
        i++;
        if (l->val[i] == 0) break;
        fprintf(out,"\tcall\tset_wait\n");
    }
    fprintf(out,"\tgoto\tspace1\n");
    fprintf(out,"\n");

}
#else
void letter_out(Char_t *l)
{
    int i,j,spc;
    char cmmt[6],*s;
    cmmt[5] = 0;
    fprintf(out,"l%s:\n",l->name);
    for (i=0;;) {
        spc = 30;
        s = cmmt;
        fprintf(out,"\tmovlw\t0");
        for (j=0;j<5; j++) {
            if (l->val[i][7-j] == '0') {
                fprintf(out,"|RED%d",j);
                *(s++) = '#';
                spc -= 5;
            } else {
                *(s++) = '.';
            }
        }
        fprintf(out,"%*s; %s\n",spc,"",cmmt);
        i++;
        if (l->val[i] == 0) break;
        fprintf(out,"\tcall\tred\n");
    }
    fprintf(out,"\tgoto\tred_last\n");
    fprintf(out,"\n");

}
#endif
#else

void letter_out1(Char_t *l)
{
    int i,j,spc;
    l->pos = g_position;
    fprintf(out,"dots_%s:\n",l->name);
    for (i=0;;) {
        spc = 38;
        fprintf(out,"\tretlw\t0");
        for (j=0;j<5; j++) {
            if (l->val[i][j] == '#') {
                fprintf(out,"|RED%d",j);
                spc -= 5;
            } else {
            }
        }
        if (l->val[i+1] == 0) {
            fprintf(out,"|RED_END");
            spc -= 8;
        }
        fprintf(out,"%*s; %s  %s\n",spc,"",l->val[i],
            i==0?l->name:"");
        g_position++;
        i++;
        if (l->val[i] == 0) break;
    }
    fprintf(out,"\n");

}

void letter_out2(void)
{
    int i;
    Char_t *l = letters;

    fprintf(out,";\n");
    fprintf(out,"; letter functions\n");
    fprintf(out,"; (W must be 0 when calling these)\n");
    fprintf(out,";\n");


    fprintf(out,"delta_%s\tequ\tdots_%s-table\n",
        l->name, l->name);
    l++;

    for (i=1; i<LETTER_CNT; i++, l++) {
        fprintf(out,"delta_%s\tequ\tdots_%s-dots_%s\n",
            l->name, l->name, (l-1)->name);
    }


    fprintf(out,"\n");
    fprintf(out,"\n");

    l--;

    for (i=0; i<LETTER_CNT; i++, l--) {
        fprintf(out,"l%s:\n", l->name);
        fprintf(out,"\taddlw\tdelta_%s\n",l->name);
    }

    fprintf(out,"\tgoto\tputc\n");
    fprintf(out,"\n\n");
}

void output_letters(void)
{
    int i;

    for (i=0; i<LETTER_CNT; i++) {
        letter_out1(&letters[i]);
    }
    fprintf(out,"\n\n");

    letter_out2();
}

#endif

void begin_table(void)
{
    int i;

    fprintf(out,"; number of phrases\n");
    fprintf(out,"PHRASE_CNT\tequ\t%d\n",PHRASE_CNT);
    fprintf(out,"\n");

    fprintf(out,";\n");
    fprintf(out,"; phrase jump table (MUST BE FIRST CODE IN THIS FILE)\n");
    fprintf(out,";\n");
    fprintf(out,"\n");
    fprintf(out,"jump:\n");
    fprintf(out,"\tclrf\tPCLATH\n");
    fprintf(out,"\taddwf\tPCL,f\n");
    fprintf(out,"\n");
    fprintf(out,"table:\n");

    for (i=0; i<PHRASE_CNT; i++) {
        fprintf(out,"   goto\tphrase%d\n",i);
    }
    fprintf(out,"\n");

    fprintf(out,";\n");
    fprintf(out,"; character column data table\n");
    fprintf(out,";\n");
    fprintf(out,"char_data:\n");

    g_position = 0;
}

int main()
{
    int i;
    char *filename = "swords.inc";

    out = fopen(filename,"wt");
    ASSERT(out);

    fprintf(out,"; DO NOT EDIT THIS FILE BY HAND!!!\n");
    fprintf(out,";\n");
    fprintf(out,";\n");
    fprintf(out,";\n");
    fprintf(out,"; This file was created by the spinwords.c program\n");
    fprintf(out,";\n");
    fprintf(out,"\n");

    begin_table();
    output_letters();

#if USE_MORSE
    for (i=0; i<=9; i++) {
        morse(i);
    }
#endif
    fprintf(out,"\n\n");


    for (i=0; i<PHRASE_CNT; i++) {
        phrase(&list[i], i);
    }
    fprintf(out,"\n\n");


    fclose(out);
    out=0;
    return 0;
}

This file Copyright (C) 2004 by Nathan (Acorn) Pooley
Go to Bat Blinker Development page
Go to Bat Blinker page
Go to JU Gadgets page
Go to Justice Unlimited homepage
Go to Acorn's personal webpage
Contact Acorn
See comments from others
Post your own comments
File created by do_doc at Wed Aug 4 20:17:33 2004