Skip to content

Instantly share code, notes, and snippets.

@electronut
Created August 18, 2017 07:22
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 electronut/382ce40be1d138bbd1ba5ee689e83344 to your computer and use it in GitHub Desktop.
Save electronut/382ce40be1d138bbd1ba5ee689e83344 to your computer and use it in GitHub Desktop.
caclChannelValue
uint32_t caclChannelValue(uint8_t level)
{
uint32_t val = 0;
// 0
if(level == 0) {
val = 0x88888888;
}
// 255
else if (level == 255) {
val = 0xeeeeeeee;
}
else {
// apply 4-bit 0xe HIGH pattern wherever level bits are 1.
val = 0x88888888;
for (uint8_t i = 0; i < 8; i++) {
if((1 << i) & level) {
uint32_t mask = ~(0x0f << 4*i);
uint32_t patt = (0x0e << 4*i);
val = (val & mask) | patt;
}
}
// swap 16 bits
val = (val >> 16) | (val << 16);
}
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment