Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript #139

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions carbon/hal/atmegau2.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ int carbon_select_u2_gpio(struct _lf_device *device) {
struct _carbon_context *context = device->_ctx;
lf_assert(context, failure, E_NULL, "No context for selected carbon device.");
struct _lf_device *u2 = context->_u2;
LF_MODULE_SET_DEVICE_AND_ID(_gpio, u2, _gpio_id);
_gpio.index = _gpio_id;
return lf_success;
failure:
return lf_error;
}

int carbon_select_atmegau2(struct _lf_device *device) {
LF_MODULE_SET_DEVICE_AND_ID(_button, device, _button_id);
// LF_MODULE_SET_DEVICE_AND_ID(_gpio, device, _gpio_id);
LF_MODULE_SET_DEVICE_AND_ID(_led, device, _led_id);
LF_MODULE_SET_DEVICE_AND_ID(_uart0, device, _uart0_id);
LF_MODULE_SET_DEVICE_AND_ID(_wdt, device, _wdt_id);
_button.index = _button_id;;
// _gpio.index = _gpio_id;;
_led.index = _led_id;;
_uart0.index = _uart0_id;;
_wdt.index = _wdt_id;;
return lf_success;
}
36 changes: 18 additions & 18 deletions carbon/hal/atsam4s.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
#include <flipper/atsam4s/modules.h>

int carbon_select_atsam4s(struct _lf_device *device) {
LF_MODULE_SET_DEVICE_AND_ID(_adc, device, _adc_id);
LF_MODULE_SET_DEVICE_AND_ID(_button, device, _button_id);
LF_MODULE_SET_DEVICE_AND_ID(_dac, device, _dac_id);
LF_MODULE_SET_DEVICE_AND_ID(_fld, device, _fld_id);
LF_MODULE_SET_DEVICE_AND_ID(_gpio, device, _gpio_id);
LF_MODULE_SET_DEVICE_AND_ID(_i2c, device, _i2c_id);
LF_MODULE_SET_DEVICE_AND_ID(_led, device, _led_id);
LF_MODULE_SET_DEVICE_AND_ID(_pwm, device, _pwm_id);
LF_MODULE_SET_DEVICE_AND_ID(_rtc, device, _rtc_id);
LF_MODULE_SET_DEVICE_AND_ID(_spi, device, _spi_id);
LF_MODULE_SET_DEVICE_AND_ID(_swd, device, _swd_id);
LF_MODULE_SET_DEVICE_AND_ID(_task, device, _task_id);
LF_MODULE_SET_DEVICE_AND_ID(_temp, device, _temp_id);
LF_MODULE_SET_DEVICE_AND_ID(_timer, device, _timer_id);
LF_MODULE_SET_DEVICE_AND_ID(_uart0, device, _uart0_id);
LF_MODULE_SET_DEVICE_AND_ID(_usart, device, _usart_id);
LF_MODULE_SET_DEVICE_AND_ID(_usb, device, _usb_id);
LF_MODULE_SET_DEVICE_AND_ID(_wdt, device, _wdt_id);
_adc.index = _adc_id;
_button.index = _button_id;
_dac.index = _dac_id;
_fld.index = _fld_id;
_gpio.index = _gpio_id;
_i2c.index = _i2c_id;
_led.index = _led_id;
_pwm.index = _pwm_id;
_rtc.index = _rtc_id;
_spi.index = _spi_id;
_swd.index = _swd_id;
_task.index = _task_id;
_temp.index = _temp_id;
_timer.index = _timer_id;
_uart0.index = _uart0_id;
_usart.index = _usart_id;
_usb.index = _usb_id;
_wdt.index = _wdt_id;
return lf_success;
}
2 changes: 1 addition & 1 deletion console/src/bin/modules_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub mod led {
let blue = args.value_of("blue").unwrap().parse::<u8>().unwrap();

let flipper = flipper::Flipper::attach();
let led = flipper::fsm::led::Led::bind(&flipper);
let led = flipper::fsm::led::Led::new(&flipper);
led.rgb(red, green, blue);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion console/src/bindings/generators/templates/c.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const struct _{{name}}_interface {{name}} {
};
{{#each funcs}}
LF_WEAK {{ret}} {{../name}}_{{name}}({{param_expansion params}}) {
{{ret}} result = lf_invoke(&_{{../name}}, _{{../name}}_{{name}}, {{ret_fmr}}, lf_args({{fmr_expansion params}}));
{{ret}} result = lf_invoke(lf_get_current_device(), &_{{../name}}, _{{../name}}_{{name}}, {{ret_fmr}}, lf_args({{fmr_expansion params}}));
}
{{/each}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const struct _user_interface user {
};

LF_WEAK int user_test(int a, char b, long int c) {
int result = lf_invoke(&_user, _user_test, lf_uint32_t, lf_args(lf_infer(a), lf_infer(b), lf_infer(c)));
int result = lf_invoke(lf_get_current_device(), &_user, _user_test, lf_uint32_t, lf_args(lf_infer(a), lf_infer(b), lf_infer(c)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const struct _user_interface user {
};

LF_WEAK char* user_test_four(uint8_t first, uint16_t second, uint32_t third) {
char* result = lf_invoke(&_user, _user_test_four, lf_uint8_t, lf_args(lf_infer(first), lf_infer(second), lf_infer(third)));
char* result = lf_invoke(lf_get_current_device(), &_user, _user_test_four, lf_uint8_t, lf_args(lf_infer(first), lf_infer(second), lf_infer(third)));
}

LF_WEAK int user_test_three(char letter) {
int result = lf_invoke(&_user, _user_test_three, lf_uint32_t, lf_args(lf_infer(letter)));
int result = lf_invoke(lf_get_current_device(), &_user, _user_test_three, lf_uint32_t, lf_args(lf_infer(letter)));
}

LF_WEAK void user_test_one() {
void result = lf_invoke(&_user, _user_test_one, lf_void_t, lf_args());
void result = lf_invoke(lf_get_current_device(), &_user, _user_test_one, lf_void_t, lf_args());
}
4 changes: 2 additions & 2 deletions languages/java/src/main/java/io/flipper/Flipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface _libflipper {
Pointer flipper_attach();

// FMR bindings
int lf_invoke(_lf_module module, byte function, Pointer parameters);
int lf_invoke(lf_get_current_device(), _lf_module module, byte function, Pointer parameters);
int lf_bind(Pointer module);

Pointer fmr_build(byte argc);
Expand Down Expand Up @@ -55,7 +55,7 @@ public <T> T bindModule(Class<T> moduleInterface, String name) {
* Upon receiving an invoke call, the ModuleInvocationHandler constructs a _fmr_list parameter list from the
* function call and uses this FMRInvoker to deliver it back to us, where we can pass it to lf_invoke.
*/
ModuleInvocationHandler invoker = new ModuleInvocationHandler(moduleInterface, (func, params) -> libflipper.lf_invoke(module, func, params));
ModuleInvocationHandler invoker = new ModuleInvocationHandler(moduleInterface, (func, params) -> libflipper.lf_invoke(lf_get_current_device(), module, func, params));

return moduleInterface.cast(Proxy.newProxyInstance(moduleInterface.getClassLoader(), new Class[] { moduleInterface }, invoker));
}
Expand Down
1 change: 1 addition & 0 deletions languages/js/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.idea/
*.iml
yarn.lock
151 changes: 151 additions & 0 deletions languages/js/flipper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const ffi = require('ffi');
const ref = require('ref');
const Struct = require('ref-struct');

// typedefs
const lf_version_t = ref.types.uint16;
const lf_crc_t = ref.types.uint16;

const lf_value = ref.types.uint32;
const lf_function = ref.types.uint8;
const lf_return_t = ref.types.uint32;

const _lf_type = [
ref.types.uint8,
ref.types.uint16,
ref.types.void,
ref.types.uint32,
];

const lf_type = ref.types.uint8;

const _lf_module = Struct({
'name': 'string',
'description': 'string',
'version': lf_version_t,
'identifier': lf_crc_t,
'index': ref.types.uint32,
'data': 'pointer',
'size': ref.refType(ref.types.uint32),
});

const _lf_arg = Struct({
'arg_type': lf_type,
'arg_value': lf_value,
});

const libflipper = ffi.Library('libflipper', {

// _lf_device *flipper_attach()
'flipper_attach': [ 'pointer', [ ] ],

// lf_return_t lf_invoke(void *device, void *module, lf_function function, lf_type type, void *parameters)
'lf_invoke': [ lf_return_t, [ 'pointer', 'pointer', lf_function, lf_type, 'pointer' ] ],

// int lf_bind(void *device, struct _lf_module *module)
'lf_bind': [ 'int', [ 'pointer', ref.refType(_lf_module) ] ],

// int lf_ll_append(struct _lf_ll *list, struct _lf_arg *arg, void *destructor)
'lf_ll_append': [ ref.types.void, [ 'pointer', ref.refType(_lf_arg), 'pointer' ] ],

// int lf_ll_release(struct _lf_ll **list)
'lf_ll_release': [ ref.types.void, [ 'pointer' ] ],
});

const create_arglist = function(types, args) {
let list_handle = ref.NULL_POINTER;
for(let i = 0; i < args.length; i++) {
const arg = new _lf_arg({
'arg_type': _lf_type.indexOf(types[i]),
'arg_value': args[i],
});
libflipper.lf_ll_append(list_handle, arg.ref(), ref.NULL_POINTER);
}
return list_handle;
};

function Flipper ( ) {
this.device = libflipper.flipper_attach();
}

/**
* Given an interface declaration and a module name, return a module object with
* function implementations for controlling Flipper.
*
* The interface declaration must be given node-ffi style as follows:
*
* ```js
* const my_module_interface = {
* 'my_module_function_name': [ RETURN TYPE, [ PARAMETER TYPES ] ],
* };
* ```
*
* The types must be represented using the "ref" library, which allows you to express
* types from C using javascript.
*
* As an example, the following will bind a module definition for the LED:
*
* ```js
* // Provides representations of C types.
* const ref = require('ref');
* const Flipper = require('flipper');
*
* const led_interface = {
* 'rgb': [ ref.types.void, [ ref.types.uint8, ref.types.uint8, ref.types.uint8 ] ],
* };
*
* const my_flipper = new Flipper();
* const led_module_object = my_flipper.bind(led_interface, "led");
*
* // Now you can execute module functions using the module object
* led_module_object.rgb(0, 10, 0); // Sets the LED to greeen
* ```
*
* @param iface An object describing the interface of the module to bind to.
* @param name The name of the module loaded on Flipper that we're binding to.
*/
Flipper.prototype.bind = function(iface, name) {

// Hold a reference to this flipper for inner functions to use.
const self = this;
const module = new _lf_module({
'name': name,
'description': '',
'version': 0,
'identifier': 0,
'index': 0,
'data': ref.types.NULL_POINTER,
'size': ref.types.NULL_POINTER,
});

// Populates the above module object with metadata.
libflipper.lf_bind(self.device, module.ref());

const bindings = {};

// For each function in the given interface, generate a proxy for it and attach it to the bindings.
Object.keys(iface || {}).forEach(function (func, i) {

// Get interface metadata for the current function definition.
const info = iface[func];
const resultType = info[0];
const paramTypes = info[1];

// Generate a proxy function and assign it to the bindings.
bindings[func] = function() {
if (arguments.length !== paramTypes.length) {
console.log("Expected " + paramTypes.length + " args, got " + arguments.length);
}

const params = create_arglist(paramTypes, arguments);
const ret = _lf_type.indexOf(resultType);
const result = libflipper.lf_invoke(self.device, module.ref(), i, ret, params);
libflipper.lf_ll_release(params.ref());
return result;
}
});

return bindings;
};

module.exports = Flipper;
Loading