Skip to content

Instantly share code, notes, and snippets.

@circuit4u-medium
Last active October 13, 2020 13:59
Show Gist options
  • Save circuit4u-medium/50f361c3003b27e7b1bcd1150c9a8458 to your computer and use it in GitHub Desktop.
Save circuit4u-medium/50f361c3003b27e7b1bcd1150c9a8458 to your computer and use it in GitHub Desktop.
FT2232H and NeoPixel
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Import libftd2xx and others"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
":dep libftd2xx\n",
":dep convert-base\n",
"use convert_base::Convert;\n",
"use libftd2xx::{Ftdi, FtdiCommon, BitMode};"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## open FTDI FT2232H"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Device information: DeviceInfo { port_open: true, speed: None, device_type: FT2232H, vendor_id: 0x0403, product_id: 0x6010, serial_number: MLTS90A, description: FT2232H MiniModule A }\n"
]
}
],
"source": [
"let mut ft = Ftdi::new()?;\n",
"//let mut ft = Ftdi::with_serial_number(\"MLTS91A\")?; //use FTDI \n",
"let info = ft.device_info()?;\n",
"println!(\"Device information: {:?}\", info);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### **defered** "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ft.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Neopixel functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"fn make_byte(a :u8) -> Vec<u8>{\n",
" let mut b = Convert::new(255,2); //color value to binary\n",
" let mut out = b.convert::<u8,u8>(&[a]);\n",
" out.resize_with(8,Default::default);//pad with zero to 8-BIT \n",
" out.reverse(); //MSB..LSB\n",
" \n",
" let mut k = Vec::new();\n",
" for x in out.chunks(2){ //every 2-bit to one SPI byte\n",
" match x {\n",
" [0,0] => k.push(0x88),\n",
" [0,1] => k.push(0x8C),\n",
" [1,0] => k.push(0xC8),\n",
" [1,1] => k.push(0xCC),\n",
" _ =>(),\n",
" } \n",
" }\n",
" k\n",
"}\n",
"\n",
"fn make_grb(g:u8,r:u8,b:u8)->Vec<u8>{\n",
" [make_byte(g),make_byte(r),make_byte(b)].concat()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[136, 136, 200, 200, 136, 136, 200, 200, 136, 136, 200, 200]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"make_grb(10,10,10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Send SPI to Neopixel"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"const MOSI:u8 = 0x02u8; //AD1\n",
"const SCK:u8 = 0x01u8; //AD0"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"ft.set_bit_mode(0, BitMode::Mpsse);\n",
"ft.write(&[0x8A, 0x86,11,0]); //400ns per bit"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"one pixel RGB => 12 bytes SPI"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ok(())"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.write(&([vec![0x80,0,MOSI+SCK,0x11,11,0],make_grb(10,10,0)].concat()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"three pixels => 36 bytes SPI"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ok(())"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ft.write(&([vec![0x80,0,MOSI+SCK,0x11,35,0],make_grb(10,0,0),make_grb(0,10,0),make_grb(0,0,10)].concat()))"
]
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Rust",
"language": "rust",
"name": "rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "Rust",
"pygment_lexer": "rust",
"version": ""
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment