Skip to content

Instantly share code, notes, and snippets.

@g8up
Created April 16, 2016 06:43
Show Gist options
  • Save g8up/d793ae951ad757c31030e3f1232402bd to your computer and use it in GitHub Desktop.
Save g8up/d793ae951ad757c31030e3f1232402bd to your computer and use it in GitHub Desktop.
生成单片机数码管显示数字对应的针脚16进制数值
// 生成单片机数码管显示数字对应的针脚16进制数值
var NumCodeMap = [
// 0
['a', 'b', 'c', 'd', 'e', 'f'],
// 1
['b', 'c'],
// 2
['a', 'b', 'd', 'e', 'g'],
// 3
['a', 'b', 'c', 'd', 'g'],
// 4
['b', 'c', 'f', 'g'],
// 5
['a', 'c', 'd', 'f', 'g'],
// 6
['a', 'c', 'd', 'e', 'f', 'g'],
// 7
['a', 'b', 'c'],
// 8
['a', 'b', 'c', 'd', 'e', 'f', 'g'],
// 9
['a', 'b', 'c', 'd', 'f', 'g'],
];
function sigma(numCode) {
var digCode = ['a', 'b', 'c', 'd', 'e', 'f', 'g'].map(function (item) {
return +(numCode.indexOf(item) > -1);
});
return parseInt(digCode.reverse().join(''), 2).toString(16);
}
function wrapHex(num) {
return '0x' + (num.length < 2 ? '0' : '') + num;
}
function get(num) {
if (num >= 0 && num <= 9) {
num = parseInt(num);
var hex = sigma(NumCodeMap[num]);
return wrapHex(hex);
}
return -1;
}
// console.log( get( 0 ) );
// '0123456789'.split('').map(function(item){ return get(item);})
// ["0x3f", "0x06", "0x5b", "0x4f", "0x66", "0x6d", "0x7d", "0x07", "0x7f", "0x6f"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment