Skip to content

Instantly share code, notes, and snippets.

View Gfast2's full-sized avatar
🚩

Su Gfast2

🚩
View GitHub Profile
@Gfast2
Gfast2 / ReactComponentSizing.tsx
Created May 15, 2020 08:47
A Way to read a react component size with resize eventhandler
class DivSize extends React.Component {
constructor(props) {
super(props)
this.state = {
width: 0
}
this.resizeHandler = this.resizeHandler.bind(this);
}
@Gfast2
Gfast2 / usingTypeParametersInTypeConstrain.ts
Created March 24, 2020 07:56
You can declare a type parameter that is constrained by another type parameter. For example, here we’d like to get a property from an object given its name. We’d like to ensure that we’re not accidentally grabbing a property that does not exist on the obj, so we’ll place a constraint between the two types:
// https://www.typescriptlang.org/docs/handbook/generics.html
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
let x = { a: 1, b: 2, c: 3, d: 4 };
getProperty(x, "a"); // okay
getProperty(x, "m"); // error: Argument of type 'm' isn't assignable to 'a' | 'b' | 'c' | 'd'.
Promise.resolve("original")
.then(
resolved => {
console.log(`Upper Promise resolved, message is "${resolved}"`);
throw "stage1 throw error"; // -> This thrown error will trigger the following then()'s rejected function, but not in this then()'s rejection.
return "stage1 succeed";
},
rejected => {
console.log(`Upper Promise rejected, message is "${rejected}"`);
// throw "stage1 failed" // -> will let the following then() receive a rejected Promise with this string
@Gfast2
Gfast2 / smartclip.js
Created December 5, 2019 16:37
A small quick answer I found in the most elegant way to solve the problem.
'use strict';
const main = () => {
const par1 = [3, 'smart'];
const par2 = [5, 'clip'];
const parser = (index, arr) => index % arr[0] === 0 ? arr[1] : '';
for (let i = 1; i <= 100; i++) {
const st = parser(i, par1);
const cp = parser(i, par2);
console.log(`${i} -> ${st}${cp}`);
alias w='cd ~/workspace;ll'
alias gs='git status'
alias gd='git diff'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log'
alias gr='git reset'
@Gfast2
Gfast2 / getTemperature.sh
Created November 15, 2018 15:23
Read Raspberry Pi's GPU & CPU's temprature.
#!/bin/bash
# Script: my-pi-temp.sh
# Purpose: Display the ARM CPU and GPU temperature of Raspberry Pi 2/3
cpu=$(</sys/class/thermal/thermal_zone0/temp)
echo "$(date) @ $(hostname)"
echo "------------------------------"
echo "GPU => $(vcgencmd measure_temp)"
echo "CPU => $((cpu/1000))°C"
@Gfast2
Gfast2 / main.c
Created November 18, 2017 18:46
A "binky" Example on ESP32 using ESP-IDF FreeRTOS, which address potential bug when man use while(1) in main_app() with vTaskDelayUntil to deal with delay job.
/* Demo program.
* https://esp32.com/viewtopic.php?f=2&t=3389
* esp-idf git commit: 7e8c2a9c00a6fb05cd5da306b62c4474a999b1a2
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
@Gfast2
Gfast2 / sdkconfig
Created October 15, 2017 18:17
addressing issue with unmathing baud rate between serial monitor and ESP32
#
# Automatically generated file; DO NOT EDIT.
# Espressif IoT Development Framework Configuration
#
#
# SDK tool configuration
#
CONFIG_TOOLPREFIX="xtensa-esp32-elf-"
CONFIG_PYTHON="python"
@Gfast2
Gfast2 / component.mk
Last active October 13, 2017 12:29
component compiling description for porting open62541 to esp-idf.
#Where the cmake project lives under this subdirectory
CODESUBDIR := $(COMPONENT_PATH)/open62541
#Path and name of the generated library
# LIBRARY := libopen62541.a
# C compiler flags
# original one: -std=gnu99 -Og -ggdb -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -Wno-old-style-declaration -DWITH_POSIX -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H -g -pipe -Wextra -Wformat -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar -Wstrict-overflow -Wcast-qual -Wmissing-prototypes -Winit-self -Wuninitialized -Wformat-security -Wformat-nonliteral -Wshadow -Wconversion -fvisibility=hidden -fPIC
CFLAGS := -Og -ggdb -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wal
@Gfast2
Gfast2 / component.mk
Created October 10, 2017 07:52
The component.mk file for open62541 library
#Where the cmake project lives under this subdirectory
CODESUBDIR := $(COMPONENT_PATH)/open62541
#Path and name of the generated library
LIBRARY := libopen62541.a
#Some projects need include files that are not available in esp-idf. You can specify a directory to 'fake'
#these entries here. Note: this will not work if the project includes these in public header files.
INCLUDE_FIXUP_DIR := $(IDF_PATH)/components/lwip/include/lwip/posix