Created
April 4, 2021 13:13
-
-
Save circuit4u-medium/e9003b3efad930628ff38fb4e3b12f42 to your computer and use it in GitHub Desktop.
FT232H NeoPixel for string art
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Device information: DeviceInfo { port_open: true, speed: None, device_type: FT232H, vendor_id: 0x0403, product_id: 0x6015, serial_number: MLRHTR, description: USB <-> Serial Converter }\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": 10, | |
"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", | |
"}\n", | |
"\n", | |
"const LED_OFF:u8 = 136; //make_byte(0)[0];\n", | |
"\n", | |
"fn modify_grb_array(mut f:[u8;12*144],led_index:usize,g:u8,r:u8,b:u8) -> [u8;12*144] {\n", | |
" let a:usize = led_index*12;\n", | |
" for (i,v) in (a..(a+12)).enumerate() {\n", | |
" f[v] = make_grb(g,r,b)[i];\n", | |
" }\n", | |
" \n", | |
" return f\n", | |
"}\n", | |
"\n", | |
"fn three_led(head:usize, middle:usize, tail:usize) ->[u8;12*144] { \n", | |
" let mut k = modify_grb_array([LED_OFF;12*144],head,0,10,0);//red\n", | |
" let mut k = modify_grb_array(k, middle,10,0,0);//green\n", | |
" let mut k = modify_grb_array(k, tail,0,0,10);//blue\n", | |
" return k\n", | |
"}\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Send SPI to Neopixel" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const MOSI:u8 = 0x02u8; //AD1\n", | |
"const SCK:u8 = 0x01u8; //AD0\n", | |
"\n", | |
"ft.set_bit_mode(0, BitMode::Mpsse);\n", | |
"ft.write(&[0x8A, 0x86,11,0]); //400ns per bit\n", | |
"\n", | |
"fn write_tuple(ft:&mut Ftdi, r:usize,g:usize,b:usize){\n", | |
" let a = three_led(r,g,b).to_vec();\n", | |
" let b = (a.len()-1) as u16;\n", | |
" ft.write(&([vec![0x80,0,MOSI+SCK,0x11,(b & 0xff) as u8,(b>>8) as u8],a].concat()));\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## write RGB tuple" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"write_tuple(&mut ft, 1,10,100)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"write_tuple(&mut ft, 100,15,12)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"write_tuple(&mut ft, 12,16,50)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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