-
Notifications
You must be signed in to change notification settings - Fork 2
/
ions.c
277 lines (265 loc) · 6.26 KB
/
ions.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#define USE_TI89
#define USE_TI92PLUS
#define USE_V200
#define SAVE_SCREEN
#include <compat.h>
#include <ctype.h>
#include <graph.h>
#include <kbd.h>
#include <stdio.h>
#include <stdlib.h>
// Number added to a key when 2nd is pressed
#define KEY_2ND (4096)
// Number returned when the Home key is pressed
#define KEY_HOME (277)
// Height of one line, in pixels
#define LINE_HEIGHT (8)
// Number of lines to show
#define LINES (LCD_HEIGHT / LINE_HEIGHT + 1)
// x-position of the ion's name, in pixels
#define NAME_X (0)
// x-position of the ion's formula, in pixels
#define FORMULA_X (100)
// x-position of the ion's charge, in pixels
#define CHARGE_X (140)
// Key values returned by ngetchx(), mapped to 'a'-'z'
const short keymap[] = {
'=',
'(',
')',
',',
'/',
'|',
'7',
'8',
'9',
'*',
149, // the 'EE' ('k') key
'4',
'5',
'6',
'-',
KEY_STO,
'1',
'2',
'3',
't',
'+',
'0',
'.',
'x',
'y',
'z',
};
struct ion {
char *name;
char *formula;
char *charge;
};
const struct ion ions[] = {
{"Acetate", "C2H3O2", "-1"},
{"Aluminum", "Al", "+3"},
{"Amide", "NH2", "-1"},
{"Ammonium", "NH4", "+1"},
{"Antimony (III,V)", "Sb", "+1"},
{"Arsenate, Ortho-", "AsO4", "-3"},
{"Arsenic (III,V)", "As", "+1"},
{"Arsenite", "AsO3", "-3"},
{"Barium", "Ba", "+2"},
{"Benzoate", "C7H5O2", "-1"},
{"Beryllium", "Be", "+2"},
{"Bismuth", "Bi", "+3"},
{"Borate", "BO3", "-3"},
{"Boron", "B", "+3"},
{"Bromate", "BrO3", "-1"},
{"Bromate, Per-", "BrO4", "-1"},
{"Bromide", "Br", "-1"},
{"Bromite", "BrO2", "-1"},
{"Bromite, Hypo-", "BrO", "-1"},
{"Cadmium", "Cd", "+2"},
{"Calcium", "Ca", "+2"},
{"Carbonate", "CO3", "-2"},
{"Carbonate, Bi-", "HCO3", "-1"},
{"Cerium (III,IV)", "Ce", "+1"},
{"Cesium", "Cs", "+1"},
{"Chlorate", "ClO3", "-1"},
{"Chlorate, Per-", "ClO4", "-1"},
{"Chloride", "Cl", "-1"},
{"Chlorite", "ClO2", "-1"},
{"Chlorite, Hypo-", "ClO", "-1"},
{"Chromate", "CrO4", "-2"},
{"Chromate, Di-", "Cr2O7", "-2"},
{"Chromium (II,III)", "Cr", "+1"},
{"Citrate", "C6H5O7", "-1"},
{"Cobalt (II,III)", "Co", "+1"},
{"Copper (I,II)", "Cu", "+1"},
{"Cyanate", "OCN", "-1"},
{"Cyanate, Thio-", "SCN", "-1"},
{"Cyanide", "CN", "-1"},
{"Diammine silver (I)", "Ag(NH3)2", "+1"},
{"Dihydrogen phosphate", "H2PO4", "-1"},
{"Fluoride", "F", "-1"},
{"Formate", "CHO2", "-1"},
{"Gold (I,III)", "Au", "+1"},
{"Hexacyanoferrate(II)", "Fe(CN)6", "-4"},
{"Hexacyanoferrate(III)", "Fe(CN)6", "-3"},
{"Hydride", "H", "-1"},
{"Hydrogen", "H", "+1"},
{"Hydrogen carbonate", "HCO3", "-1"},
{"Hydrogen phosphate", "HPO4", "-2"},
{"Hydrogen sulfate", "HSO4", "-1"},
{"Hydrogen sulfite", "HSO3", "-1"},
{"Hydronium", "H3O", "+1"},
{"Hydroxide", "OH", "-1"},
{"Iodate", "IO3", "-1"},
{"Iodate, Per-", "IO4", "-1"},
{"Iodide", "I", "-1"},
{"Iodite", "IO2", "-1"},
{"Iodite, Hypo", "IO", "-1"},
{"Iron (II,III)", "Fe", "+1"},
{"Lead (II,IV)", "Pb", "+1"},
{"Lithium", "Li", "+1"},
{"Magnesium", "Mg", "+2"},
{"Manganate", "MnO4", "-2"},
{"Manganate, Per-", "MnO4", "-1"},
{"Manganese (II,IV,VII)", "Mn", "+1"},
{"Mercury (I)", "Hg2", "+2"},
{"Mercury (II)", "Hg", "+2"},
{"Molybdate", "MoO4", "-2"},
{"Molybdenum (II,III,IV,V,VI)", "Mo", "+1"},
{"Nickel", "Ni", "+2"},
{"Nitrate", "NO3", "-1"},
{"Nitride", "N", "-3"},
{"Nitrite", "NO2", "-1"},
{"Oxalate", "C2O4", "-2"},
{"Oxide", "O", "-2"},
{"Oxide, Per-", "O2", "-2"},
{"Phosphate", "PO4", "-3"},
{"Phosphate, Hypo-", "P2O6", "-4"},
{"Phosphate, Meta-", "PO3", "-1"},
{"Phosphate, Ortho-", "PO4", "-3"},
{"Phosphate, Pyro-", "P2O7", "-4"},
{"Phosphide", "P", "3-"},
{"Phosphite", "PO3", "-3"},
{"Phosphite, Hypo-", "H2PO2", "-1"},
{"Platinum (II,IV)", "Pt", "+1"},
{"Potassium", "K", "+1"},
{"Propionate", "C3H5O2", "-1"},
{"Selenide", "Se", "-2"},
{"Selenite", "SeO3", "-2"},
{"Silicate, Meta-", "SiO3", "-2"},
{"Silicate, Ortho-", "SiO4", "-2"},
{"Silver", "Ag", "+1"},
{"Sodium", "Na", "+1"},
{"Strontium", "Sr", "+2"},
{"Sulfate", "SO4", "-2"},
{"Sulfate, Bi-", "HSO4", "-1"},
{"Sulfate, Thio-", "S2O3", "-2"},
{"Sulfide", "S", "-2"},
{"Sulfide, Bi-", "HS", "-1"},
{"Sulfite", "SO3", "-2"},
{"Sulfite, Bi-", "HSO3", "-1"},
{"Tartarate", "C4H4O6", "-2"},
{"Telluride", "Te", "-2"},
{"Thallium (I,III)", "Tl", "+1"},
{"Tin (II,IV)", "Sn", "+1"},
{"Titanium (II,III,IV)", "Ti", "+1"},
{"Zinc", "Zn", "+2"},
{"Zirconium (II,III,IV)", "Zr", "+1"},
};
const int ions_length = 109;
// Indices to scroll to for each letter
const int indices[] = {
0,
8,
19,
39,
41,
41,
43,
44,
54,
60,
60,
60,
62,
70,
74,
77,
88,
88,
88,
102,
107,
107,
107,
107,
107,
107,
};
// Draws everything on the line specified and below; line is zero indexed
void scroll(int line) {
clrscr();
int i;
FontSetSys(F_4x6);
for (i = 0; i < LINES; ++i) {
if (line + i < 0 || ions_length <= line + i) { // off the screen
continue;
} else {
DrawStr(NAME_X, LINE_HEIGHT * i, ions[line + i].name, A_NORMAL);
DrawStr(FORMULA_X, LINE_HEIGHT * i, ions[line + i].formula, A_NORMAL);
DrawStr(CHARGE_X, LINE_HEIGHT * i, ions[line + i].charge, A_NORMAL);
}
}
}
// Keep line on the screen
inline int clamp(int line) {
return min(ions_length - LINES + 1, max(line, 0));
}
// Returns the
int key_index(short key) {
if (isalpha(key)) {
return indices[tolower(key) - 'a'];
}
int i;
for (i = 0; i < 26; ++i) {
if (key == keymap[i] ||
key - KEY_2ND == keymap[i] ||
key - KEY_DIAMOND == keymap[i]) {
return indices[i];
}
}
return -1;
}
void _main() {
FontSetSys(F_4x6);
int line;
scroll(line = 0);
for (;;) {
short key = ngetchx();
if (key == KEY_ESC || key == KEY_HOME || key - KEY_2ND == KEY_ESC) {
exit(0);
} else if (key == KEY_UP) {
scroll(line = clamp(--line)); // up one
continue;
} else if (key - KEY_2ND == KEY_UP) {
scroll(line = clamp(line -= 5)); // up five
continue;
} else if (key - KEY_DIAMOND == KEY_UP) {
scroll(line = clamp(line = 0)); // top
} else if (key == KEY_DOWN) {
scroll(line = clamp(++line)); // down one
continue;
} else if (key - KEY_2ND == KEY_DOWN) {
scroll(line = clamp(line += 5)); // down five
continue;
} else if (key - KEY_DIAMOND == KEY_DOWN) {
scroll(line = clamp(line = ions_length)); // bottom
}
// handle alphabetic keypresses
int index;
if ((index = key_index(key)) >= 0) {
scroll(line = clamp(index));
}
}
}