optimize blocks

1. Fix bug of display clear;
2. Add set display brightness level effective immediately;
3. Add limit display vlaue when use display bit show;
4. Add ultrasonic measured interval.
This commit is contained in:
jinyuan.weng 2017-10-23 10:23:08 +08:00
parent 759cc8c0a8
commit b0b23af91d

101
main.ts
View File

@ -69,7 +69,7 @@ namespace grove {
const gestureEventId = 3100; const gestureEventId = 3100;
let lastGesture = GroveGesture.None; let lastGesture = GroveGesture.None;
let paj7620: PAJ7620 = undefined; let paj7620: PAJ7620 = undefined;
let data: number = 0; let distanceBackup: number = 0;
/** /**
* Do something when a gesture is detected by Grove - Gesture * Do something when a gesture is detected by Grove - Gesture
* @param gesture type of gesture to detect * @param gesture type of gesture to detect
@ -102,32 +102,21 @@ namespace grove {
{ {
let duration = 0; let duration = 0;
let RangeInCentimeters = 0; let RangeInCentimeters = 0;
let buf: number [] = [0, 0, 0, 0, 0];
let num = 0;
for(let i = 0; i < 5; i ++)
{
pins.digitalWritePin(pin, 0); pins.digitalWritePin(pin, 0);
control.waitMicros(2); control.waitMicros(2);
pins.digitalWritePin(pin, 1); pins.digitalWritePin(pin, 1);
control.waitMicros(5); control.waitMicros(20);
pins.digitalWritePin(pin, 0); pins.digitalWritePin(pin, 0);
duration = pins.pulseIn(pin, PulseValue.High, 50000); // Max duration 50 ms
buf[i] = pins.pulseIn(pin, PulseValue.High, 100000); // Max duration 100 ms
}
for(let i = 0; i < 5; i ++)
{
if(buf[i] != 0)
{
duration = duration + buf[i];
num ++;
}
}
duration = duration / num;
RangeInCentimeters = duration * 153 / 29 / 2 / 100; RangeInCentimeters = duration * 153 / 29 / 2 / 100;
if(RangeInCentimeters > 0) distanceBackup = RangeInCentimeters;
else RangeInCentimeters = distanceBackup;
basic.pause(50);
return RangeInCentimeters; return RangeInCentimeters;
} }
@ -139,34 +128,23 @@ namespace grove {
export function measureInInches(pin: DigitalPin): number export function measureInInches(pin: DigitalPin): number
{ {
let duration = 0; let duration = 0;
let RangeInCentimeters = 0; let RangeInInches = 0;
let buf: number [] = [0, 0, 0, 0, 0];
let num = 0;
for(let i = 0; i < 5; i ++)
{
pins.digitalWritePin(pin, 0); pins.digitalWritePin(pin, 0);
control.waitMicros(2); control.waitMicros(2);
pins.digitalWritePin(pin, 1); pins.digitalWritePin(pin, 1);
control.waitMicros(5); control.waitMicros(20);
pins.digitalWritePin(pin, 0); pins.digitalWritePin(pin, 0);
duration = pins.pulseIn(pin, PulseValue.High, 100000); // Max duration 100 ms
buf[i] = pins.pulseIn(pin, PulseValue.High, 100000); // Max duration 100 ms RangeInInches = duration * 153 / 74 / 2 / 100;
}
for(let i = 0; i < 5; i ++) if(RangeInInches > 0) distanceBackup = RangeInInches;
{ else RangeInInches = distanceBackup;
if(buf[i] != 0)
{
duration = duration + buf[i];
num ++;
}
}
duration = duration / num; basic.pause(50);
RangeInCentimeters = duration * 153 / 74 / 2 / 100;
return RangeInCentimeters; return RangeInInches;
} }
/** /**
@ -179,10 +157,12 @@ namespace grove {
{ {
let display = new TM1637(); let display = new TM1637();
display.buf = pins.createBuffer(4);
display.clkPin = clkPin; display.clkPin = clkPin;
display.dataPin = dataPin; display.dataPin = dataPin;
display.brightnessLevel = 0; display.brightnessLevel = 0;
display.pointFlag = false; display.pointFlag = false;
display.clear();
return display; return display;
} }
@ -304,6 +284,7 @@ namespace grove {
dataPin: DigitalPin; dataPin: DigitalPin;
brightnessLevel: number; brightnessLevel: number;
pointFlag: boolean; pointFlag: boolean;
buf: Buffer;
private writeByte(wrData: number) private writeByte(wrData: number)
{ {
@ -363,6 +344,11 @@ namespace grove {
this.bit(0x7f, 2); this.bit(0x7f, 2);
this.bit(0x7f, 1); this.bit(0x7f, 1);
this.bit(0x7f, 0); this.bit(0x7f, 0);
this.buf[3] = dispData;
this.buf[2] = 0x7f;
this.buf[1] = 0x7f;
this.buf[0] = 0x7f;
} }
else if(dispData < 100) else if(dispData < 100)
{ {
@ -370,6 +356,11 @@ namespace grove {
this.bit((dispData / 10) % 10, 2); this.bit((dispData / 10) % 10, 2);
this.bit(0x7f, 1); this.bit(0x7f, 1);
this.bit(0x7f, 0); this.bit(0x7f, 0);
this.buf[3] = dispData % 10;
this.buf[2] = (dispData / 10) % 10;
this.buf[1] = 0x7f;
this.buf[0] = 0x7f;
} }
else if(dispData < 1000) else if(dispData < 1000)
{ {
@ -377,6 +368,11 @@ namespace grove {
this.bit((dispData / 10) % 10, 2); this.bit((dispData / 10) % 10, 2);
this.bit((dispData / 100) % 10, 1); this.bit((dispData / 100) % 10, 1);
this.bit(0x7f, 0); this.bit(0x7f, 0);
this.buf[3] = dispData % 10;
this.buf[2] = (dispData / 10) % 10;
this.buf[1] = (dispData / 100) % 10;
this.buf[0] = 0x7f;
} }
else else
{ {
@ -384,6 +380,11 @@ namespace grove {
this.bit((dispData / 10) % 10, 2); this.bit((dispData / 10) % 10, 2);
this.bit((dispData / 100) % 10, 1); this.bit((dispData / 100) % 10, 1);
this.bit((dispData / 1000) % 10, 0); this.bit((dispData / 1000) % 10, 0);
this.buf[3] = dispData % 10;
this.buf[2] = (dispData / 10) % 10;
this.buf[1] = (dispData / 100) % 10;
this.buf[0] = (dispData / 1000) % 10;
} }
} }
@ -396,6 +397,11 @@ namespace grove {
set(level: number) set(level: number)
{ {
this.brightnessLevel = level; this.brightnessLevel = level;
this.bit(this.buf[0], 0x00);
this.bit(this.buf[1], 0x01);
this.bit(this.buf[2], 0x02);
this.bit(this.buf[3], 0x03);
} }
/** /**
@ -408,8 +414,11 @@ namespace grove {
//% bitAddr.min=0 bitAddr.max=3 //% bitAddr.min=0 bitAddr.max=3
//% advanced=true //% advanced=true
bit(dispData: number, bitAddr: number) bit(dispData: number, bitAddr: number)
{
if((dispData == 0x7f) || ((dispData <= 9) && (bitAddr <= 3)))
{ {
let segData = 0; let segData = 0;
segData = this.coding(dispData); segData = this.coding(dispData);
this.start(); this.start();
this.writeByte(0x44); this.writeByte(0x44);
@ -421,6 +430,9 @@ namespace grove {
this.start(); this.start();
this.writeByte(0x88 + this.brightnessLevel); this.writeByte(0x88 + this.brightnessLevel);
this.stop(); this.stop();
this.buf[bitAddr] = dispData;
}
} }
/** /**
@ -432,6 +444,11 @@ namespace grove {
point(point: boolean) point(point: boolean)
{ {
this.pointFlag = point; this.pointFlag = point;
this.bit(this.buf[0], 0x00);
this.bit(this.buf[1], 0x01);
this.bit(this.buf[2], 0x02);
this.bit(this.buf[3], 0x03);
} }
/** /**
@ -441,10 +458,10 @@ namespace grove {
//% advanced=true //% advanced=true
clear() clear()
{ {
this.bit(0x00, 0x7f); this.bit(0x7f, 0x00);
this.bit(0x01, 0x7f); this.bit(0x7f, 0x01);
this.bit(0x02, 0x7f); this.bit(0x7f, 0x02);
this.bit(0x03, 0x7f); this.bit(0x7f, 0x03);
} }
} }
} }