Created
June 6, 2015 16:20
-
-
Save HappyCodingRobot/8761235163f9a726bac9 to your computer and use it in GitHub Desktop.
Create correction table for PWM values based on CIE1931 algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
''' Script to create a correction table for PWM values | |
to create linear brightness for a LED | |
based on the CIE1931 algorithm | |
original author: ?? | |
''' | |
PWM_BIT_SIZE = 8 # Bit size of used PWM | |
CORR_TABLE_SIZE = 16 # Number of elements in vector | |
#### | |
INPUT_SIZE = CORR_TABLE_SIZE-1 # Input integer size / max vector index | |
OUTPUT_SIZE = 2**PWM_BIT_SIZE-1 # Output integer size / max pwm range | |
#INT_TYPE = 'const unsigned char' | |
INT_TYPE = 'const uint8_t' | |
TABLE_NAME = 'cie'; | |
def cie1931(L): | |
L = L*100.0 | |
if L <= 8: | |
return (L/902.3) | |
else: | |
return ((L+16.0)/116.0)**3 | |
x = range(0,int(INPUT_SIZE+1)) | |
y = [round(cie1931(float(L)/INPUT_SIZE)*OUTPUT_SIZE) for L in x] | |
with open('cie1931.h', 'w') as f: | |
f.write('// CIE1931 correction table\n') | |
f.write('// Automatically generated\n\n') | |
f.write('%s %s[%d] = {\n' % (INT_TYPE, TABLE_NAME, INPUT_SIZE+1)) | |
f.write('\t') | |
for i,L in enumerate(y): | |
if i < len(y)-1: | |
f.write('%d, ' % int(L)) | |
if i % 10 == 9: | |
f.write('\n\t') # linefeed after 10 elements | |
else: | |
f.write('%d' % int(L)) | |
f.write('\n};\n\n') | |
print('Done!\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment