KRITSADA

iKB Extension for microbit old version

Aug 27th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 5.03 KB | Source Code | 0 0
  1. /**
  2.  * ใช้ไฟล์นี้สำหรับการสร้างบล็อกพิเศษขึ้นมาในโปรแกรมของเรา
  3.  * ดูรายละเอียดเพิ่มเติมได้ที่ /blocks/custom
  4.  */
  5. /**
  6.   * Enumeration of iKBADC.
  7.   */
  8. enum iKBADC {
  9.     //% block="ADC 0"
  10.     ADC0 = 0x00,
  11.     //% block="ADC 1"
  12.     ADC1 = 0x10,
  13.     //% block="ADC 2"
  14.     ADC2 = 0x20,
  15.     //% block="ADC 3"
  16.     ADC3 = 0x30,
  17.     //% block="ADC 4"
  18.     ADC4 = 0x40,
  19.     //% block="ADC 5"
  20.     ADC5 = 0x50,
  21.     //% block="ADC 6"
  22.     ADC6 = 0x60,
  23.     //% block="ADC 7"
  24.     ADC7 = 0x70
  25. }
  26.  
  27. enum pinx {
  28.     //% block="0"
  29.     D0,
  30.     //% block="1"
  31.     D1,
  32.     //% block="2"
  33.     D2,
  34.     //% block="3"
  35.     D3,
  36.     //% block="4"
  37.     D4,
  38.     //% block="5"
  39.     D5,
  40.     //% block="6"
  41.     D6,
  42.     //% block="7"
  43.     D7
  44. }
  45. enum st {
  46.     //% block="0"
  47.     OFF,
  48.     //% block="1"
  49.     ON
  50. }
  51. enum sv {
  52.     //% block="10"
  53.     SV10,
  54.     //% block="11"
  55.     SV11,
  56.     //% block="12"
  57.     SV12,
  58.     //% block="13"
  59.     SV13,
  60.     //% block="14"
  61.     SV14,
  62.     //% block="15"
  63.     SV15
  64. }
  65. /**
  66.  * Custom blocks
  67.  */
  68. //% weight=1 color=#31a751 icon="\uf085"
  69. namespace iKB {
  70.     /**
  71.      * ฟังก์ชั่นสำหรับการติดต่อกับบอร์ด iKB
  72.      * @param n describe parameter here, eg: 5
  73.      * @param s describe parameter here, eg: "Hello"
  74.      * @param e describe parameter here
  75.      */
  76.  
  77.     //% blockId="out" block="OUT pin %pinx| to %st "
  78.     //% weight=75
  79.     export function out(p: pinx, st: st): void {
  80.         pins.i2cWriteNumber(72, ((8 + p) * 256) + st, NumberFormat.UInt16BE, false)
  81.     }
  82.  
  83.     //% blockId="sv" block="Servo CH %sv | speed %Speed "
  84.     //% Speed.min=-1  Speed.max=200
  85.     //% weight=75
  86.     export function servo(CH: sv, Speed: number): void {
  87.         pins.i2cWriteNumber(72, (0x23 * 256) + Speed, NumberFormat.UInt16BE, false)
  88.     }
  89.  
  90.     //% blockId="in" block="in pin %pinx "
  91.     //% weight=50
  92.     export function In(p: pinx): number {
  93.         pins.i2cWriteNumber(72, ((8 + p) * 256) + 2, NumberFormat.UInt16BE, false)
  94.         return pins.i2cReadNumber(72, NumberFormat.UInt8BE, false)
  95.     }
  96.     //% blockId="in_p" block="in pullup pin %pinx "
  97.     //% weight=50
  98.     export function In_p(p: pinx): number {
  99.         pins.i2cWriteNumber(72, ((8 + p) * 256) + 3, NumberFormat.UInt16BE, false)
  100.         return pins.i2cReadNumber(72, NumberFormat.UInt8BE, false)
  101.     }
  102.  
  103.     /**ReadADC for read analog sensor, Select ADC channel 0-7.
  104.          *
  105.          */
  106.     //% blockId="iKBADC" block="Read %iKBADC"
  107.     //% weight=75
  108.     export function iKBADC(ADC_CH: iKBADC): number {
  109.         let ADCValue: number;
  110.         pins.i2cWriteNumber(72, 0x80 + ADC_CH, NumberFormat.UInt8LE, false)
  111.         return pins.i2cReadNumber(72, NumberFormat.UInt16BE, false)
  112.     }
  113.     //% blockId="IKB_reset" block="iKB Reset"
  114.     //% weight=90
  115.     export function iKB_Reset(): void {
  116.         pins.i2cWriteNumber(72, 0, NumberFormat.UInt8BE, false)
  117.     }
  118.  
  119.  
  120.  
  121.     //% blockId="fd" block="FD speed %Speed "
  122.     //% Speed.min=0  Speed.max=100
  123.     //% weight=75
  124.     export function fd(Speed: number): void {
  125.         pins.i2cWriteNumber(72, (0x23 * 256) + Speed, NumberFormat.UInt16BE, false)
  126.     }
  127.  
  128.     //% blockId="bk" block="BK speed %Speed "
  129.     //% Speed.min=0  Speed.max=100
  130.     //% weight=74
  131.     export function bk(Speed: number): void {
  132.         pins.i2cWriteNumber(72, (0x23 * 256) + (256 - Speed), NumberFormat.UInt16BE, false)
  133.     }
  134.  
  135.     //% blockId="sl" block="SL speed %Speed "
  136.     //% Speed.min=0  Speed.max=100
  137.     //% weight=73
  138.     export function sl(Speed: number): void {
  139.         pins.i2cWriteNumber(72, (0x21 * 256) + (Speed), NumberFormat.UInt16BE, false)
  140.         pins.i2cWriteNumber(72, (0x22 * 256) + (256 - Speed), NumberFormat.UInt16BE, false)
  141.     }
  142.  
  143.     //% blockId="sr" block="SR speed %Speed "
  144.     //% Speed.min=0  Speed.max=100
  145.     //% weight=72
  146.     export function sr(Speed: number): void {
  147.         pins.i2cWriteNumber(72, (0x22 * 256) + (Speed), NumberFormat.UInt16BE, false)
  148.         pins.i2cWriteNumber(72, (0x21 * 256) + (256 - Speed), NumberFormat.UInt16BE, false)
  149.     }
  150.  
  151.     //% blockId="tl" block="TL speed %Speed "
  152.     //% Speed.min=0  Speed.max=100
  153.     //% weight=71
  154.     export function tl(Speed: number): void {
  155.         pins.i2cWriteNumber(72, (0x22 * 256) + (Speed), NumberFormat.UInt16BE, false)
  156.         pins.i2cWriteNumber(72, (0x21 * 256) + (0), NumberFormat.UInt16BE, false)
  157.     }
  158.  
  159.     //% blockId="tr" block="TR speed %Speed "
  160.     //% Speed.min=0  Speed.max=100
  161.     //% weight=70
  162.     export function tr(Speed: number): void {
  163.         pins.i2cWriteNumber(72, (0x21 * 256) + (Speed), NumberFormat.UInt16BE, false)
  164.         pins.i2cWriteNumber(72, (0x22 * 256) + (0), NumberFormat.UInt16BE, false)
  165.     }
  166.  
  167.     //% blockId="ao" block="Motor Stop "
  168.     //% weight=69
  169.     export function ao(): void {
  170.         pins.i2cWriteNumber(72, (0x23 * 256) + (0), NumberFormat.UInt16BE, false)
  171.     }
  172.  
  173. }
Add Comment
Please, Sign In to add comment