C Language

C Language Hello World #include <stdio.h> int main() { printf("hello, world\n"); return 0; } With arguments #include <stdio.h> int main(int argc, char* argv[]) { printf("arg count: %i\n", argc); for (int i=1; i<argc; i++) { printf("argv: %s\n", argv[i]); } return 0; } 编译与运行 gcc hello.c ./a.out comments // comment /* multiline comments */ Command-line arguments #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i=1; i<argc; i++) { printf("%s%s", argv[i], (i < argc-1) ? " ": ""); } printf("\n"); return 0; } argc: argument count argv: argument vector Includes #include <xxx.h>: search in standard places, ex: /usr/include #include "yyy.h": search in working dir Variables Data Types Data Type Size Value Range Format boolean 1 byte 1 char 1 -128 ~ 127 %d unsigned char 1 0 ~ 255 %u short 2+(dependent on complier) -32768 ~ 32767 %d unsigned short 2+ 0 ~ 65535 %u int short to long(dependent on complier) -2147483648 ~ 2147483647 %d unsigned int short to long 0 ~ 2^32 %u long 4+(dependent on complier) %ld unsigned long 4+ %lu float 4 3.4e-38 to 3.4e+38 %f double 4 1.7e-308 to 1.7e+308 Type Cast int x = 1; // signed与unsigned互转,保持二进制表示不变,值可能改变 unsigned int ux = (unsigned int) x; // 短类型转长类型 // 对于signed类型(Two's Complement编码)在左侧补最高位bit的值(sign extension) // 对于unsigned类型,在左侧补0(zero extension) long lx = (long) x; // 长类型转短类型,截断高位,然后根据编码类型(signed or unsigned)解释数值 short sx = (short) lx; Declaring and Instantiating Variables // declarating int year; // instantiating year = 2022; // Declaring and instantiating simultaneously int year = 2022 // Constant const float pi = 3.14; Symbolic Constants #define LOWER 0 #define UPPER 300 #define STEP 20 Enum Constants // starts at 0, next 1, etc... enum boolean { NO, YES }; // values are specified enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r' }; /* FEB = 2, MAR = 3, etc. */ enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; Array int ndigit[10]; for (i = 0; i < 10; ++i) { ndigit[i] = 0; } // assigns values to first 5 data int myarray[10] = {20, 30, 40, 50, 100}; int myarray[] = {20, 30, 40, 50, 100}; // size size = sizeof(myArray) / sizeof(int) 2-D Array // definition char daytab[2][13] = { {0, 31, 28, ..., 31}, {0, 31, 29, ..., 31} }; // get value daytab[i][j]; // passed to function f(int daytab[2][13]) { ... } f(int daytab[][13]) { ... } /* the number of row is irrelevant */ f(int (*daytab)[13]) { ... } /* a pointer to an array of 13 integers */ sizeof size_t:unsigned interger, defined in <stddef.h> #define NKEYS (sizeof keytab / sizeof(struct key)) #define NKEYS (sizeof keytab / sizeof(keytab[0])) Character string every character string has a ’\0’ at the end ...

January 1, 2000

C++ Language

C++ Language Install # MacOS xcode-select --install Hello World #include <iostream> int main() { std::cout << "Hello world!" << std::endl; return 0; } 编译与运行 g++ main.cpp -o main.out ./main.out 输入输出 iosteam #include <iostream> int main() { int v1 = 0, v2 = 0; std::cin >> v1 >> v2; std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1+v2 << std::endl; return 0; } 不知道有多少个输入时 int main() { string word; while (cin >> word) cout << word << endl; return 0; } 读取整行 int main() { string line; while (getline(cin, line)) cout << line << endl; return 0; } fstream #include <fstream> // open file ofstream outfile("file.txt"); // append file ofstream outfile("file.txt", ios_base::app); // write data to file if (! outfile) cerr << "failed to open file.\n"; else outfile << "content" << endl; // read data from file ifstream infile("file.txt"); if (! infile) cerr << "failed to open file.\n"; else { while (infile >> word) { cout << word << endl; } } 注释 /* * multi-line * comment */ int main() { // single line comment return 0; } 流程控制 while ...

January 1, 2000

Caddy

References Github - caddy 安装 Binary Keep Caddy Running 下载 binary:https://caddyserver.com/download 运行命令 sudo mv caddy /usr/bin/ sudo mkdir /etc/caddy sudo cp Caddyfile /etc/caddy/Caddyfile sudo groupadd --system caddy sudo useradd --system \ --gid caddy \ --create-home \ --home-dir /var/lib/caddy \ --shell /usr/sbin/nologin \ --comment "Caddy web server" \ caddy 将以下内容写入 /etc/systemd/system/caddy.service 文件 [Unit] Description=Caddy Documentation=https://caddyserver.com/docs/ After=network.target network-online.target Requires=network-online.target [Service] Type=notify User=caddy Group=caddy ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force TimeoutStopSec=5s LimitNOFILE=1048576 PrivateTmp=true ProtectSystem=full AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE [Install] WantedBy=multi-user.target 启动 sudo systemctl daemon-reload sudo systemctl enable --now caddy sudo systemctl status caddy 注意:centos 7 的 systemd 版本太低(219),需要先升级 ...

January 1, 2000

Censors

加速度传感器 MPU6050 References How to use MPU6050 6-axis (gyro + accelerometer) MEMS motion tracking sensor with Arduino Libraries Arduino-MPU6050 接线 VCC:2.0 ~ 5.0V I2C通信:SCL/SDA接Arduino对应接口 人体感应 AM312 接线 红外传感器 树莓派安装(系统为Raspbian Stretch) sudo apt update sudo apt install lirc 编辑/boot/config.txt dtoverlay=gpio-ir,gpio_out_pin=17,gpio_in_pin=18,gpio_in_pull=up 编辑 /etc/lirc/lirc_options.conf driver = default device = /dev/lirc0 重启sudo reboot后,查看是否有lirc0 ls -l /dev/lirc0 lsmod | grep lirc systemctl status lircd.service systemctl status lircd.socket References https://raspberrypi.stackexchange.com/questions/81876/raspberry-pi-3-not-lirc-not-running-working Remotes Database 音频 DFPlayer Mini References DFRobot - DFR0299 DFPlayer Mini Libraries DFRobotDFPlayerMini 接线 VCC:3.2 ~ 5.0V 通过UART接口与Arduino通信,注意Rx接口需要接1kΩ的电阻 似乎只能识别FAT16格式的SD卡,无法识别FAT32格式的 ...

January 1, 2000

ChatGPT

Ideas 3 levels Use chatgpt to do job make tools to facilitate the workflow of using chatgpt improve model to do job LLM Learning things by induction, Human can learn by deduction Learning Papers Recent Advances in Natural Language Processing via Large Pre-Trained Language Models- A Survey Articles 拆解追溯 GPT-3.5 各项能力的起源 Generative AI exists because of the transformer Prompt Engineering Prompting Principles Principle 1: Write clear and specific instructions Use delimiters to clearly indicate distinct parts of the input Ask for a structured output Ask the model to check whether conditions are satisfied “Few-shot” prompting Principle 2: Give the model time to “think” Specify the steps required to complete a task Instruct the model to work out its own solution before rushing to a conclusion Iterative Prompt Development ...

January 1, 2000

CLI Apps

References Github - awesome-cli-apps Shell chezmoi chezmoi - Quick Start dotfiles dotfiles example # install sh -c "$(curl -fsLS get.chezmoi.io)" sudo mv bin/chezmoi /usr/local/bin # init chezmoi init # add dotfile chezmoi add {dotfile} # goto git dir chezmoi cd # init chezmoi and pull repo chezmoi init {git_url} # apply changes chezmoi apply -v # pull and apply chezmoi update -v tmux 可以通过修改配置 .tmux.conf 修改 prefix 键 ...

January 1, 2000

Commands

Commands 文件与文件夹操作 文件夹操作 ls # 查看文件夹 ls your_dir ls -F # 显示文件类型,/:文件夹,*:可执行文件,@:链接 ls -F your_dir | grep / # 只显示文件夹 ls -F your_dir | grep -v / # 只显示文件 tree # 以树形显示文件夹 tree tree -L 2 # 参看目录层数2以内的文件 tree -f # 显示完整路径 tree -d # 只显示文件夹 pwd # 显示当前目录 pwd mkdir # 创建文件夹 mkdir dir mkdir -p your_dir/subdir # 创建多级文件夹 tree -fid --noreport your_dir >> your_dir.txt && mkdir tmp && cd tmp/ && mkdir -p `cat ../your_dir.txt` # 复制目录结构 touch # 创建文件 touch your_file # 创建文件或更新文件时间戳 touch -a|-m your_file # 只更新访问|修改时间 touch -t 202102071200.00|YYYYmmddHHMM.SS # 更新为指定时间戳 cd # 切换文件夹 cd your_dir cd ..|~|- # 切换到父文件夹|用户文件夹|上一次文件夹 cp # 复制文件夹 cp source target cp -p # 保留源文件所有属性 cp -d # 保留符号链接指向 cp -r # 递归复制文件夹 cp -a # == cp -pdr mv # 移动文件夹 mv source target mv source_1 source_2 source_3 target # 移动多个文件到目标文件夹 rm # 删除 rm file rm -rf dir # 你懂的 rename # 重命名文件 rename .png .jpg ./* # 将所有文件的后缀由 .png 改成 .jpg ln # 链接 ln your_target your_link # hard link,多个文件名指向同一个索引节点(inode),只有当文件所有的硬链接都被删除时,文件才真正删除。可以通过创建硬链接防止文件误删 ln -s your_target your_link # soft/symbolic link,相当于创建快捷方式 # 查看链接具体的命令 type ll # 输出:ll is aliased to `ls -l --color=auto' 文件夹路径 basename # 显示文件名 basename /data/dir1/file1.txt basename /data/dir1/file1.txt .txt # 不显示后缀 .txt dirname # 显示路径 dirname /data/dir1/file1.txt dirname ./file1.txt # 传入相对路径也会返回相对路径 查看文件信息 ls -l 输出的含义 1|2 |3 |4 |5| 6 | 7 | 8 | 9 | 10 -rw-rw-r-- 1 yzchen yzchen 6 Feb 7 15:23 newfile drwxrwxr-x 2 yzchen yzchen 4096 Feb 7 15:25 subdir 文件(夹)类型 文件所属用户权限 文件所属用户组权限 其他用户权限 硬链接个数 所属用户 所属用户组 文件(夹)大小 修改时间 名称 file 查看文件类型 ...

January 1, 2000

Computer Vision

Face Detection retina face Github - retinaface: deep learning based cutting-edge facial detector deepface Github - deepface: a lightweight face recognition and facial attribute analysis (age, gender, emotion and race) framework Install: pip install deepface Usage: from deepface import DeepFace # Detection: 从照片中检测人脸 face_objs = DeepFace.extract_faces( img_path = "img.jpg", detector_backend = 'retinaface', # retina face 检测效果较好 align = False, enforce_detection = False, ) # Verification: 判断两张图片是否同一个人 result = DeepFace.verify(img1_path = "img1.jpg", img2_path = "img2.jpg") # 默认会先做 detection,如果是已经提取了人脸的照片可以用 result = DeepFace.verify(img1_path = "img1.jpg", img2_path = "img2.jpg", detector_backend='skip') # 指定相似度阈值 result = DeepFace.verify(img1_path = "img1.jpg", img2_path = "img2.jpg", threshold=0.5) # Embedding: 提取人脸 embedding,可以用于后续的对比计算 embedding_objs = DeepFace.represent(img_path = "img.jpg") # Recognition: 人脸识别,从人脸库中找出最符合的人 dfs = DeepFace.find( img_path = "img1.jpg", db_path = "./workspace/my_db", model_name = "VGG-Face", # 默认模型是 VGG-Face ) # Analysis:提取脸部特征(年龄、性别、情绪等) objs = DeepFace.analyze( img_path = "img4.jpg", actions = ['age', 'gender', 'race', 'emotion'], )

January 1, 2000

Crypto

Crypto Exchanges coinmarketcap Binance okex 火币 coinbase coinbase pro gemini kraken bitstamp swap cash.app Wallet Electrum https://electrum.readthedocs.io/en/latest/ seed:squeeze harsh aware aisle lemon blood dog paddle size super reflect two Google Authenticator biance:PATFWTFTREMYRFZT Tools CCXT Install pip install ccxt Usage import ccxt print(ccxt.exchanges) Keywords sandwich attack MEV(Miner Extractable Value) Data Bitcoin Futures Market Breakdown coinglass laevitas Ref https://ethereum.org/en/developers/docs/ https://www.picol.com/zh-CN https://www.pionex.com/en-US Terms Spot:现货 Margin:杠杠 Derivatives Options expires Futures expires Perpetual Swaps:永续合约 no expiry date ...

January 1, 2000

Design

Snippets text center <div class="text-center"> <h1>Welcome</h1> </div> Design Margin & padding Flex A Complete Guide to Flexbox FLexbox Playground Fonts Google Fonts Top 10 Web Fonts Serif Georgia Times New Roman Merriweather Sans-Serif Roboto Open Sans Source Sans Pro Lato Raleway Poppins Ariel Color Color Harmonies Complementary colors: Colors that are directly opposite one another on the color wheel are known as complementary colors. Complementary colors have a high contrast and can be very effective as accent colors when paired with a more neutral palette. ...

January 1, 2000