Skip to content

Commit 7ebbb30

Browse files
committed
hal: Update matrix keyboard and lcd components to become 64-bit clean
1 parent e1c6a55 commit 7ebbb30

2 files changed

Lines changed: 87 additions & 54 deletions

File tree

‎src/hal/components/lcd.c‎

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MODULE_DESCRIPTION("Hal-to-text component for Mesa 7I73 and similar");
3636
MODULE_LICENSE("GPL");
3737

3838
#define MAX_CHAN 16
39-
#define MAX_ENTRY 20
39+
#define MAX_ENTRY 22 // Octal of 64-bit is 22 digits long
4040

4141
typedef struct {
4242
hal_refs_u *args;
@@ -47,7 +47,7 @@ typedef struct {
4747

4848
typedef struct {
4949
lcd_page_t *pages;
50-
unsigned num_pages;
50+
rtapi_uint num_pages;
5151
hal_uint_t page_num;
5252
hal_uint_t out;
5353
hal_real_t contrast;
@@ -104,16 +104,20 @@ int rtapi_app_main(void){
104104

105105
// allocate shared memory for data
106106
lcd = hal_malloc(sizeof(lcd_t));
107-
if (lcd == 0) {
108-
rtapi_print_msg(RTAPI_MSG_ERR,
109-
"lcd component: Out of Memory\n");
107+
if (NULL == lcd) {
108+
rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n");
110109
hal_exit(comp_id);
111110
return -1;
112111
}
113112

114113
// Count the instances. Very unlikely to be more than one, but...
115114
for (lcd->num_insts = 0; fmt_strings[lcd->num_insts];lcd->num_insts++){}
116115
lcd->insts = hal_malloc(lcd->num_insts * sizeof(lcd_inst_t));
116+
if (NULL == lcd->insts) {
117+
rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n");
118+
hal_exit(comp_id);
119+
return -1;
120+
}
117121

118122
for (i = 0; i < lcd->num_insts; i++){
119123
lcd_inst_t *inst = &lcd->insts[i];
@@ -124,6 +128,11 @@ int rtapi_app_main(void){
124128
if (fmt_strings[i][f] =='|') inst->num_pages++;
125129
}
126130
inst->pages = hal_malloc(inst->num_pages * sizeof(lcd_page_t));
131+
if (NULL == inst->pages) {
132+
rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n");
133+
hal_exit(comp_id);
134+
return -1;
135+
}
127136

128137
//second pass
129138
f1 = k = p = 0;
@@ -143,6 +152,11 @@ int rtapi_app_main(void){
143152
inst->pages[p].length = f - f1 + 2;
144153
inst->pages[p].args = hal_malloc(inst->pages[p].num_args
145154
* sizeof(*inst->pages[p].args));
155+
if (NULL == inst->pages[p].fmt || NULL == inst->pages[p].args) {
156+
rtapi_print_msg(RTAPI_MSG_ERR, "lcd: Out of Memory\n");
157+
hal_exit(comp_id);
158+
return -1;
159+
}
146160
f1 = f + 2;
147161
{
148162
int a = -1, s = -1;
@@ -165,7 +179,7 @@ int rtapi_app_main(void){
165179
break;
166180
case 'u':
167181
case 'c':
168-
retval = hal_pin_new_ui32(comp_id, HAL_IN,
182+
retval = hal_pin_new_uint(comp_id, HAL_IN,
169183
&(inst->pages[p].args[a].u), 0,
170184
"lcd.%02i.page.%02i.arg.%02i",
171185
i, p, a);
@@ -175,7 +189,7 @@ int rtapi_app_main(void){
175189

176190
break;
177191
case 's':
178-
retval = hal_pin_new_si32(comp_id, HAL_IN,
192+
retval = hal_pin_new_sint(comp_id, HAL_IN,
179193
&(inst->pages[p].args[a].s), 0,
180194
"lcd.%02i.page.%02i.arg.%02i",
181195
i, p, a);
@@ -207,13 +221,13 @@ int rtapi_app_main(void){
207221
}
208222

209223
for (i = 0; i < lcd->num_insts; i++){
210-
retval = hal_pin_new_ui32(comp_id, HAL_IN, &(lcd->insts[i].page_num), 0,
224+
retval = hal_pin_new_uint(comp_id, HAL_IN, &(lcd->insts[i].page_num), 0,
211225
"lcd.%02i.page_num", i);
212226
if (retval != 0) {
213227
return retval;
214228
}
215229
lcd->insts[i].last_page = -1; // force screen refresh
216-
retval = hal_pin_new_ui32(comp_id, HAL_OUT, &(lcd->insts[i].out), 0,
230+
retval = hal_pin_new_uint(comp_id, HAL_OUT, &(lcd->insts[i].out), 0,
217231
"lcd.%02i.out",i);
218232
if (retval != 0) {
219233
return retval;
@@ -223,7 +237,7 @@ int rtapi_app_main(void){
223237
if (retval != 0) {
224238
return retval;
225239
}
226-
retval = hal_param_new_ui32(comp_id, HAL_RW, &(lcd->insts[i].dp), '.',
240+
retval = hal_param_new_uint(comp_id, HAL_RW, &(lcd->insts[i].dp), '.',
227241
"lcd.%02i.decimal-separator",i);
228242
if (retval != 0) {
229243
return retval;
@@ -276,18 +290,18 @@ static void write_one(lcd_inst_t *inst){
276290
return;}
277291

278292
if (inst->buff[inst->c_ptr] != 0){
279-
hal_set_ui32(inst->out, inst->buff[inst->c_ptr++]);
293+
hal_set_uint(inst->out, inst->buff[inst->c_ptr++]);
280294
return;
281295
}
282296

283297
inst->c_ptr = 0;
284298
inst->buff[0] = 0;
285299

286-
if (hal_get_ui32(inst->page_num) >= inst->num_pages) return; // should this error?
300+
if (hal_get_uint(inst->page_num) >= inst->num_pages) return; // should this error?
287301

288-
if (hal_get_ui32(inst->page_num) != inst->last_page){
289-
inst->last_page = hal_get_ui32(inst->page_num);
290-
hal_set_ui32(inst->out, 0x11); //cursor off
302+
if (hal_get_uint(inst->page_num) != inst->last_page){
303+
inst->last_page = hal_get_uint(inst->page_num);
304+
hal_set_uint(inst->out, 0x11); //cursor off
291305
inst->buff[0] = 0x1E; //cursor home
292306
inst->buff[1] = 0x1A; //clear screen
293307
inst->buff[2] = 0; // end
@@ -297,8 +311,8 @@ static void write_one(lcd_inst_t *inst){
297311
return;
298312
}
299313

300-
if (inst->f_ptr >= inst->pages[hal_get_ui32(inst->page_num)].length){
301-
hal_set_ui32(inst->out, 0x1B); // ESC
314+
if (inst->f_ptr >= inst->pages[hal_get_uint(inst->page_num)].length){
315+
hal_set_uint(inst->out, 0x1B); // ESC
302316
inst->buff[0] = 0x3D; // =
303317
inst->buff[1] = 0x20; // Line 0
304318
inst->buff[2] = 0x20; // Column 0
@@ -314,17 +328,17 @@ static void write_one(lcd_inst_t *inst){
314328
if (c > 0xBF) c = 0xBF;
315329
if (c < 0x20) c = 0x20;
316330
inst->last_contrast = hal_get_real(inst->contrast);
317-
hal_set_ui32(inst->out, 0x1B);
331+
hal_set_uint(inst->out, 0x1B);
318332
inst->buff[0] = 'C';
319333
inst->buff[1] = c;
320334
inst->buff[2] = 0;
321335
inst->c_ptr = 0;
322336
return;
323337
}
324338

325-
switch (inst->pages[hal_get_ui32(inst->page_num)].fmt[inst->f_ptr]){
339+
switch (inst->pages[hal_get_uint(inst->page_num)].fmt[inst->f_ptr]){
326340
case '\\': //escape chars
327-
c1 = inst->pages[hal_get_ui32(inst->page_num)].fmt[++inst->f_ptr];
341+
c1 = inst->pages[hal_get_uint(inst->page_num)].fmt[++inst->f_ptr];
328342
switch (c1){
329343
case 'n':
330344
case 'N':
@@ -350,7 +364,7 @@ static void write_one(lcd_inst_t *inst){
350364
/* Fallthrough */
351365

352366
default: //check for hex
353-
c2 = inst->pages[hal_get_ui32(inst->page_num)].fmt[++inst->f_ptr];
367+
c2 = inst->pages[hal_get_uint(inst->page_num)].fmt[++inst->f_ptr];
354368
inst->f_ptr++;
355369
if (c1 > '9') c1 &= 0xDF; //upper case
356370
if (c2 > '9') c2 &= 0xDF;
@@ -362,32 +376,32 @@ static void write_one(lcd_inst_t *inst){
362376
inst->buff[0] = 0;
363377
}
364378
}
365-
hal_set_ui32(inst->out, inst->buff[0]);
379+
hal_set_uint(inst->out, inst->buff[0]);
366380
inst->c_ptr = 1;
367381
return;
368382
case '%':
369-
retval = parse_fmt(inst->pages[hal_get_ui32(inst->page_num)].fmt,
383+
retval = parse_fmt(inst->pages[hal_get_uint(inst->page_num)].fmt,
370384
&inst->f_ptr,
371385
inst->buff,
372-
&(inst->pages[hal_get_ui32(inst->page_num)].args[inst->a_ptr++]),
373-
(char)hal_get_ui32(inst->dp));
386+
&(inst->pages[hal_get_uint(inst->page_num)].args[inst->a_ptr++]),
387+
(char)hal_get_uint(inst->dp));
374388
if (retval >= 0) {
375-
hal_set_ui32(inst->out, inst->buff[0]);
389+
hal_set_uint(inst->out, inst->buff[0]);
376390
inst->c_ptr = 1;
377391
inst->f_ptr++;
378392
return;
379393
}
380394
/* Fallthrough */
381395
default:
382-
hal_set_ui32(inst->out, inst->pages[hal_get_ui32(inst->page_num)].fmt[inst->f_ptr++]);
396+
hal_set_uint(inst->out, inst->pages[hal_get_uint(inst->page_num)].fmt[inst->f_ptr++]);
383397
}
384398
}
385399

386-
int num_digits_baseN(rtapi_u64 val, int base){
400+
static int num_digits_baseN(rtapi_u64 val, int base){
387401
int n = 1;
388-
rtapi_u64 m = 1;
389-
while ((m *= base) <= val){
390-
n += 1;
402+
while(val >= (rtapi_u64)base) {
403+
val /= base;
404+
n++;
391405
}
392406
return n;
393407
}
@@ -473,9 +487,9 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){
473487
case 'u':
474488
if (out == NULL || val == NULL) return 'u';
475489
{
476-
unsigned int tmp;
490+
rtapi_uint tmp;
477491
int i;
478-
unsigned int v = hal_get_ui32(val->u);
492+
rtapi_uint v = hal_get_uint(val->u);
479493

480494
if (c < 1) c = num_digits_baseN(v, base);
481495

@@ -501,16 +515,17 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){
501515
case 'i':
502516
if (out == NULL || val == NULL) return 's';
503517
{
504-
int tmp, s = 0;
518+
rtapi_sint tmp;
519+
int s = 0;
505520
int i;
506-
int v = hal_get_si32(val->s);
521+
rtapi_sint v = hal_get_sint(val->s);
507522

508523
if (sgn == '+') s = 1;
509524
if (v < 0) {s = 1; sgn = '-'; v = -v;}
510525

511526
if (c < 1) c = num_digits_baseN(v, base) + s;
512527

513-
tmp = abs(v);
528+
tmp = v < 0 ? -v : v;
514529
for (i = c - 1; i >= s; i--){
515530
if (tmp != 0 || i == c - 1){
516531
out[i] = digits[tmp % 10];
@@ -550,6 +565,10 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){
550565
if (v < 0) {s = 1; sgn = '-'; v = -v;}
551566

552567
if (m < 0) m = 4;
568+
if (m >= (int)(sizeof(pow10) / sizeof(*pow10))) {
569+
tmp = 2;
570+
goto overflow;
571+
}
553572

554573
if (c < 1) c = num_digits_baseN(v, base) + m + 1 + s;
555574

@@ -607,7 +626,7 @@ static int parse_fmt(char *in, int *ptr, char *out, hal_refs_u *val, char dp){
607626
if (out == NULL || val == NULL) return 'c';
608627
{
609628
int i;
610-
unsigned char v = hal_get_ui32(val->u);
629+
unsigned char v = hal_get_uint(val->u) & 0xff;
611630

612631
if (c == 0) c = 1;
613632
for (i = 0; i < c; i++){

0 commit comments

Comments
 (0)