Skip to content

Instantly share code, notes, and snippets.

@HappyCodingRobot
Created June 6, 2015 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HappyCodingRobot/8761235163f9a726bac9 to your computer and use it in GitHub Desktop.
Save HappyCodingRobot/8761235163f9a726bac9 to your computer and use it in GitHub Desktop.
Create correction table for PWM values based on CIE1931 algorithm
#!/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