{"id":16762455,"url":"https://github.com/tlack/cproto2atomnif","last_synced_at":"2025-03-16T10:21:21.236Z","repository":{"id":145865541,"uuid":"332385169","full_name":"tlack/cproto2atomnif","owner":"tlack","description":"Call C functions from AtomVM (Erlang/Elixir on microcontrollers)","archived":false,"fork":false,"pushed_at":"2021-01-24T07:17:29.000Z","size":19,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-01-22T22:26:04.810Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tlack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-24T06:45:52.000Z","updated_at":"2022-08-08T17:23:13.000Z","dependencies_parsed_at":"2023-04-06T03:55:14.640Z","dependency_job_id":null,"html_url":"https://github.com/tlack/cproto2atomnif","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fcproto2atomnif","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fcproto2atomnif/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fcproto2atomnif/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fcproto2atomnif/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tlack","download_url":"https://codeload.github.com/tlack/cproto2atomnif/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243853625,"owners_count":20358452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T04:44:47.389Z","updated_at":"2025-03-16T10:21:21.188Z","avatar_url":"https://github.com/tlack.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"cproto2atomnif\n==============\n\nPlug existing C code into your [AtomVM](https://github.com/bettio/AtomVM) \napplications quickly and easily.\n\ncproto2atomnif is a Python script that reads C prototype header files and spits\nout code to use them from inside Erlang or Elixir on AtomVM.\n\nThe process of creating these AtomVM nifs is very straightforward, but there \nis a lot of boilerplate involved. This script automates that by creating the \nC source code for the AtomVM nif component.\n\nYou still need to edit the resulting code to add your specific validation\nlogic, etc.\n\n# example\n\n```\n$ cat test-library.c\nint add(int a, int b) {\n\t        return a+b;\n}\n$ cat test-library.h\nint add(int a, int b);\n$ cproto2atomnif test-library.h testlib \u003e testlib.c\n$ cat testlib.c\n\n    #include \"freertos/FreeRTOS.h\"\n    #include \"freertos/task.h\"\n    #include \u003cesp_log.h\u003e\n    #include \u003cstdlib.h\u003e\n    #include \u003cdefaultatoms.h\u003e\n    #include \u003cinterop.h\u003e\n    #include \u003cnifs.h\u003e\n    #include \u003cport.h\u003e\n    #include \u003cterm.h\u003e\n\n    static term nif_testlib_add(Context *ctx, int argc, term argv[]) {\n        printf(\"entering testlib:add/%d\", argc); \n        if (argc != 2) {\n            printf(\"testlib:add/%d - expected %d args, got %d\", 2, 2, argc);\n            return BADARG_ATOM;\n        }\n        \n        int a = (int)term_to_int(argv[0]);\n        int b = (int)term_to_int(argv[1]);\n        int func_result = add(a, b);\n       \n\tterm atom_val = term_from_int32(func_result);\n        VALIDATE_VALUE(atom_val, term_is_integer);\n        return atom_val;\n    }\n    static const struct Nif testlib_add_nif_info = \n    {\n        .base.type = NIFFunctionType,\n        .nif_ptr = nif_testlib_add\n    };\n\n    const struct Nif *testlib_get_nif(const char *nifname)\n    {\n        if (strcmp(\"testlib:add/2\", nifname) == 0) {\n            return \u0026testlib_add_nif_info;\n        }\n\n        return NULL;\n    }\n\n```\n\nNow compile and burn your firmware and scripts to the device, and the following should work:\n\n```X = testlib:add(5, 7)```\n\n# status\n\n- Does NOT support precompiler macros, or real `.h` files. You must simplify your prototypes before use!\n- Rough, barbarian-style first draft\n- Supports only `int` (of various kinds), `float`/`double`, and `char*`/`uint8_t*` (as beam binaries).\n- Very crude regular expression-based parser; should switch to `pyclanguage`\n- Would be cool to have more config options, or any config options.\n- Needs docs; see test.py for some ideas about what it supports\n\n# note on c++\n\nTo call C++ from a ESP-IDF toolkit component, which is C, the C++ code will need to declare\nthe target function with `extern \"C\"` at the site of the definition (not the prototype).\n\nFurthermore, neither C nor this tool understand class method invocation, so your\nprototypes used with cproto2atomnif should only refer to `thing_do_blah`, not `Thing.do_blah`.\n\n# motivation\n\nThere is a lot of existing Arduino code that I need to call from Erlang code\nrunning in AtomVM!\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlack%2Fcproto2atomnif","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftlack%2Fcproto2atomnif","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlack%2Fcproto2atomnif/lists"}