因为在工作中,尤其在目前的工作中,经常是以周为单位进行工作总结和规划,那么, 在一个具体的项目工作中会碰上很多的问题,如果这些问题经常花一定的时间去写blog,感觉时间上不是特别允许。所以,这个category应该是总结,而不是plan。但是已经有了这个title,我也懒得再次进行修改,一直用下去得了。
这次在Raspberry安装CHIP对我而言是一个大考,难在了代理这块的设置上。 最终的解决方案是: 服务端使用的squid软件,然后,在树莓派不是使用终端定义http_proxy的方式,而是使用 raspi-config 配置工具进行总体配置。ok,成功解决问题。
看来有一个自己的服务器还是非常关键的。
善用vscode Task扩展。
过去的知识可能会变得过时,但是基础性的原理永远不会变,这也就是掌握基础的原因之一。
这周的任务是:
发现还是喜欢编代码,哈哈 , 挺好的目前,看看情况怎么发展吧,还真得修炼内功。
这篇文章缩减了不少内容。
Emscripten toolchain是WebAssembly的第一个工具,它模拟了一个特别的OS 系统在web 接口,也可以允许开发者使用libc。Emscripten Compiler Frontend(EMCC)可以直接看成类似gcc的工具。emcc可以使用Clang或者LLVM编译成wasm或者asm.js文件。
这个不用说了,就是一个标准的编译器集合。
JavaScript文件镶嵌在HTML中,当然也需要借助浏览器进行查看。那么,如果不用浏览器怎么运行呢?这个时候就需要借助JS的runtime (Node.js)去跑了。
该Runtime内部包含一个WebAssembly VMcore(iwasm).
Wasmer是一个非浏览器的、支持WASI和Emscription
https://liux120.github.io/ECE202_WASM/
这个命令真的值得花一点时间去记录。
最简单,如果处理.gz的文件,需要使用”-z“参数,符合认知。如果是bz2,需要使用”-j”选项。
“-x”
“-v”: verbose
“-f”: file name
将qemu目录压缩成gz包。
tar -czvf qemu-bak.tar.gz qemu # 源目录在后面
将上述压缩包解压
tar -zxvf qemu.tar.gz qemu # 目标文件在后面
首先创建文件夹: mkdir tmp-dir,然后使用 “-C” 参数解压到制定目录。
tar -zxvf qemu.tar.gz -C tmp-dir
tar xf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
tar --owner root --group root -cjf kernel_supplements.tbz2 lib/modules
这条命令是压缩成 tbz2 文件,且带着相应的权限。具体的作用就是把 lib/modules目录下 的文件压缩成kernel_supplements.tbz2 文件。
参考这个链接: https://www.cnblogs.com/hh2737/p/7710830.html
如果打包一个文件夹比如一个dts文件,那么正常情况下我是压缩的这个包,但是,如果在项目中需要 我们把dts下的东西直接解压到 dtb 目录下,这样是不行的。因为解压的时候,正常是在 dtb/dts的。 那么,这就需要我们在压缩的时候做一些改动。
有两种方式可以解决这个问题:
tar zcf dts.tar.gz *
这种方式会不会把这在生成的tar包也压缩进去,这值得我们怀疑,但是据我观察,好像不会的。
tar -zcf dts.tar.gz -C dts/ .
尤其别忘了后面的那一个”.”
初始化一个rust项目。
vimer@host:~/test/rust_2$ ls
Cargo.toml src/
rust的源代码可以放在这个目录下面。
vimer@host:~/test/rust_2$ cargo run
Compiling rust_2 v0.1.0 (/home/vimer/test/rust_2)
Finished dev [unoptimized + debuginfo] target(s) in 0.13s
Running `target/debug/rust_2`
Hello, world!
cargo run可以直接执行rust程序(当然是经过编译过后的),这样就省去了编译、执行的过程。
这个方式和python的import的道理是相同的。例子:
vimer@host:~/test/rust_2/src$ cat print.rs
pub fn run(){
println!("hell from run");
}// 这里必须有 pub 修饰词
main.rs:
vimer@host:~/test/rust_2/src$ cat main.rs
mod print; // 引入文件名,去掉后缀名
fn main() {
println!("Hello, world!");
print::run(); // mod name and fun
}
执行后的结果为:
vimer@host:~/test/rust_2/src$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `/home/vimer/test/rust_2/target/debug/rust_2`
Hello, world!
hell from run
再次强调一遍, 如果引入外部的函数,需要在声明函数的时候键入关键词pub.
fn greeting(greet: &str, name: &str){
println!("{}, {},nice to meet you", greet, name);
}
fn main() {
greeting("hello,", "vimer")
} // 参数,使用 &str表示表类型
返回值:
// 返回值, 使用->进行暗示
fn add(n1: i32, n2: i32) -> i32{
n1 + n2
} // 注意 n1 + n2 后面没有 ;
fn main() {
let get_sum = add(5,3);
println!("Sum: {}", get_sum);
}
还有一种近似的函数:
let add_nums = |n1:i32, n2:i32| n1 + n2 + n3;
println!("C sum: {}", add_nums(3,3));
// print 16
这个应该和C的指针差不多。
let arr1 = [1,2,3];
let arr2 = arr1;
println!("Values: {:?}", (arr1, arr2));
// print
//
//Vector
let vec1 = vec![1,2,3];
let vec2 = &vec1;
println!("Values: {:?}", (&vec1, vec2));
// Values: ([1, 2, 3], [1, 2, 3])
// Values: ([1, 2, 3], [1, 2, 3])
struct Color {
red: u8,
green: u8,
blue: u8,
}
fn main() {
let mut c = Color {
red: 255,
green: 0,
blue: 0,
};
println!("Color : {} {} {}", c.red, c.green, c.blue);
}
还有下面的这种方式也行:
struct Test (u8, u8, u8);
let mut c = Test(255, 0, 0);
一个更复杂的例子:
struct Person{
first_name: String,
last_name: String,
}
impl Person{
// Construct
fn new(first: &str, last: &str) -> Person{
Person {
first_name: first.to_string(), // Convert original struct member
last_name: last.to_string(),
}
}
// Get full name
// format?
fn full_name(&self) -> String {
format!("{} {}", self.first_name, self.last_name)
}
// Set last name
// ??
fn set_last(&mut self, last:&str) {
self.last_name = last.to_string();
}
// Name to tupel
fn to_tuple(self) ->(String, String) {
(self.first_name, self.last_name)
}
}
fn main(){
let mut p = Person::new("Mary", "Doe");
println!("Full name is {}", p.full_name());
p.set_last("vimer");
println!("Full name is {}", p.full_name());
println!("Tuple name is {:?}", p.to_tuple());
}
/* output:
Full name is Mary Doe
Full name is Mary vimer
Tuple name is ("Mary", "vimer")
*/
函数有返回值的,函数体没有最后的”;”.
enum Movement {
Up,
Down,
Left,
Right
}
fn move_avator(m: Movement) {
match m {
Movement::Up => println!("Up"),
Movement::Down => println!("Down"),
Movement::Left => println!("Left"),
Movement::Right => println!("Right")
}
}
fn main(){
let avatar1 = Movement::Up;
let avatar2 = Movement::Down;
let avatar3 = Movement::Left;
let avatar4 = Movement::Right;
move_avator(avatar1);
move_avator(avatar2);
move_avator(avatar3);
move_avator(avatar4);
}
/* output:
Up
Down
Left
Right
*/
还是要注意 match 中的符号问题。
use std::env;
fn main(){
let args: Vec<String> = env::args().collect();
println!("Args: {:?}", args);
}
// Args: ["/home/vimer/test/rust_2/target/debug/rust_2"]
vimer@host:~/test/rust_2/src$ cargo run hello
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `/home/vimer/test/rust_2/target/debug/rust_2 hello`
Args: ["/home/vimer/test/rust_2/target/debug/rust_2", "hello"]
let cmd = args[1].clone();
首先要根据官网的指令把rust安装到pc上,其包管理器是Cargo.
fn main() {
println!("hello, world");
}
保存为文件为 hello.rs. 然后使用命令rustc hello.rs去编译生成hello的二进制文件。
rust的注释有三种,其中”/**/”和”//”与c/c++的注释方式一致,第三种方式是
/// Doc which are parsed into html, begin
//! end
上面的println!就是将文本打印到console上并换行。
println!("{ } days", 31);
println!("{0}, this is {1}, {1} this is {0}", "yubo", "hechun");
”{}”会自动替换任何参数。指示词可以使用0和1自动填入参数,这样的参数在这里叫做位置参数Positional arguments。
还可以有 Named arguments. 比如下面这种:
// name arg:
println!("{sub} {verb} {obj}",
obj="vimer",
verb="hello",
sub = "test");
// {:b} print it with binary format
println!("{} of {:b} know binary, the other half does not", 1, 2);
// Right-align text with a special width
println!("{number:>width$}", number=1, width=6);
output:
test hello vimer
1 of 10 know binary, the other half does not
1
// Right-align text with a special width
println!("{number:>width$}", number=1, width=6);
println!("{number:>0width$}", number=1, width=6);
/*
1 of 10 know binary, the other half does not
1
000001
*/
以上的代码中,包含了格式(二进制b,对其, 填充)等格式说明。
如果位置参数确实怎么办? rust会直接给你报错。
println!("my name is {0}, and your name is {1}, {1} this is {0}", "vimer");
error:
error: invalid reference to positional argument 1 (there is 1 argument)
--> hello.rs:2:47
|
2 | println!("my name is {0}, and your name is {1}, {1} this is {0}", "vimer");
| ^^^ ^^^
|
= note: positional arguments are zero-based
error: aborting due to previous error
”” to debug rust program
Rust的类型比较特殊,是需要值得注意点。
fn main() {
// Variables can be type annotated.
let logical: bool = true;
let a_float: f64 = 1.0; // Regular annotation
let an_integer = 5i32; // Suffix annotation
// Or a default will be used.
let default_float = 3.0; // `f64`
let default_integer = 7; // `i32`
// A type can also be inferred from context
let mut inferred_type = 12; // Type i64 is inferred from another line
inferred_type = 4294967296i64;
// A mutable variable's value can be changed.
let mut mutable = 12; // Mutable `i32`
mutable = 21;
// Error! The type of a variable can't be changed.
mutable = true;
// Variables can be overwritten with shadowing.
let mutable = true;
}
下面的代码把rust基本的数据总结下来了:
fn main() {
println!("1+2={}", 1u32+2); // 3
println!("1-2={}", 1i32-2); // -1
println!("true and false is {}", true && false);// true and false is false
println!("true or false is {}", true || false); // true or false is true
println!("Not true is {}", !true); // Not true is false
// bitwise
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101u32); //0011 AND 0101 is 0001
println!("0011 or 0101 is {:04b}", 0b0011u32 | 0b0101u32);//0011 or 0101 is 0111
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101u32); //0011 XOR 0101 is 0110
println!("1 << 5 is {}", 1u32 << 5); // 1 << 5 is 32
println!("0x80 >> 2 is {:x}", 0x80u32 >> 2); //
println!("One million is written as {}", 1_000_000u32);// One million is written as 1000000
}
该修饰符可以改变变量的属性,主要是否可以改变其值的属性。
// first
let long_tuple = (1u8, 2u16, 3u32, 4u64,
-1i8, -2i16, -3i32, -4i64,
0.1f32, 0.2f64,
'a', true);
// print content of tuple of indexing
println!("first value {}", long_tuple.0);
println!("second value {}", long_tuple.1);
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
// print tuples, 元组中有元组可以打印,
println!("tuple of tuples: {:?}", tuple_of_tuples);
let too_long_tuple = (1,2,3,4,5,6,7,8,9,10);
println!("long tuple :{:?}", too_long_tuple); // 之前版本是不可以的,但是目前是可以的了
// make pair for tuple
let pair = (1, true);
println!("pair is {:?}",pair);
// 如果需要只打印一个元素的tuple,则需要","进行分割
println!("the one element tupleis {:?}", (8i8,)); //p: the one element tupleis (8,)
// 如果只有一个元素,就是单单打印一个元素
println!("the one element {:?}", (8i8)); //p: the one element 8
// second
let age = 28; // can not reassign value
let mut age = 28;
age = 36; // is ok
const ID: i32 = 001; // ok
// Assign multiple vars
let (my_name , my_age ) = ("Brad", 37);
println!("{} is {}, my_name, my_age");
println!("Max i32 is {} and i64 is {}", std::i32::MAX, std::i64::MAX);
rust是一个基于 block 的语言。
rust提供两种string的声明。一种是char*的形式,另一种是直接使用内置方法, 基于heap的内存模型。
// 1
let hello = "Hello"; // It is fixed-len string in memory
// 2
let hello = String::from("hello");
println!("Length: {}",hello.len());
下面是个有趣的例子:
fn main() {
let hello = String::from("hello ");
hello.push("world");
println!("{}", hello);
} // error! 1. let is default behave for immutable
// 2. push() is only for a word
//3. push_str() is supported for string
// right code :
fn main() {
let mut hello = String::from("hello ");
hello.push_str("world");
hello.push('!');
println!("{}", hello);
}
String的属性有
for word in hello.split_whitespace(){
println!("{}", word);
}
// hello
// world!
let mut s = String::with_capacity(10);
s.push('a');
s.push('b');
assert_eq!(3, s.len); // debug track
Tuple group together value of different types, max is 12
let person: (&str, &str, i8) = ("yubo", "vimer", 28);
println!("{} is form {} and is {}", person.0, person.1, person.2);
It is the same elements in a array.
let num: [i32;5] = [1,2,3,4,5]; // [ypye; num]
println!("{:?}", num);
// 1. 少一个元素都不行的
// 2. get a single val
println!("{}", num[3]);
// 3. reassign value
// 4. Arrays are stack
println!("Array occupies {} bytes", std::mem::size_of_val(&num));
/* Array occupies 20 bytes
*/
std还可以使用 use std; 在rs源文件开始的地方。
Slices这里可以看成切片,同python的用法差不多。可以由 array 转化而来。
let slice: &[i32] = #
println!("Slices is {:?}", slice);
// Slices is [1, 2, 3, 4, 5]
如果在使用切片时指定下标,则使用符号..
let slice: &[i32] = &num[0..2];
println!("Slices is {:?}", slice);
// Slices is [1, 2]
Vector is resize array, 目前看与array没有太大的区别。
fn main() {
let mut num: Vec<i32> = vec![1,2,3,4,5];
println!("{:?}", num);
println!("{}", num[3]);
// reassign value
num[3] = 20;
println!("Array occupies {} bytes", std::mem::size_of_val(&num));
let slice: &[i32] = &num[0..2];
println!("Slices is {:?}", slice);
}
则输出为:
[1, 2, 3, 4, 5]
4
Array occupies 24 bytes
Slices is [1, 2]
当然可以使用 push(), pop(),loop through vector values
for x in num.iter() {
println!("number: {}", x);
} // this is immut_value
for x in num.iter_mut() {
*x +x = 2;
}
let age = 22;
let check_id: bool = false;
if age >= 21 && check_id{
println!("haha");
} else {
println!("no you are not");
}
// short of if
let is_of_age = if age >= 21 { true } else { false };
let mut count = 0;
// infinite
loop {
count += 1;
println!("Number: {}", count);
if count == 20 {
break;
}
}
/*
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10
Number: 11
Number: 12
Number: 13
Number: 14
Number: 15
Number: 16
Number: 17
Number: 18
Number: 19
Number: 20
*/
while count <= 50 {
if count % 15 == 0 {
println!("fizzbuzz");
} else if count % 3 == 0 {
println!("fizee");
} else if count % 5 == 0{
println!("buzz");
} else {
println!("no {}", count);
}
count += 1;
}
The same result as above code:
for count in 0..50 {
if count % 15 == 0 {
println!("fizzbuzz");
} else if count % 3 == 0 {
println!("fizee");
} else if count % 5 == 0{
println!("buzz");
} else {
println!("no {}", count);
}
}
目前的c++还是熟悉数据结构的阶段,所以总结记录下。
作为工具就是,该容器的效率为O(1).常用方法有:
#include <unordered_map>
#include <iostream>
#include <vector>
using namespace std;
int main() {
unordered_map<string, vector<int>> keys;
keys["a"] = vector<int>(); // Initialize key with all null vector
keys["a"].push_back(1);
keys["a"].push_back(2);
for (const int &x : keys["a"]){
cout << x << endl;
}
/* Above code is store a int array with string */
/* insert value by [] oper */
unordered_map<string, double> umap;
umap["PI"] = 3.14;
umap["root2"] = 1.414;
umap["user1"] = 2.302;
/* insert value by insert function */
umap.insert(make_pair("e", 2.78));
string key = "PI";
if (umap.find(key) == umap.end())
cout << key << "not found\n\n";
else
cout << "found " << key << "\n";
key = "lambda";
if (umap.find(key) == umap.end())
cout << key << "not found" << "\n";
else
cout << "found " << "\n";
/* iterator elements with iter */
unordered_map<string, double>::iterator iter;
for (iter = umap.begin(); iter !=umap.end(); iter++){
cout << iter->first << " " << iter->second << endl;
}
}