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

Experimental rust support #199

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
91 changes: 91 additions & 0 deletions templates/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{% import 'macros.jinja2' as utils %}
{{ utils.pad_string('//! ', utils.license(info.copyright.name, info.copyright.date, info.license.name)) -}}
//!
//! This driver was built using [`embedded-hal`] traits.
//! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.2
//!
//! Auto-generated file for {{ info.title }} v{{ info.version }}.
//! Generated from {{ fileName }} using Cyanobyte Codegen v{{ version }}
//!
//! {{info.title}}
//!
//! {{info.description}}

#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]

extern create embedded_hal as hal;

use hal::blocking::i2c::{Write, WriteRead};

enum Register {
{% for key,register in registers|dictsort %}
{{key.upper()}} = {{register.address}},
{% endfor %}
}

{% if i2c.address is iterable and i2c.address is not string %}
pub enum DeviceAddress {
{% for address in i2c.address %}
I2C_ADDRESS_{{address}} = {{address}},
{% endfor %}
}
{% endif %}

{# Create enums for fields #}
{% if fields %}
{% for key,field in fields|dictsort %}
{% if field.enum %}
{# Create enum class #}
pub enum {{key[0].upper()}}{{key[1:]}} {
{% for ekey,enum in field.enum|dictsort %}
{{ekey.upper()}} = {{enum.value}} /// {{enum.title}}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}

/// {{info.title}} driver
pub struct {{info.title}}<I2C, u8> {
i2c: I2C,
address: u8
}

// TODO Endian, swap sign

impl<I2C, E> {{info.title}}<I2C, u8>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
{
/// Creates a new driver from an I2C peripheral
{% if i2c.address is iterable and i2c.address is not string %}
pub fn new(i2c: I2C, addr: u8) -> Result<Self, E> {
let mut {{info.title.lower()}} = {{info.title}} { i2c, addr };
{% else %}
pub fn new(i2c: I2C) -> Result<Self, E> {
let mut {{info.title.lower()}} = {{info.title}} { i2c, {{i2c.address}} };
{% endif %}
{% if '_lifecycle' in functions and 'Begin' in functions._lifecycle.computed %}
{{info.title.lower()}}._lifecycle_begin()?;
{% endif %}

Ok({{info.title.lower()}})
}

{% for key,register in registers|dictsort %}
{% if (not 'readWrite' in register) or ('readWrite' in register and 'R' is in(register.readWrite)) %}
// TODO u8 or other num types
fn read_{{key.lower() | camel_to_snake }}(&mut self) -> Result<u8, E> {
let buffer: &mut [u8] = &mut buffer;
self.i2c.write_read(
self.address,
&[register.address],
buffer
)?;
Ok(buffer[0])
}
{% endif %}

{% endfor %}
}
116 changes: 116 additions & 0 deletions test/sampleData/rust/Example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! Copyright (C) 2019 Google Inc.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! you may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//! http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.
//!
//! This driver was built using [`embedded-hal`] traits.
//! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.2
//!
//! Auto-generated file for Example v0.1.0.
//! Generated from peripherals/example.yaml using Cyanobyte Codegen v0.1.0
//!
//! Example
//!
//! Example of a package

#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]

extern create embedded_hal as hal;

use hal::blocking::i2c::{Write, WriteRead};

enum Register {
REGISTERA = 0,
REGISTERB = 1,
REGISTERC = 2,
REGISTERD = 3,
}

pub enum DeviceAddress {
I2C_ADDRESS_16 = 16,
I2C_ADDRESS_32 = 32,
I2C_ADDRESS_48 = 48,
}

pub enum FieldB {
VAL_1 = 1 /// Value 1
VAL_2 = 2 /// Value 2
VAL_3 = 4 /// Value 3
VAL_4 = 8 /// Value 4

/// Example driver
pub struct Example<I2C, u8> {
i2c: I2C,
address: u8
}

// TODO Endian, swap sign

impl<I2C, E> Example<I2C, u8>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
{
/// Creates a new driver from an I2C peripheral
pub fn new(i2c: I2C, addr: u8) -> Result<Self, E> {
let mut example = Example { i2c, addr };
example._lifecycle_begin()?;

Ok(example)
}

// TODO u8 or other num types
fn read_registera(&mut self) -> Result<u8, E> {
let buffer: &mut [u8] = &mut buffer;
self.i2c.write_read(
self.address,
&[register.address],
buffer
)?;
Ok(buffer[0])
}

// TODO u8 or other num types
fn read_registerb(&mut self) -> Result<u8, E> {
let buffer: &mut [u8] = &mut buffer;
self.i2c.write_read(
self.address,
&[register.address],
buffer
)?;
Ok(buffer[0])
}

// TODO u8 or other num types
fn read_registerc(&mut self) -> Result<u8, E> {
let buffer: &mut [u8] = &mut buffer;
self.i2c.write_read(
self.address,
&[register.address],
buffer
)?;
Ok(buffer[0])
}

// TODO u8 or other num types
fn read_registerd(&mut self) -> Result<u8, E> {
let buffer: &mut [u8] = &mut buffer;
self.i2c.write_read(
self.address,
&[register.address],
buffer
)?;
Ok(buffer[0])
}

}