mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-05-24 13:55:51 +08:00
Example of CAML call from C
This commit is contained in:
@@ -26,6 +26,7 @@ INCLUDES= -I +xml-light -I +pcre -I +netstring
|
||||
XINCLUDES= -I +lablgl -I +lablgtk2 -I +xml-light
|
||||
OCAMLC=ocamlc
|
||||
OCAMLOPT=ocamlopt
|
||||
OCAMLLIBDIR=$(shell ocamlc -where)
|
||||
|
||||
|
||||
SRC = debug.ml base64.ml serial.ml ocaml_tools.ml extXml.ml env.ml xml2h.ml latlong.ml srtm.ml http.ml gm.ml iGN.ml geometry_2d.ml cserial.o convert.o ubx.ml pprz.ml xbee.ml xmlCom.ml editAirframe.ml
|
||||
@@ -77,6 +78,12 @@ ml_gtkgl_hack.o : ml_gtkgl_hack.c
|
||||
@echo OC $<
|
||||
$(Q)$(OCAMLC) $(INCLUDES) -c -ccopt "$(GTKCFLAGS)" $<
|
||||
|
||||
camltm.o : register_example.cmo
|
||||
$(OCAMLC) $(INCLUDES) -output-obj -o $@ unix.cma str.cma xml-light.cma ivy-ocaml.cma debug.cmo serial.cmo extXml.cmo env.cmo pprz.cmo tm.cmo
|
||||
|
||||
caml_from_c_example : cserial.o convert.o caml_from_c_example.o camltm.o
|
||||
$(CC) -o $@ $^ -L$(OCAMLLIBDIR) -lunix -lstr -livy-ocaml -lcamlrun -lm -livy -lcurses
|
||||
|
||||
%.cmo : %.ml
|
||||
@echo OC $<
|
||||
$(Q)$(OCAMLC) $(INCLUDES) -c $<
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <caml/mlvalues.h>
|
||||
#include <caml/callback.h>
|
||||
#include <caml/memory.h>
|
||||
#include <caml/alloc.h>
|
||||
#include <caml/custom.h>
|
||||
|
||||
|
||||
void print_ascii_to_binary(int n, char* msg)
|
||||
{
|
||||
static value * caml_to_binary_closure = NULL;
|
||||
if (caml_to_binary_closure == NULL)
|
||||
caml_to_binary_closure = caml_named_value("to_binary");
|
||||
|
||||
value s = caml_callback2(*caml_to_binary_closure, Val_int(n), caml_copy_string(msg));
|
||||
|
||||
int i;
|
||||
for(i = 0; i < string_length(s); i++)
|
||||
printf("%02x ", Byte_u(s, i));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
caml_startup(argv);
|
||||
|
||||
print_ascii_to_binary(42, "ATTITUDE 7 22 33");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
module Tm_Pprz = Pprz.Messages (struct let name = "telemetry" end)
|
||||
|
||||
let to_binary = fun ac_id m ->
|
||||
let msg_id, vs = Tm_Pprz.values_of_string m in
|
||||
let payload = Tm_Pprz.payload_of_values msg_id ac_id vs in
|
||||
let buf = Pprz.Transport.packet payload in
|
||||
Printf.printf "From caml: %s\n%!" (Debug.xprint buf);
|
||||
buf
|
||||
|
||||
let _ = Callback.register "to_binary" to_binary
|
||||
Reference in New Issue
Block a user