summaryrefslogtreecommitdiffstats
path: root/tabgen.c
blob: 18c594d2a1a690aaa2e83ce4e6b6b4d721ad2f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 *     low frequency Digital-Function-Generator
 *     Lookup table generator code
 *
 *   Copyright (C) 2007  Michael Buesch <m@bues.ch>
 *
 *   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 <stdio.h>
#include <math.h>
#include <strings.h>
#include <ctype.h>


#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 <avr/pgmspace.h>\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");
}
bues.ch cgit interface