Advertisement
KRITSADA

iLED4 Extension for makeCode

Aug 27th, 2022
1,539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 4.82 KB | Source Code | 0 0
  1. /**
  2.  * makecode iLED4 4 Digit 7 Segment with I2C Bus Package
  3.  */
  4. enum iLED4_CMD {TURN_OSCILLATOR_ON = 0x21, TURN_DISPLAY_ON = 0x81, SET_BRIGHTNESS = 0xE0 }
  5.  
  6. /**
  7.  * iLED4 block
  8.  */
  9. //% weight=100 color=#008000 icon="\uf2db" block="iLED4"
  10. namespace ht16k33 {
  11.     let segment= [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71];
  12.     let displaybuffer: Array<NumberFormat.UInt16BE> = [0, 0, 0, 0, 0, 0, 0, 0];
  13.     function sendCommand(command: iLED4_CMD) {
  14.         pins.i2cWriteNumber(0x70,0,NumberFormat.Int8LE,false)
  15.         pins.i2cWriteNumber(0x70,command,NumberFormat.Int8LE,false)
  16.     }
  17.     function WriteDisplay() {
  18.         let buffer = pins.createBuffer(17);
  19.         buffer[0] = 0x00;
  20.         for (let i = 0; i < 8; i++) {
  21.             buffer[1 + (i * 2) + 0] = displaybuffer[i] & 0xFF;
  22.             buffer[1 + (i * 2) + 1] = displaybuffer[i] >> 8;
  23.         }
  24.         pins.i2cWriteBuffer(0x70, buffer);
  25.     }
  26.     /**
  27.      * set iLED4 intensity, range is [0-15], 0 is off.
  28.      * @param val the brightness of the iLED4, eg: 15
  29.      */
  30.     //% blockId="iLED4_SET_BRIGHTNESS" block="iLED4 set Brightness %bright"
  31.     //% weight=30 blockGap=8
  32.     //% bright.min=0 bright.max=15
  33.     export function setBrightness(bright: number) {
  34.         sendCommand(iLED4_CMD.SET_BRIGHTNESS | bright );
  35.     }
  36.  
  37.  
  38.     /**
  39.     * set iLED4 Blink Rate, range is [0-3], 0 is Not Blink.
  40.     * @param set Blink Rate for iLED4, eg: 0
  41.     */
  42.     //% blockId="iLED4_BLINK_RATE" block="iLED4 set Blink Rate %rate"
  43.     //% weight=30 blockGap=8
  44.     //% rate.min=0 rate.max=3
  45.     export function BlinkRate(rate: number) {
  46.         sendCommand(iLED4_CMD.TURN_DISPLAY_ON | (rate & 3) << 1);
  47.     }
  48.  
  49.  
  50.     //% blockId="iLED4_INIT" block="Init_iLED4"
  51.     //% weight=98 blockGap=8
  52.     export function init_() {
  53.         pins.P20.setPull(PinPullMode.PullNone)
  54.         pins.P19.setPull(PinPullMode.PullNone)
  55.         sendCommand(iLED4_CMD.TURN_OSCILLATOR_ON)
  56.         sendCommand(iLED4_CMD.TURN_DISPLAY_ON)
  57.         setBrightness(15);
  58.     }
  59.  
  60.     //% blockId="iLED4_Clear" block="iLED4 Clear"
  61.     //% weight=70 blockGap=8
  62.     export function clear(){
  63.         for (let i = 0; i < 8; i++) {
  64.             displaybuffer[i]=0;
  65.         }
  66.         WriteDisplay();
  67.     }
  68.     //% blockId="iLED4_Turn_ON" block="iLED4 Turn ON"
  69.     //% weight=35 blockGap=8
  70.     export function Turn_ON() {
  71.         sendCommand(0x81);
  72.     }
  73.     //% blockId="iLED4_Turn_OFF" block="iLED4 Turn OFF"
  74.     //% weight=34 blockGap=8
  75.     export function Turn_OFF() {
  76.         sendCommand(0x80);
  77.     }
  78.     /**
  79.     * set iLED4 Colon : , True or False.
  80.     * @param set Colon for iLED4, eg: false
  81.     */
  82.     //% blockId="iLED4_Colon" block="iLED4 Colon %status"
  83.     //% weight=70 blockGap=8
  84.     export function colon(status:boolean) {
  85.         if (status) { displaybuffer[4] = 0x01; }
  86.         else        { displaybuffer[4] = 0x00; }
  87.         WriteDisplay();
  88.     }
  89.  
  90.     //% blockId="iLED4_ShowDot" block="iLED4 Dotpoint at %digit show %status"
  91.     //% weight=60 blockGap=8
  92.     //% digit.min=0 digit.max=4
  93.     export function ShowDot(digit:number,status: boolean) {
  94.         if (digit==4){colon(status);}
  95.         if (status) { displaybuffer[digit] = displaybuffer[digit] | 0x80; }
  96.         else { displaybuffer[digit] = displaybuffer[digit] & 0x7F; }
  97.         WriteDisplay();
  98.     }
  99.  
  100.     //% blockId="iLED4_Write_Digit_Dot" block="iLED4 Show Number %num at %digit dot %dot"
  101.     //% weight=75 blockGap=8
  102.     //% digit.min=0 digit.max=3
  103.     //% num.min=0 num.max=15
  104.     export function WriteDigitNum(num:number,digit:number,dot:boolean){
  105.         if (dot) {
  106.             displaybuffer[digit]=segment[num] | 0x80;
  107.         }
  108.         else{
  109.             displaybuffer[digit] = segment[num] ;
  110.         }
  111.         WriteDisplay();
  112.     }
  113.     //% blockId="iLED4_Show_Number" block="iLED4 Show Number %num"
  114.     //% weight=85 blockGap=8
  115.     export function ShowNumber(num: number) {
  116.         if (num<0){
  117.             displaybuffer[0]=0x40;
  118.             num=-num;
  119.         }
  120.         else{
  121.             displaybuffer[0]=segment[Math.idiv(num,1000)%10];
  122.         }
  123.             displaybuffer[1]=segment[Math.idiv(num,100) % 10];
  124.             displaybuffer[2]=segment[Math.idiv(num,10) % 10];
  125.             displaybuffer[3]=segment[num % 10];
  126.         WriteDisplay();
  127.     }
  128.     //% blockId="iLED4_Show_Hex_Number" block="iLED4 Show Hex Number %num"
  129.     //% weight=80 blockGap=8
  130.     export function ShowHexNumber(num: number) {
  131.         if (num < 0) {
  132.             displaybuffer[0] = 0x40;
  133.             num = -num;
  134.         }
  135.         else {
  136.             displaybuffer[0] = segment[(num>>12) % 16];
  137.         }
  138.         displaybuffer[1] = segment[(num>>8) % 16];
  139.         displaybuffer[2] = segment[(num>>4) % 16];
  140.         displaybuffer[3] = segment[num % 16];
  141.         WriteDisplay();
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement