原理
python 脚本亮度和gammga 转换
#!/usr/bin/env python
## Creates a gamma-corrected lookup table
import sys
import math
import argparse
def gamma(nsteps, gamma):
gammaedUp = [math.pow(x, gamma) for x in range(nsteps)]
return [x/max(gammaedUp) for x in gammaedUp]
def rounder(topValue, gammas):
return [min(topValue, round(x*topValue)) for x in gammas]
def gamma_generator(steps, myGamma):
output = file("led_gamma.h", "w")
output.write("/* %d-step brightness table: gamma = %s */ \n" % (steps, myGamma))
output.write("const uint8_t gamma_table[%d] = {" % steps)
for idx,value in enumerate(rounder(255, gamma(steps, myGamma))):
if idx % 16 == 0:
output.write("\n\t")
output.write("%3d," % value)
output.write("\n};\n")
output.close()
def main(argv):
parser = argparse.ArgumentParser(
description='A tool to generate led gamma table'
)
parser.add_argument('-s', '--step', default="256", help='gamma table step')
parser.add_argument('-g', '--gamma', default="2.3", help='gamma value')
args=parser.parse_args(argv)
gamma_generator(int(args.step), float(args.gamma))
if __name__ == "__main__":
main(sys.argv[1:])
第一个参数代表 pwm 可调节挡位 默认 256 档, 第二个参数为要调节的gamma 值
执行如上的脚本 ,会生成 led_gamma.h
/* 256-step brightness table: gamma = 2.3 */
const uint8_t gamma_table[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 10,
11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 24, 25, 26, 26,
27, 28, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38,
38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
69, 70, 71, 72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 84, 85, 86,
87, 89, 90, 91, 92, 94, 95, 96, 98, 99,100,102,103,104,106,107,
109,110,112,113,114,116,117,119,120,122,123,125,126,128,130,131,
133,134,136,138,139,141,143,144,146,148,149,151,153,154,156,158,
160,161,163,165,167,169,170,172,174,176,178,180,182,183,185,187,
189,191,193,195,197,199,201,203,205,207,209,211,213,215,218,220,
222,224,226,228,230,233,235,237,239,241,244,246,248,250,253,255,
};
比如我们设置的颜色为 0xff2233, 如果按照没有校准前的话,LED三色灯的PWM亮度设置为255/34/51
经过gamma 校准后 设置为 gamma_table[255]/gamma_table[34]/gamma_table[51]