Compare commits

...

2 Commits

Author SHA1 Message Date
Edward Emelianov
d18ab25440 example of helpcmds.in 2026-03-07 15:12:20 +03:00
Edward Emelianov
524a39b8a2 fixed for new libusefull_macros 2026-03-07 15:08:35 +03:00
4 changed files with 113 additions and 22 deletions

View File

@@ -5,3 +5,16 @@ another files used for test: use script ./run for it
Compile: gcc -lusefull_macro file.c -o file
gcc -lusefull_macro test.c hash.c -o test
file helpcmds.in should be included into proto.c as help list:
const char *helpstring =
"https://github.com/eddyem/stm32samples/tree/master/F3:F303/Multistepper build#" BUILD_NUMBER " @ " BUILD_DATE "\n"
"Format: cmd [N] - getter, cmd [N] = val - setter; N - optional index\n"
"* - command not supported yet, G - getter, S - setter\n\n"
#include "hashgen/helpcmds.in"
;
helpcmds.in format: each string should be in quotes with '\n' @ its end. First word - command, next text - help.

View File

@@ -31,23 +31,23 @@ typedef struct{
static glob_pars G = {.headerfile = "hash.h", .sourcefile = "hash.c"};
static int help = 0;
static myoption cmdlnopts[] = {
static sl_option_t cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"},
{"dict", NEED_ARG, NULL, 'd', arg_string, APTR(&G.dict), "dictionary file"},
{"header", NEED_ARG, NULL, 'H', arg_string, APTR(&G.headerfile),"output header filename"},
{"source", NEED_ARG, NULL, 'S', arg_string, APTR(&G.sourcefile),"output source filename"},
{"genfunc", NO_ARGS, NULL, 'F', arg_int, APTR(&G.genfunc), "generate function bodys"},
{"genfunc", NO_ARGS, NULL, 'F', arg_int, APTR(&G.genfunc), "generate function bodies"},
end_option
};
static void parse_args(int argc, char **argv){
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
sl_parseargs(&argc, &argv, cmdlnopts);
if(help) sl_showhelp(-1, cmdlnopts);
if(argc > 0){
red("Unused arguments:\n");
for(int i = 0; i < argc; ++i)
printf("%s ", argv[i]);
printf("\n");
showhelp(-1, cmdlnopts);
sl_showhelp(-1, cmdlnopts);
}
}
@@ -170,16 +170,13 @@ static char *fnname(const char *cmd){
static const char *fhdr =
"int parsecmd(const char *str){\n\
char cmd[CMD_MAXLEN + 1];\n\
if(!str || !*str) return RET_CMDNOTFOUND;\n\
int i = 0;\n\
while(*str > '@' && i < CMD_MAXLEN){ cmd[i++] = *str++; }\n\
cmd[i] = 0;\n\
if(*str){\n\
while(*str <= ' ') ++str;\n\
}\n\
while(*str > '@' && i < CMD_MAXLEN){ lastcmd[i++] = *str++; }\n\
lastcmd[i] = 0;\n\
while(*str && *str <= ' ') ++str;\n\
char *args = (char*) str;\n\
uint32_t h = hashf(cmd);\n\
uint32_t h = hashf(lastcmd);\n\
switch(h){\n\n"
;
static const char *ffooter =
@@ -189,30 +186,40 @@ static const char *ffooter =
}\n\n"
;
static const char *fns =
"int fn_%s(_U_ uint32_t hash, _U_ char *args) WAL; // \"%s\" (%u)\n\n"
"int fn_%s(uint32_t _U_ hash, char _U_ *args) WAL; // \"%s\" (%u)\n\n"
;
static const char *headercontent = "#ifndef _U_\n\
static const char *headercontent =
"// Generated by HASHGEN (https://github.com/eddyem/eddys_snippets/tree/master/stringHash4MCU_)\n\
// Licensed by GPLv3\n\
#pragma once\n\
#ifndef _U_\n\
#define _U_ __attribute__((__unused__))\n\
#endif\n\n\
#define CMD_MAXLEN (32)\n\n\
enum{\n\
RET_HELP = -3,\n\
RET_CMDNOTFOUND = -2,\n\
RET_WRONGCMD = -1,\n\
RET_BAD = 0,\n\
RET_GOOD = 1\n\
RET_GOOD = 0,\n\
RET_BAD = 1\n\
};\n\n\
int parsecmd(const char *cmdwargs);\n\n";
int parsecmd(const char *cmdwargs);\n\n\
extern char lastcmd[];\n\n";
static const char *sw =
" case CMD_%s:\n\
return fn_%s(h, args);\n\
break;\n";
static const char *srchdr =
"#include <stdint.h>\n\
"// Generated by HASHGEN (https://github.com/eddyem/eddys_snippets/tree/master/stringHash4MCU_)\n\
// Licensed by GPLv3\n\
#include <stdint.h>\n\
#include <stddef.h>\n\
#include \"%s\"\n\n\
#ifndef WAL\n\
#define WAL __attribute__ ((weak, alias (\"__f1\")))\n\
#endif\n\nstatic int __f1(_U_ uint32_t h, _U_ char *a){return 1;}\n\n"
#endif\n\nstatic int __f1(uint32_t _U_ h, char _U_ *a){return 1;}\n\n\
char lastcmd[CMD_MAXLEN + 1];\n\n"
;
static void build(strhash *H, int hno, int hlen){
@@ -245,16 +252,22 @@ static void build(strhash *H, int hno, int hlen){
}
fprintf(source, "%s", ffooter);
fclose(source);
fprintf(header, "\n\n");
for(int i = 0; i < hlen; ++i){
char *m = macroname(H[i].str);
fprintf(header, "#define STR_%-*s \"%s\"\n", lmax, m, H[i].str);
}
fclose(header);
}
int main(int argc, char **argv){
initial_setup();
sl_init();
parse_args(argc, argv);
if(!G.dict) ERRX("point dictionary file");
if(!G.headerfile) ERRX("point header source file");
if(!G.sourcefile) ERRX("point c source file");
mmapbuf *b = My_mmap(G.dict);
sl_mmapbuf_t *b = sl_mmap(G.dict);
if(!b) ERRX("Can't open %s", G.dict);
char *word = b->data;
strhash *H = MALLOC(strhash, ALLOCSZ);
@@ -310,6 +323,6 @@ int main(int argc, char **argv){
}
if(hno == HASHFNO) WARNX("Can't find proper hash function");
FREE(H);
My_munmap(b);
sl_munmap(b);
return 0;
}

View File

@@ -0,0 +1,61 @@
"absposN - GS absolute position (in steps, setter just changes current value)\n"
"accelN - GS accel/decel (steps/s^2)\n"
"adcN - G ADC value (N=0..3)\n"
"button[N] - G all or given (N=0..6) buttons' state\n"
"canerrcodes - G print last CAN errcodes\n"
"canfilter - GS can filters, format: bank# FIFO# mode(M/I) num0 [num1 [num2 [num3]]]\n"
"canflood - send/clear flood message: ID byte0 ... byteN\n"
"canfloodT - GS flood period (>=0ms)\n"
"canid - GS CAN ID of device\n"
"canignore - GS ignore list (max 10 IDs), negative to delete\n"
"canincrflood - send incremental flood message (ID is last for 'flood', stop by 'flood')\n"
"canpause - pause IN packets displaying\n"
"canreinit - reinit CAN\n"
"canresume - resume IN packets displaying\n"
"cansend - send data over CAN: send ID byte0 .. byteN (N<8)\n"
"canspeed - GS CAN speed (reinit if setter)\n"
"canstat - G CAN status\n"
"diagn[N] - G DIAG state of motor N (or all)\n"
"drvtypeN - GS driver type (0 - only step/dir, 1 - UART, 2 - SPI, 3 - reserved)\n"
"dumperr - dump error codes\n"
"dumpcmd - dump command codes\n"
"dumpconf - dump current configuration\n"
"dumpmotN - dump Nth motor configuration\n"
"dumpmotflags - dump motor flags' bits\n"
"dumpstates - dump motors' state codes\n"
"emstop[N] - emergency stop motor N or all\n"
"eraseflash [=N] - erase flash data storage (full or only N'th page of it)\n"
"esw[N] - G end-switches state\n"
"eswreactN - GS end-switches reaction (0 - ignore, 1 - ignore ESW1 and stop@0 only when moving negative, 2 - stop@any, 3 - stop@dir)\n"
"gotoN - GS move motor to given absolute position\n"
"gotozN - find zero position & refresh counters\n"
"gpioconfN* - GS GPIO configuration (0 - PUin, 1 - PPout, 2 - ODout), N=0..2\n"
"gpioN - GS GPIO values, N=0..2\n"
"help - print this help\n"
"maxspeedN - GS max speed (steps per sec)\n"
"maxstepsN - GS max steps (from zero ESW)\n"
"mcut - G MCU T\n"
"mcuvdd - G MCU Vdd\n"
"microstepsN - GS microsteps settings (2^0..2^9)\n"
"minspeedN - min speed (steps per sec)\n"
"motcurrentN - GS motor current (1..32 for 1/32..32/32 of max current)\n"
"motflagsN - motorN flags\n"
"motmul* - GS external multiplexer status (<0 - disable, 0..7 - enable and set address)\n"
"motno - GS motor number for next `pdn` commands\n"
"motreinit - re-init motors after configuration changed\n"
"pdnN - GS read/write TMC2209 registers over uart @ motor0\n"
"ping - echo given command back\n"
"relposN - GS relative move (get remaining)\n"
"relslowN - GS like 'relpos' but with slowest speed\n"
"reset - software reset\n"
"saveconf - save current configuration\n"
"screen* - GS screen enable (1) or disable (0)\n"
"speedlimit - G limiting speed for current microsteps setting\n"
"stateN - G motor state (0-relax, 1-accel, 2-move, 3-mvslow, 4-decel, 5-stall, 6-err)\n"
"stopN - stop motor with deceleration\n"
"time - G time from start (ms)\n"
"tmcbus* - GS TMC control bus (0 - USART, 1 - SPI)\n"
"udata* - GS data by usart in slave mode (text strings, '\\n'-terminated)\n"
"usartstatus* - GS status of USART1 (0 - off, 1 - master, 2 - slave)\n"
"vdrive - G approx voltage on Vdrive\n"
"vfive - G approx voltage on 5V bus\n"

4
stringHash4MCU_/mktestdic Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
awk '{print $1}' helpcmds.in |sed -e 's/"//' -e 's/\*//' -e 's/\[.*//' -e 's/N//' > testdic
./hashgen -d testdic -H hdr.h -S hdr.c -F