def generate_responses_code(self): encoder_defs = [] output_code = [] encoder_code = [] did_output = {} for msgname, msg in self.encoders: msgid = self.msg_to_id[msg] if msgid in did_output: continue s = msg did_output[msgid] = True code = (' if (__builtin_strcmp(str, "%s") == 0)\n' ' return &command_encoder_%s;\n' % (s, msgid)) if msgname is None: parser = msgproto.OutputFormat(msgid, msg) output_code.append(code) else: parser = msgproto.MessageFormat(msgid, msg) encoder_code.append(code) parsercode = self.build_parser(parser, 0) encoder_defs.append( "const struct command_encoder command_encoder_%s PROGMEM = {" " %s\n};\n" % (msgid, parsercode)) fmt = """ %s const __always_inline struct command_encoder * ctr_lookup_encoder(const char *str) { %s return NULL; } const __always_inline struct command_encoder * ctr_lookup_output(const char *str) { %s return NULL; } """ return fmt % ("".join(encoder_defs).strip(), "".join(encoder_code).strip(), "".join(output_code).strip())
def build_commands(cmd_by_id, messages_by_name, all_param_types): max_cmd_msgid = max(cmd_by_id.keys()) index = [] parsers = [] externs = {} for msgid in range(max_cmd_msgid + 1): if msgid not in cmd_by_id: index.append(" 0,") continue funcname, flags, msgname = cmd_by_id[msgid] msg = messages_by_name[msgname] externs[funcname] = 1 parsername = 'parser_%s' % (funcname, ) index.append(" &%s," % (parsername, )) parser = msgproto.MessageFormat(msgid, msg) parsercode = build_parser(parser, 1, all_param_types) parsers.append("const struct command_parser %s PROGMEM = {" " %s\n .flags=%s,\n .func=%s\n};" % (parsername, parsercode, flags, funcname)) index = "\n".join(index) externs = "\n".join([ "extern void " + funcname + "(uint32_t*);" for funcname in sorted(externs) ]) fmt = """ %s %s const struct command_parser * const command_index[] PROGMEM = { %s }; const uint8_t command_index_size PROGMEM = ARRAY_SIZE(command_index); """ return fmt % (externs, '\n'.join(parsers), index)