# b15f.rs - source
Source of the Rust file `src/b15f.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374

    //! This module contains all the structures and functions related to
    //! interacting with the B15 on a high level. If you are writing code
    //! for the B15, this is the module you want to use.

    use std::{process::Command, time::Duration, fmt::Debug, thread::sleep};
    use rand::Rng;
    use serialport::SerialPort;

    use crate::{assert::assert_in_range, error::Error, request::Request, build_request};

    macro_rules! log {
    	($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*));
    	($text: literal) => (println!(concat!("[B15F] ", $text)));
    }

    macro_rules! log_start {
    	($text: literal, $($arg:tt)*) => (print!(concat!("[B15F] ", $text, "... "), $($arg)*));
    	($text: literal) => (print!(concat!("[B15F] ", $text, "... ")));
    }

    macro_rules! log_end {
    	($text: literal, $($arg:tt)*) => (println!($text, $($arg)*));
    	($text: literal) => (println!($text));
    }

    /// Structure representing the driver for the board 15
    pub struct B15F {
    	usart: Box<dyn SerialPort>
    }

    impl B15F {
    	const MSG_OK: u8 = 0xFF;

    	/// Creates a new instance of the B15
    	///
    	/// This function will establish a connection to a connected B15 and return
    	/// a handle to interact with it. Only one such instance should exist per
    	/// program; calling `B15F::new()` more than once might lead to unexpected
    	/// behaviour.
    	///
    	/// # Returns
    	/// A new B15F object is returned. It contains an already active USART connection,
    	/// so calling this function multiple times will create an Error
    	///
    	/// # Errors
    	/// An `error::Error` is generated if the connection to the board cannot be
    	/// established, or if testing of that connection fails.
    	///
    	/// # Examples
    	/// ```
    	/// use b15f::B15F;
    	///
    	/// let drv = B15F::new().unwrap();
    	/// ```
    	pub fn new() -> Result<B15F, Error> {
    		let port = B15F::init_connection()?;

    		let mut drv =B15F {
    			usart: port
    		};

    		log_start!("Testing connection");
    		let mut tries = 3;
    		while tries > 0 {
    			drv.discard()?;

    			match drv.test_connection() {
    				Ok(()) => break,
    				Err(_) => {} // Do nothing
    			};

    			match drv.test_int_conv() {
    				Ok(()) => break,
    				Err(_) => {}
    			}

    			tries -= 1;
    		}

    		if tries == 0 {
    			return Err("Connection test failed. Are you using the newest version?".into());
    		}

    		log_end!("Ok!");

    		let info = drv.get_board_info()?;
    		log!("AVR firmware version: {} built at {} ({})", info[0], info[1], info[2]);

    		// let avr_commit_hash = info[3];
    		// if avr_commit_hash != COMMIT_HASH {
    		// 	log!("Different commit hashes: {} vs {}", avr_commit_hash, COMMIT_HASH);
    		// 	return Err("Versions incompatible. Please update the software!".into());
    		// }

    		Ok(drv)
    	}

    	fn init_connection() -> Result<Box<dyn SerialPort>, Error> {
    		let devices = B15F::get_devices();

    		let device = match devices.first() {
    			Some(item) => item,
    			None => return Err("Failed to find adapter".into())
    		};

    		log!("Using adapter: {}", device);

    		log_start!("Establish connection with adapter");

    		let port = serialport::new(device, 57_600)
    											.timeout(Duration::from_millis(1000))
    											.open()?;
    		log_end!("Ok!");

    		Ok(port)
    	}

    	/// Sets the value of the specified port
    	///
    	/// # Errors
    	/// `PORT` can either be 0 or 1, other values will cause a compile-time
    	/// error. Otherwise an `error::Error` is generated if communication
    	/// with the B15 fails.
    	///
    	/// # Examples
    	/// ```
    	/// use b15f::B15F;
    	///
    	/// fn main() -> Result<(), String> {
    	/// 	let mut drv = B15F::new()?;
    	///
    	/// 	drv.digital_write::<0>(0xFF)?;		// Turn on all bits of port 0
    	/// 	drv.digital_write::<1>(0x0F)?;		// Turn on bits 0-4 of port 1
    	///
    	/// 	// drv.digital_write::<2>(0xFF);	// Compiler error
    	///
    	/// 	Ok(())
    	/// }
    	/// ```
    	///
    	pub fn digital_write<const PORT: usize> (&mut self, value: u8) -> Result<(), Error> {
    		assert_in_range::<PORT, 0, 1>();

    		let reversed = value.reverse_bits();
    		let request = match PORT {
    			0 => Request::DigitalWrite0,
    			1 => Request::DigitalWrite1,
    			_ => panic!("Report this issue to someone, this should not ever be visible.")
    		};

    		self.usart.write(build_request![request, reversed])?;

    		let mut aw: [u8; 1] = [0; 1];
    		self.usart.read(&mut aw)?;

    		if aw[0] != B15F::MSG_OK {
    			return Err(format!("Setting Port {} failed", PORT).into());
    		}

    		Ok(())
    	}

    	/// Reads the value of the specified port
    	///
    	/// # Errors
    	/// `PORT` can either be 0 or 1, other values will cause a compile-time
    	/// error. Otherwise an `error::Error` is generated if communication
    	/// with the B15 fails.
    	///
    	/// # Examples
    	/// ```
    	/// use b15f::B15F;
    	///
    	/// fn main() -> Result<(), String> {
    	/// 	let mut drv = B15F::new()?;
    	///
    	/// 	let _ = drv.digital_read::<0>()?;		// Read inputs of port 0
    	/// 	let _ = drv.digital_read::<1>()?;		// Read inputs of port 1
    	///
    	/// 	// drv.digital_read::<2>();				// Compiler error
    	///
    	/// 	Ok(())
    	/// }
    	/// ```
    	///
    	pub fn digital_read<const PORT: usize> (&mut self) -> Result<u8, Error> {
    	 	assert_in_range::<PORT, 0, 1>();

    		let request = match PORT {
    			0 => Request::DigitalRead0,
    			1 => Request::DigitalRead1,
    			_ => panic!("Report this issue to someone, this should not ever be visible.")
    		};

    		self.usart.clear(serialport::ClearBuffer::Input)?;
    		self.usart.write(build_request![request])?;

    		let mut aw: [u8; 1] = [0; 1];
    		self.usart.read(&mut aw)?;

    		Ok(u8::reverse_bits(aw[0]))
    	}

    	/// Yields information about the installed firmware on the B15
    	///
    	/// Returns an array of strings, where each string contains a piece
    	/// of information stored on the B15
    	///
    	/// # Returns
    	/// A list of strings where each string contains a piece of information
    	/// about the board. What string contains what information is determined,
    	/// but not explicitly listed.
    	///
    	/// # Errors
    	/// An `error::Error` is generated if the communication with the board fails.
    	///
    	/// # Examples
    	/// ```
    	/// use b15f::B15F;
    	///
    	/// let mut drv = B15F::new().unwrap();
    	///
    	/// // Print each bit of information on a new line
    	/// drv.get_board_info()
    	/// 	.unwrap()
    	/// 	.iter()
    	/// 	.for_each(|info| println!("{info}"));
    	/// ```
    	pub fn get_board_info(&mut self) -> Result<Vec<String>, Error> {
    		let mut info: Vec<String> = vec![];

    		self.usart.write(build_request!(Request::Info))?;

    		let mut data_count: [u8; 1] = [0;1];
    		self.usart.read(&mut data_count)?;

    		while data_count[0] > 0 {
    			let mut len: [u8; 1] = [0;1];
    			self.usart.read(&mut len)?;

    			let mut data: Vec<u8> = vec![0; len[0] as usize];
    			self.usart.read(data.as_mut_slice())?;

    			info.push(
    				data.into_iter()
    					.map(|c| char::from(c))
    					.collect::<String>()
    			);

    			sleep(Duration::from_millis(4));	// Add delay to give the board time to catch up with our requests
    			data_count[0] -= 1;
    		}

    		let mut aw: [u8; 1] = [0; 1];
    		self.usart.read(&mut aw)?;

    		if aw[0] != B15F::MSG_OK {
    			return Err(format!("Board info is faulty: code {}", aw[0]).into());
    		}

    		Ok(info)
    	}

    	/// Clears data in the USART buffers on this device and on the B15
    	pub fn discard(&mut self) -> Result<(), Error> {
    		self.usart.clear(serialport::ClearBuffer::Output)?;

    		for _ in 0..16 {
    			self.usart.write(build_request![Request::Discard])?;
    			sleep(Duration::from_millis(4));
    		}

    		self.usart.clear(serialport::ClearBuffer::Input)?;

    		Ok(())
    	}

    	/// Test the integer conversion of the USART connection
    	///
    	/// # Errors
    	/// If an error occurs in the conversion or the communication with the
    	/// board, an `error::Error` will be returned.
    	pub fn test_int_conv(&mut self) -> Result<(), Error> {
    		let dummy: u16 = rand::thread_rng().gen_range(0x0000..=(0xFFFF / 3));

    		self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?;

    		let mut aw: [u8; 2] = [0; 2];
    		self.usart.read(&mut aw)?;

    		let result = u16::from_le_bytes(aw);
    		if result != dummy * 3 {
    			return Err("Int conversion failed".into());
    		}

    		Ok(())
    	}

    	/// Tests the connetion to the B15
    	///
    	/// To test the connection a `Request::Test` request will be sent
    	/// to the board together with a randomly generated value. If the
    	/// board returns that value the connection is working correctly.
    	///
    	/// # Errors
    	/// An `error::Error` is returned if the test fails, or if the
    	/// communication itself fails.
    	///
    	/// # Examples
    	/// ```
    	/// use b15f::B15F;
    	///
    	/// fn main() {
    	/// 	let mut drv = B15F::new().unwrap();
    	///
    	/// 	if let Err(err) = drv.test_connection() {
    	/// 		panic!("Connection is not working: {err}");
    	/// 	}
    	/// }
    	/// ```
    	pub fn test_connection(&mut self) -> Result<(), Error> {
    		let dummy: u8 = rand::thread_rng().gen_range(0x00..=0xFF);

    		self.usart.write(build_request![Request::Test, dummy])?;

    		let mut buffer: [u8; 2]= [0; 2];
    		self.usart.read(&mut buffer)?;

    		if buffer[0] != B15F::MSG_OK || buffer[1] != dummy {
    			return Err("Test request failed".into());
    		}

    		Ok(())
    	}

    	#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
    	fn get_devices() -> Vec<String> {
    		let output = Command::new("bash")
    			.args(["-c", "ls /dev/ttyAMA*"])
    			.output()
    			.expect("Failed to get serial interface");

    		String::from_utf8(output.stdout)
    			.expect("Failed to convert stdout to string")
    			.split_ascii_whitespace()
    			.map(|item| item.into())
    			.collect()
    	}

    	#[cfg(not(target_arch = "arm"))]
    	#[cfg(not(target_arch = "aarch64"))]
    	fn get_devices() -> Vec<String> {

    		let output = Command::new("bash")
    			.args(["-c", "ls /dev/ttyUSB*"])
    			.output()
    			.expect("Failed to get serial interface");

    		String::from_utf8(output.stdout)
    			.expect("Failed to convert stdout to string")
    			.split_ascii_whitespace()
    			.map(|item| item.into())
    			.collect()
    	}
    }

    impl Debug for B15F {
    	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    		writeln!(f, "Baudrate:  {}", self.usart.baud_rate().unwrap())?;
    		writeln!(f, "Data bits: {:?}", self.usart.data_bits().unwrap())?;
    		writeln!(f, "Parity:    {:?}", self.usart.parity().unwrap())
    	}
    }