Skip to content

Instantly share code, notes, and snippets.

View shawnfeng0's full-sized avatar
💭
Keep thinking

Shawn Feng shawnfeng0

💭
Keep thinking
View GitHub Profile
@shawnfeng0
shawnfeng0 / L_TOKEN.h
Created September 18, 2020 14:34
C++ version of LOGGER_TOKEN
#include <ulog/ulog.h>
namespace ulog {
namespace token {
// void *
inline void print(const char *name, const void *value) {
logger_raw(false, "%s => %p", name ? name : "unnamed", value);
}
@shawnfeng0
shawnfeng0 / slog.h
Last active August 19, 2020 03:52
Single header log library
#pragma once
#include <stdio.h>
#include <string.h>
// Precompiler define to get only filename;
#if !defined(__FILENAME__)
#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 \
: strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 \
@shawnfeng0
shawnfeng0 / fifo_power_of_2.h
Last active November 26, 2020 07:42
Efficient fifo queue class implemented by C++. (Reference from kfifo of linux)
// Reference from kfifo of linux
class FifoPowerOfTwo {
#define _is_power_of_2(x) ((x) != 0 && (((x) & ((x)-1)) == 0))
public:
FifoPowerOfTwo(void *buffer, size_t buf_size, size_t element_size = 1)
: data_((unsigned char *)buffer),
element_size_(element_size != 0 ? element_size : 1),
is_allocated_memory_(false) {
size_t num_elements = buf_size / element_size_;
@shawnfeng0
shawnfeng0 / trans_wrapper_for_zh-CN
Last active February 4, 2020 23:42
Package trans command line translation software to realize automatic translation into Chinese when the input language is English
#!/bin/bash
target_language=$(trans -id ${!#} | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g" | awk '$1=="Code" && $2!="zh-CN" {printf ":zh-CN"}')
trans $target_language "$@"
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
uintptr_t HexDumpData(const uint8_t *data, size_t length, size_t width = 16,
uintptr_t base_address = 0, bool tail_addr_out = true) {
const uint8_t *data_cur = data;
if (!data || width == 0) return 0;
while (length) {
@shawnfeng0
shawnfeng0 / Hex2Str.c
Created November 14, 2019 05:44
Convert hexadecimal to a string
char *Hex2Str(uint8_t *addr, size_t len, char *out_str, size_t out_str_len,
bool from_end) {
if (addr == NULL || len == 0 || out_str == NULL || out_str_len < 3)
return out_str;
len = len < (out_str_len-1) / 2 ? len : (out_str_len-1) / 2;
char hex[] = "0123456789ABCDEF";
char *str = out_str;
@shawnfeng0
shawnfeng0 / battery_level.cpp
Last active November 14, 2019 06:12
Battery voltage level back measurement
#include <stdint.h>
#include <stdlib.h>
enum BatteryLevel {
BATTERY_LEVEL_VERY_LOW = 0,
BATTERY_LEVEL_LOW = 1,
BATTERY_LEVEL_MEDIUM = 2,
BATTERY_LEVEL_HIGH = 3,
BATTERY_LEVEL_VERY_HIGH = 4,
BATTERY_LEVEL_END = 5
@shawnfeng0
shawnfeng0 / bits.h
Last active November 25, 2021 20:07
Read/Write some bits within a number
/**
* Modify some bits within a number
*
* @note The lowest index is 0, increasing sequentially
*
* @param origin Original data
* @param offset Offset that needs to be modified
* @param len Bit length
* @param value The value to which the specified bit needs to be replaced
* @return Modified value
@shawnfeng0
shawnfeng0 / CMakeLists.txt
Created November 30, 2018 16:47 — forked from khancyr/CMakeLists.txt
Ardupilot clion
cmake_minimum_required(VERSION 2.8.4)
project(Ardupilot)
# Function: EXCLUDE_FILES_FROM_DIR_IN_LIST
# Description: Exclude all files from a list under a specific directory.
# Param _InFileList: Input and returned List
# Param _excludeDirName: Name of the directory, which shall be ignored.
# Param _verbose: Print the names of the files handled
FUNCTION (EXCLUDE_FILES_FROM_DIR_IN_LIST _InFileList _excludeDirName _verbose)
@shawnfeng0
shawnfeng0 / docker-cross-device-link.md
Created November 28, 2018 12:53 — forked from Francesco149/docker-cross-device-link.md
docker error creating new backup file '/var/lib/dpkg/status-old': Invalid cross-device link