/* * low frequency Digital-Function-Generator * Lookup table generator code * * Copyright (C) 2007 Michael Buesch * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #undef M_PI #define M_PI 3.14159265358979323846264338327 /* Make sure buf is long enough! */ static void toupper_str(char *buf, const char *str) { while (*str) { *buf = toupper(*str); buf++; str++; } *buf = 0; } static void generate_sine_table(void) { double i; double step; double s; unsigned int val; int cnt = 0; printf("/* Sine function table */\n"); printf("extern const uint8_t PROGMEM table_sine[];\n"); printf("const uint8_t PROGMEM table_sine[] = {"); /* sin() takes radians as arg. 2pi rad == 360deg */ step = (M_PI * 2) / (0xFF + 1); for (i = 0; i < (M_PI * 2) - step; i += step) { s = sin(i); s += 1; s *= 0xFF / 2; val = round(s); if (cnt % 8 == 0) printf("\n\t0x%02X,", val & 0xFF); else printf(" 0x%02X,", val & 0xFF); cnt++; } printf("\n};\n"); } static void generate_triangle_table(void) { int i; unsigned int val; printf("/* Triangle function table */\n"); printf("extern const uint8_t PROGMEM table_triang[];\n"); printf("const uint8_t PROGMEM table_triang[] = {"); for (i = 0, val = 0; i < 0xFF + 1; i++) { if (i % 8 == 0) printf("\n\t0x%02X,", val & 0xFF); else printf(" 0x%02X,", val & 0xFF); if (val == 0xFE) { val++; } else { if (i < 128) val += 2; else val -= 2; } } printf("\n};\n"); } static void gen_chartab(const char *name, const char *template) { int i, column; unsigned int val; char upper_name[128]; static unsigned int char_ascii = 0; toupper_str(upper_name, name); printf("/* Character table: %s */\n", upper_name); printf("extern const uint8_t PROGMEM table_char_%s[];\n", name); printf("const uint8_t PROGMEM table_char_%s[] = {\n\t", name); column = 0; val = 0; for (i = 0; i < 40; i++) { if (template[i] != ' ') val |= (1 << (4 - column)); column++; if (column == 5) { printf("0x%02X, ", val & 0xFF); column = 0; val = 0; } } printf("\n};\n"); printf("#define ASCII_%s %u\n\n", upper_name, char_ascii); char_ascii++; } static void generate_char_tables(void) { const char *t; t = "# " " # " " # " " # " " # " "# " "# " " # "; gen_chartab("sine", t); t = "# " "#####" " #" " #" " #" "#####" "# " "# "; gen_chartab("square", t); t = "# " " # " " # " " # " " # " " # " "# " " "; gen_chartab("tri", t); t = "# " " # " " # " " # " "#####" " # " " # " " # "; gen_chartab("saw", t); } int main(int argc, char **argv) { printf("/******************************************\n"); printf(" ** THIS FILE IS GENERATED. DO NOT EDIT! **\n"); printf(" ******************************************/\n"); printf("\n"); printf("#include \n"); printf("\n"); printf("\n"); generate_sine_table(); printf("\n\n\n"); generate_triangle_table(); printf("\n\n\n"); generate_char_tables(); printf("\n\n\n"); }