I'm trying to lip sync a 3d model using BabylonJS but not all lip positions are happening at the time their supposed to - babylonjs

I'm trying to lip sync a 3D model using babylonJS, morph targets, and audio obtained from Amazon Polly but the lip positions aren't happening at the exact time their supposed to (for example when the speech starts the lip positions start as well but not all the lip positions occur in sync with the sound or the speech ends before the lip positions end.) I tried creating a time counter variable called alpha and tried to increase the amount it's incremented by but there are times when it skips some of the lip position occurrences. The time (in milliseconds) that I want the lip positions to occur are shown in the variable jsonParsed. Does anyone know of a way to fix this. If you'd like to see my source code I've pasted it below.
var createScene = function () {
// This creates a basic Babylon Scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
// This creates and positions a free camera (non-mesh)
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
url = 'https://raw.githubusercontent.com/chris45242/BabylonModel/main/';
fileName = "project.blend1.gltf";
const Casi = BABYLON.SceneLoader.Append(url, fileName, scene, function (s){
// Create a default arc rotate camera and light.
scene.createDefaultCameraOrLight(true, true, true);
scene.activeCamera.alpha += Math.PI;
scene.stopAllAnimations();
//scene.animationGroups[0].start(true);
scene.animationGroups[1].start(true);
var casiVisor = scene.getMeshByName("Casi's Visor");
casiVisor.setEnabled(true);
casiVisor.visibility = 0.7;
var casiBody = scene.getMeshByName("Casi's Body.001_primitive0");
var casiInnerMouth = scene.getMeshByName("Casi's Body.001_primitive2");
var casiTeeth = scene.getMeshByName("Casi's Teeth");
var primitive = scene.getMeshByName("Primitives.001");
var jsonParsed = //'[{"time":6,"type":"word","start":0,"end":4,"value":"it\'s"},'+
'[{"time":6,"type":"viseme","value":"i"},' +
'{"time":64,"type":"viseme","value":"t"},' +
'{"time":122,"type":"viseme","value":"s"},' +
//'{"time":190,"type":"word","start":5,"end":9,"value":"very"},' +
'{"time":190,"type":"viseme","value":"f"},' +
'{"time":241,"type":"viseme","value":"E"},' +
'{"time":282,"type":"viseme","value":"r"},' +
'{"time":376,"type":"viseme","value":"i"},' +
//'{"time":416,"type":"word","start":10,"end":14,"value":"good"},' +
'{"time":416,"type":"viseme","value":"k"},' +
'{"time":481,"type":"viseme","value":"u"},' +
'{"time":576,"type":"viseme","value":"t"},' +
//'{"time":627,"type":"word","start":15,"end":17,"value":"to"},' +
'{"time":627,"type":"viseme","value":"t"},' +
'{"time":674,"type":"viseme","value":"u"},' +
//'{"time":716,"type":"word","start":18,"end":22,"value":"meet"},' +
'{"time":716,"type":"viseme","value":"p"},' +
'{"time":801,"type":"viseme","value":"i"},' +
'{"time":923,"type":"viseme","value":"t"},' +
//'{"time":999,"type":"word","start":23,"end":26,"value":"you"},' +
'{"time":999,"type":"viseme","value":"i"},' +
'{"time":1135,"type":"viseme","value":"u"},' +
'{"time":1357,"type":"viseme","value":"sil"}]';
const obj = JSON.parse(jsonParsed);
let t = 0;
//const lipSync = 'a';
const lipSync = obj[t]['value'];
const vowelInput = {
value: lipSync,
}
// Create custom observable for the 'vowelInput'
vowelInput.onValueChange = new BABYLON.Observable();
// Create function to set current vowel
const setCurrentVowel = (value) => {
if (value === vowelInput.value) {
return value
} else {
vowelInput.value = value
console.log("test", value, vowelInput.value)
vowelInput.onValueChange.notifyObservers(vowelInput.value)
}
}
// Add callback function to the custom observable
vowelInput.onValueChange.add((value) => {
switch (value) {
case 'a': {
// Instead of changing position, you can all lip animations according to the vowel.
casiBody.morphTargetManager.getTarget(12).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 1;
primitive.morphTargetManager.getTarget(3).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'e':
case 'E': {
casiBody.morphTargetManager.getTarget(14).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 1;
casiTeeth.morphTargetManager.getTarget(0).influence = 1;
primitive.morphTargetManager.getTarget(5).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'o':
case 'r': {
casiBody.morphTargetManager.getTarget(11).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 1;
primitive.morphTargetManager.getTarget(2).influence = 1;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'u': {
casiBody.morphTargetManager.getTarget(13).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 1;
primitive.morphTargetManager.getTarget(4).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'i': {
casiBody.morphTargetManager.getTarget(15).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 1;
primitive.morphTargetManager.getTarget(6).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'f':
case 'v': {
casiBody.morphTargetManager.getTarget(16).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 1;
casiTeeth.morphTargetManager.getTarget(1).influence = 1;
primitive.morphTargetManager.getTarget(7).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
break
}
}
})
let idx = 0
const vowels = ['a', 'e', 'o', 'u', 'i', 'f']
var soundURL = "speech.mp3";
//alpha is the timer I use
let alpha = 0;
// Change current vowel repeatedly
var speech = new BABYLON.Sound("speech", url + soundURL, scene, function(){
setTimeout(speech.play(), 10000);
});
//const inter = setInterval(() => {
var intervalID = scene.onBeforeRenderObservable.add(function(){
alpha += 1;//continuously add 1 to the timer
if(alpha === obj[t]['time']){//if we've reached a time shown in jsonParsed array
console.log(alpha, t, obj[t]['time']);
setCurrentVowel(obj[t]['value']);
t = (t + 1) % obj.length;
console.log(alpha, t, obj[t]['time']);
}
/*setTimeout(() => {
setInterval(inter, obj[t]['time']);
});*/
//setCurrentVowel(obj[t]['value']);
//t = (t + 1) % obj.length;
//idx = (idx + 1) % 6
});
//}, 1000)
/*function inter(){
setCurrentVowel(obj[t]['value']);
}*/
scene.onDisposeObservable.add(() => {
clearInterval(inter)
})
});
// Our built-in 'sphere' shape.
//var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
// Move the sphere upward 1/2 its height
//sphere.position.y = 1;
// Our built-in 'ground' shape.
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 6, height: 6}, scene);
return scene;
};

Related

Why is my serial monitor showing the binary value updating every other cycle where as the decimal is every cycle? - Arduino

Project Context:
This is code I wrote for my arduino to try and create a binary clock, where I create a 24 length array of boleans to define a binary clock.
the array is broken down like this:
00|0000|000|0000|000|0000
H1| H2 | M1| M2 | S1| S2
I intent to use 3 8 Bit shift registers to control the output LEDs, so this could be broken up 1 nibble per digit however I did not do that......not sure why.
TLDR:
I made this shit code, it is currently only incrememnting the binary value that's printed every other cycle and the decimal every cycle, would anyone be able to help me figure out why?
`
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
int h1 = 0;
int h2 = 0;
int m1 = 0;
int m2 = 0;
int s1 = 0;
int s2 = -1;
while(0 == 0)
{
// put your main code here, to run repeatedly:
s2 = s2 + 1;
if (s2 == 10)
{
s2 = 0;
s1 = s1 + 1;
if ( s1 == 6)
{
s1 = 0;
m2 = m2 + 1;
if ( m2 == 10)
{
m2 = 0;
m1 = m1 + 1;
if (m1 == 6)
{
m1 = 0;
h2 = h2 + 1;
if ( h2 == 10 )
{
h2 = 0;
h1 = h1 + 1;
}
else if ((h2 == 5) && (h1 == 2))
{
h1 = 0;
h2 = 0;
}
}
}
}
}
stringSetter(h1,h2,m1,m2,s1,s2);
delay(1000);
}
}
void stringSetter(int h1, int h2, int m1, int m2, int s1, int s2)
{
//h1 - converting to boolean array
boolean h1b[2];
if ( h1 == 0 )
{
h1b[0] = 0;
h1b[1] = 0;
}
else if ( h1 == 1 )
{
h1b[0] = 1;
h1b[1] = 0;
}
else if ( h1 == 2 )
{
h1b[0] = 0;
h1b[1] = 1;
}
//h2 - converting to boolean array
boolean h2b[4];
if ( h2 == 0 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 1 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 2)
{
h2b[0] = 0;
h2b[1] = 1;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 3 )
{
h2b[0] = 1;
h2b[1] = 1;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 4 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 5 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 6 )
{
h2b[0] = 0;
h2b[1] = 1;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 7 )
{
h2b[0] = 1;
h2b[1] = 1;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 8 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 1;
}
else if ( h2 == 9 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 1;
}
//m1 - converting to boolean array
boolean m1b[3];
if ( m1 == 0 )
{
m1b[0] = 0;
m1b[1] = 0;
m1b[2] = 0;
}
else if ( m1 == 1 )
{
m1b[0] = 1;
m1b[1] = 0;
m1b[2] = 0;
}
else if ( m1 == 2)
{
m1b[0] = 0;
m1b[1] = 1;
m1b[2] = 0;
}
else if ( m1 == 3 )
{
m1b[0] = 1;
m1b[1] = 1;
m1b[2] = 0;
}
else if ( m1 == 4 )
{
m1b[0] = 0;
m1b[1] = 0;
m1b[2] = 1;
}
else if ( m1 == 5 )
{
m1b[0] = 1;
m1b[1] = 0;
m1b[2] = 1;
}
//m2 - converting to boolean array
boolean m2b[4];
if ( m2 == 0 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 1 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 2)
{
m2b[0] = 0;
m2b[1] = 1;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 3 )
{
m2b[0] = 1;
m2b[1] = 1;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 4 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 5 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 6 )
{
m2b[0] = 0;
m2b[1] = 1;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 7 )
{
m2b[0] = 1;
m2b[1] = 1;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 8 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 1;
}
else if ( m2 == 9 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 1;
}
//s1 - converting to boolean array
boolean s1b[3];
if ( s1 == 0 )
{
s1b[0] = 0;
s1b[1] = 0;
s1b[2] = 0;
}
else if ( s1 == 1 )
{
s1b[0] = 1;
s1b[1] = 0;
s1b[2] = 0;
}
else if ( s1 == 2 )
{
s1b[0] = 0;
s1b[1] = 1;
s1b[2] = 0;
}
else if ( s1 == 3 )
{
s1b[0] = 1;
s1b[1] = 1;
s1b[2] = 0;
}
else if ( s1 == 4 )
{
s1b[0] = 0;
s1b[1] = 0;
s1b[2] = 1;
}
else if ( s1 == 5 )
{
s1b[0] = 1;
s1b[1] = 0;
s1b[2] = 1;
}
//s2 - converting to boolean array
boolean s2b[4];
if ( s2 == 0 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 1 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 2)
{
s2b[0] = 0;
s2b[1] = 1;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 3 )
{
s2b[0] = 1;
s2b[1] = 1;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 4 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 5 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 6 )
{
s2b[0] = 0;
s2b[1] = 1;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 7 )
{
s2b[0] = 1;
s2b[1] = 1;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 8 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 1;
}
else if ( s2 == 9 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 1;
}
Serial.print('H');
Serial.print(h1);
Serial.print(h2);
Serial.print('M');
Serial.print(m1);
Serial.print(m2);
Serial.print('S');
Serial.print(s1);
Serial.print(s2);
Serial.println();
// setting up pin booleans
boolean combiBi[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
combiBi[23] = 0;
combiBi[22] = 0;
combiBi[21] = 0;
combiBi[20] = 0;
combiBi[19] = h1b[1];
combiBi[18] = h1b[0];
combiBi[17] = h2b[3];
combiBi[16] = h2b[2];
combiBi[15] = h2b[1];
combiBi[14] = h2b[0];
combiBi[13] = m1b[2];
combiBi[12] = m1b[1];
combiBi[11] = m1b[0];
combiBi[10] = m2b[3];
combiBi[9] = m2b[2];
combiBi[8] = m2b[1];
combiBi[7] = m2b[0];
combiBi[6] = s1b[2];
combiBi[5] = s1b[1];
combiBi[4] = s1b[0];
combiBi[3] = s2b[3];
combiBi[2] = s2b[2];
combiBi[1] = s2b[1];
combiBi[0] = s2b[0];
// printing out binary for pins
for(int o = 23; o > 0; o--){
if ( o == 19 )
{
Serial.print('H');
}
else if (o == 13)
{
Serial.print('M');
}
else if (o == 6)
{
Serial.print('S');
}
Serial.print(combiBi[o]);
}
Serial.println();
}
`
Serial Monitor Output
I tried to get a decimal and binary equivilant output printed in the serial monitor, however the binary is only updated on every other cycle while the decimal performs as expect.
for(int o = 23; o > 0; o--)
In this loop o will never reach 0. Hence you'll never print your last bit which changes every second.
Change your run condition to o >= 0.
As already mentioned in my comments you should really learn more about digital numbers and their representation in memory. You don't have to explicitly implement every single bit combination by hand using arrays.
This is a computer, let it compute.

How can I remove the white-space around the numbers on a JS timer?

I have a JavaScript timer here, and I am trying to remove the white-space around the numbers so that the entire timer is green.
I have tried using CSS to remove the white-space but have had no luck. I am unsure whether CSS or JS has to be used to remove the space.
This is the code for the timer
An image can be found here
I just want to remove the white-space around the numbers. Thanks
var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = '';
var output;
var clock;
var thetable = document.getElementById("timerTable");
// Adjust limits and colors here
// Specify limits in seconds
var limit1 = 0;
color1 = "lightgreen";
var limit2 = 360;
color2 = "orange";
var limit3 = 600;
color3 = "red";
function startstop() {
var startdate = new Date();
var starttime = startdate.getTime();
if (flagclock == 0) {
startstop.value = 'Stop';
flagclock = 1;
counter(starttime);
} else {
startstop.value = 'Start';
flagclock = 0;
flagstop = 1;
splitdate = '';
}
}
function counter(starttime) {
output = document.getElementById('output');
clock = document.getElementById('clock');
currenttime = new Date();
var timediff = currenttime.getTime() - starttime;
if (flagstop == 1) {
timediff = timediff + stoptime
}
if (flagclock == 1) {
clock.value = formattime(timediff, '');
refresh = setTimeout('counter(' + starttime + ');', 100);
var secs = timediff / 1000;
var thecolor = "white";
if (secs > limit3) thecolor = color3;
else if (secs > limit2) thecolor = color2;
else if (secs > limit1) thecolor = color1;
thetable.style.backgroundColor = thecolor;
console.log(timediff / 1000)
} else {
window.clearTimeout(refresh);
stoptime = timediff;
}
}
function formattime(rawtime, roundtype) {
if (roundtype == 'round') {
var ds = Math.round(rawtime / 100) + '';
} else {
var ds = Math.floor(rawtime / 100) + '';
}
var sec = Math.floor(rawtime / 1000);
var min = Math.floor(rawtime / 60000);
ds = ds.charAt(ds.length - 1);
if (min >= 15) {
startstop();
}
sec = sec - 60 * min + '';
if (sec.charAt(sec.length - 2) != '') {
sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
} else {
sec = 0 + sec.charAt(sec.length - 1);
}
min = min + '';
if (min.charAt(min.length - 2) != '') {
min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
} else {
min = 0 + min.charAt(min.length - 1);
}
return min + ':' + sec;
}
<body onload="startstop();">
<table id="timerTable">
<tr>
<td>
<input class="timerArea" id="clock" type="text" value="00:00" style="text-align:center;font-weight:bold;font-size:14pt;" readonly><br>
</td>
</tr>
</table>
</body>
In order to make the entire timer green, you can set the CSS property background-color of the <input> element to "transparent". I've also removed the element's default border.
.timerArea {
text-align: center;
font-weight: bold;
font-size: 14pt;
background-color: transparent;
border: none;
}
Here's a demonstration:
var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = '';
var output;
var clock;
var thetable = document.getElementById("timerTable");
// Adjust limits and colors here
// Specify limits in seconds
var limit1 = 0;
color1 = "lightgreen";
var limit2 = 360;
color2 = "orange";
var limit3 = 600;
color3 = "red";
function startstop() {
var startdate = new Date();
var starttime = startdate.getTime();
if (flagclock == 0) {
startstop.value = 'Stop';
flagclock = 1;
counter(starttime);
} else {
startstop.value = 'Start';
flagclock = 0;
flagstop = 1;
splitdate = '';
}
}
function counter(starttime) {
output = document.getElementById('output');
clock = document.getElementById('clock');
currenttime = new Date();
var timediff = currenttime.getTime() - starttime;
if (flagstop == 1) {
timediff = timediff + stoptime
}
if (flagclock == 1) {
clock.value = formattime(timediff, '');
refresh = setTimeout('counter(' + starttime + ');', 100);
var secs = timediff / 1000;
var thecolor = "white";
if (secs > limit3) thecolor = color3;
else if (secs > limit2) thecolor = color2;
else if (secs > limit1) thecolor = color1;
thetable.style.backgroundColor = thecolor;
console.log(timediff / 1000)
} else {
window.clearTimeout(refresh);
stoptime = timediff;
}
}
function formattime(rawtime, roundtype) {
if (roundtype == 'round') {
var ds = Math.round(rawtime / 100) + '';
} else {
var ds = Math.floor(rawtime / 100) + '';
}
var sec = Math.floor(rawtime / 1000);
var min = Math.floor(rawtime / 60000);
ds = ds.charAt(ds.length - 1);
if (min >= 15) {
startstop();
}
sec = sec - 60 * min + '';
if (sec.charAt(sec.length - 2) != '') {
sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
} else {
sec = 0 + sec.charAt(sec.length - 1);
}
min = min + '';
if (min.charAt(min.length - 2) != '') {
min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
} else {
min = 0 + min.charAt(min.length - 1);
}
return min + ':' + sec;
}
startstop();
.timerArea {
text-align: center;
font-weight: bold;
font-size: 14pt;
background-color: transparent;
border: none;
}
<table id="timerTable">
<tr>
<td>
<input class="timerArea" id="clock" type="text" value="00:00" readonly><br>
</td>
</tr>
</table>

Control 4 digits 7-segment LED interfacing MCU 8051

I have some error while reading the voltage displayed on 7-segment LED. The PCB was designed by another person. They didn't assign the 7-segment LED pins to the same port on MCU.
Herein, I assume the MCU code is correct. But the LED displayed wrong voltage value. I have a question first. Can we connect 7-segment LED pins to 2 ports of MCU (microcontroller)? Such as segment a~d connect to MCU pins P1.0~P1.3, segment e~h connect to pins P2.0~P2.3. Or only connect to the same port, e.g. LED segment a~h connect to MCU pins P1.0~P1.7?
Here's the code of pin addressing and LED control:
// PORT 1
//--------------------------------------
sbit UP_BUTTON = P1^0;
sbit DOWN_BUTTON = P1^1;
sbit LEFT_BUTTON = P1^2;
sbit RIGHT_BUTTON = P1^3;
sbit ENTER_BUTTON = P1^4;
sbit _7SEG_COM5 = P1^5;
sbit _7SEG_COM4 = P1^6;
sbit _7SEG_COM3 = P1^7;
//--------------------------------------
// PORT 2
//--------------------------------------
sbit _7SEG_F = P2^0;
sbit _7SEG_G = P2^1;
sbit _7SEG_H = P2^2;
sbit _7SEG_COM0 = P2^3;
sbit _7SEG_COM1 = P2^4;
sbit _7SEG_COM2 = P2^5;
sbit B_RST = P2^6; //DATA B RST
sbit B_RSTSEL = P2^7; //DATA B RSTSEL
//--------------------------------------
// PORT 3
//--------------------------------------
sbit THREE_ZERO = P3^0;
sbit THREE_ONE = P3^1;
sbit READY = P3^2;
sbit _7SEG_A = P3^3;
sbit _7SEG_B = P3^4;
sbit _7SEG_C = P3^5;
sbit _7SEG_D = P3^6;
sbit _7SEG_E = P3^7;
sbit Sinitial_f = Flag0^1;
unsigned char idata SEG[6] _at_ 0x82;
// 7 segment display in addition,3rd number(SEG2) is dot always
//=======================================================================================
void SEGMENT_DISPLAY(void)
{
unsigned char SFRPAGE_BUF,seg_num,seg_value;
int cnt, i, num, temp;
SFRPAGE_BUF = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
if(!Sinitial_f)
{
SEG[0] = 0x0A;
SEG[1] = 0x0A;
SEG[2] = 0x0A;
SEG[3] = 0x0A;
SEG[4] = 0x0A;
SEG[5] = 0x0A;
Sinitial_f = 1;
}
for(seg_num=0x00;seg_num<=0x04;seg_num++)
{
seg_value = SEG[seg_num];
switch(seg_value)
{
case 0x00: _7SEG_A = 1;//0
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x01: _7SEG_A = 0;//1
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x02: _7SEG_A = 1;//2
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x03: _7SEG_A = 1;//3
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x04: _7SEG_A = 0;//4
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x05: _7SEG_A = 1;//5
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x06: _7SEG_A = 1;//6
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x07: _7SEG_A = 1;//7
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x08: _7SEG_A = 1;//8
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x09: _7SEG_A = 1;//9
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x0A: _7SEG_A = 0;//.
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x0B: _7SEG_A = 0;//-
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x0C: _7SEG_A = 0;//Close
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x0D: _7SEG_A = 1;//E
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x0E: _7SEG_A = 1;//C
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x0F: _7SEG_A = 1;//A
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x10: _7SEG_A = 0;//r
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x11: _7SEG_A = 1;//S
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x12: _7SEG_A = 1;//P
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x13: _7SEG_A = 1;//C.
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x14: _7SEG_A = 0;//d
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x15: _7SEG_A = 0;//n
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x16: _7SEG_A = 0;//V
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x17: _7SEG_A = 0;//h
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x18: _7SEG_A = 0;//L
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x19: _7SEG_A = 0;//U
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x1A: _7SEG_A = 0;//b
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x1B: _7SEG_A = 1;//g
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x1C: _7SEG_A = 0;//V.
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x1D: _7SEG_A = 0;//r.
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x1E: _7SEG_A = 1;//g.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x1F: _7SEG_A = 0;//b.
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
//number + dot
case 0x20: _7SEG_A = 1;//0.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x21: _7SEG_A = 0;//1.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x22: _7SEG_A = 1;//2.
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x23: _7SEG_A = 1;//3.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x24: _7SEG_A = 0;//4.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x25: _7SEG_A = 1;//5.
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x26: _7SEG_A = 1;//6.
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x27: _7SEG_A = 1;//7.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x28: _7SEG_A = 1;//8.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x29: _7SEG_A = 1;//9.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
default: break;
}
switch(seg_num)
{
case 0x00: _7SEG_COM5 = 0;
break;
case 0x01: _7SEG_COM4 = 0;
break;
case 0x02:
_7SEG_COM3 = 0;
break;
case 0x03: if(engineering_f)
_7SEG_H = 1;
_7SEG_COM2 = 0;
break;
case 0x04: _7SEG_COM1 = 0;
break;
default: break;
}
DELAY_X2us(100);
_7SEG_COM0 = 1;
_7SEG_COM1 = 1;
_7SEG_COM2 = 1;
_7SEG_COM3 = 1;
_7SEG_COM4 = 1;
_7SEG_COM5 = 1;
}
SFRPAGE = SFRPAGE_BUF;
}
void SEGMENT_REG_REFRESH(void)
{
//for display voltage (V)
unsigned long voltage;
unsigned char voltage_ten_thousand;
unsigned char voltage_thousand;
unsigned char voltage_hundred;
unsigned char voltage_ten;
unsigned char voltage_unit;
unsigned long DAC_buf =0x0000;
//for display pattern index
unsigned char hundred;
unsigned char ten;
unsigned char unit;
if(Keyboard ==1)
{
switch(Keyboard_Item_Index)
{
case 0: //VGH
voltage = VGH_VGL_A*0.05+ 3.2;
voltage_ten = voltage/10 ;
voltage = voltage%10;
SEG[5] = 0x16;
SEG[4] = 0x1B;
SEG[3] = 0x17;
if(voltage_ten!=0x00)
{
SEG[2] = voltage_ten;
SEG[1] = voltage_unit + 0x20; //add dot
SEG[0] = voltage_hundred;
}
else
{
SEG[2] = voltage_thousand + 0x20; //add dot
SEG[1] = voltage_hundred;
SEG[0] = voltage_ten;
}
break;
case 1: //VGL
voltage = VGH_VGL_B*0.05+ 1.2;
voltage_ten = voltage/10 ;
voltage = voltage%10;
SEG[5] = 0x16;
SEG[4] = 0x1B;
SEG[3] = 0x18;
if(voltage_ten!=0x00)
{
SEG[2] = voltage_ten;
SEG[1] = voltage_unit + 0x20; //add dot
SEG[0] = voltage_hundred;
}
else
{
SEG[2] = voltage_thousand + 0x20; //add dot
SEG[1] = voltage_hundred;
SEG[0] = voltage_ten;
}
break;
// Button Control to switch voltage for adjustment. Keyboard_Item_Index //corresponds each voltage VGH/VGL...
//=======================================================================================
void Button_Check()
{
if(!UP_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Up_Button_f = 1;
Action_f = 0;
Up_Hold_Cnt++;
if(Up_Hold_Cnt > 100)
{
Up_Hold_Cnt = 0;
Up_Button_Hold_f = 1;
if(Keyboard == 1)
{
BUZZER();
}
else
{
BUZZER();
DELAY_X2us(50000);
BUZZER();
DELAY_X2us(50000);
}
}
}
}
else if(!DOWN_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Down_Button_f = 1;
Action_f = 0;
Down_Hold_Cnt++;
if(Down_Hold_Cnt > 100)
{
Down_Hold_Cnt = 0;
Down_Button_Hold_f = 1;
if(Keyboard==1)
{
BUZZER();
}
}
}
}
else if(!LEFT_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Left_Button_f = 1;
Action_f = 0;
Left_Hold_Cnt++;
if(Keyboard == 1)
{
}
else
{
BUZZER();
DELAY_X2us(50000);
BUZZER();
DELAY_X2us(50000);
}
if(Left_Hold_Cnt > 100)
{
Left_Hold_Cnt = 0;
Left_Button_Hold_f = 1;
//BUZZER();
}
}
}
else if(!RIGHT_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Right_Button_f = 1;
Action_f = 0;
Right_Hold_Cnt++;
if(Keyboard == 1)
{
}
else
{
POWER_SLEEP();
//BUZZER();
DELAY_X2us(50000);
//BUZZER();
DELAY_X2us(50000);
}
if(Right_Hold_Cnt > 100)
{
Right_Hold_Cnt = 0;
Right_Button_Hold_f = 1;
//BUZZER();
}
}
}
else if(!ENTER_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Enter_Button_f = 1;
Action_f = 0;
Enter_Hold_Cnt++;
if(Enter_Hold_Cnt > 100)
{
Enter_Hold_Cnt = 0;
Enter_Button_Hold_f = 1;
BUZZER();
DELAY_X2us(50000);
BUZZER();
DELAY_X2us(50000);
}
}
}
else
{
Action_f = 1;
Debounce_Cnt = 0;
Up_Hold_Cnt = 0;
Down_Hold_Cnt = 0;
Left_Hold_Cnt = 0;
Right_Hold_Cnt = 0;
Enter_Hold_Cnt = 0;
}
}
void Button_Function()
{
//int i;
if(Up_Button_Hold_f)
{
Up_Button_f = 0;
Up_Button_Hold_f = 0;
if(Keyboard ==1)
{
Keyboard_Adj(Keyboard_Item_Index,3);
}
else
{
returnPatternZero();
}
//Keyboard = !Keyboard;
}
else if(Down_Button_Hold_f)
{
Down_Button_f = 0;
Down_Button_Hold_f = 0;
BUZZER();
check_ID();
// DELAY_X2us(5000);
// if(Keyboard ==1)
// {
//
// Keyboard_Adj(Keyboard_Item_Index,2);
//
// }
}
else if(Left_Button_Hold_f)
{
Left_Button_f = 0;
Left_Button_Hold_f = 0;
}
else if(Right_Button_Hold_f)
{
Right_Button_f = 0;
Right_Button_Hold_f = 0;
//DC Power wake up
POWER_INITIAL();
DELAY_X2us(5000);
returnPatternZero();
}
else if(Enter_Button_Hold_f)
{
Enter_Button_f = 0;
Enter_Button_Hold_f = 0;
Keyboard = !Keyboard;
}
else if(Up_Button_f)
{
Up_Button_f = 0;
if(Keyboard ==1)
{
Keyboard_Adj(Keyboard_Item_Index,1);
}
else
{
if(Pattern_Index<Total_Pattern-1)
{
Pattern_Index++;
}
else
{
Pattern_Index=0;
}
quickPattern();
}
}
else if(Down_Button_f)
{
Down_Button_f = 0;
if(Keyboard ==1)
{
Keyboard_Adj(Keyboard_Item_Index,0);
}
else
{
if(Pattern_Index>0)
{
Pattern_Index--;
}
else
{
Pattern_Index=Total_Pattern-1;
}
quickPattern();
}
}
else if(Left_Button_f)
{
Left_Button_f = 0;
if(Keyboard ==1)
{
if(Keyboard_Item_Index >0)
{
Keyboard_Item_Index-=1 ;
}
else
{
Keyboard_Item_Index = Total_Keyboard_Item_Index-1;
}
}
else
{
returnPatternZero();
}
}
else if(Right_Button_f)
{
Right_Button_f = 0;
if(Keyboard ==1)
{
if(Keyboard_Item_Index < Total_Keyboard_Item_Index-1)
{
Keyboard_Item_Index +=1 ;
}
else
{
Keyboard_Item_Index = 0;
}
}
else
{
//DC Power wake up
POWER_INITIAL();
DELAY_X2us(5000);
returnPatternZero();
}
}
else if(Enter_Button_f)
{
Enter_Button_f = 0;
}
}
void main(void)
{
while(1)
{
Button_Check();
if(Action_f)
{
Button_Function();
}
SEGMENT_REG_REFRESH();
SEGMENT_DISPLAY();
}
}
In you definition voltage is an int value:
unsigned long voltage;
You can't do things like this:
voltage = VGH_VGL_B*0.05+ 1.2;
with an integer.
I guess you should avoid float at all.

How to get glass break effect with text

I am searching for a script that creates a glass break effect with text using CSS but I didn't find anything.
I used this but
// triangulation using https://github.com/ironwallaby/delaunay
// For more check out zachsaucier.com
const TWO_PI = Math.PI * 2;
var images = [],
imageIndex = 0;
var image,
imageWidth = 50,
imageHeight = 50;
var vertices = [],
indices = [],
prevfrag = [],
fragments = [];
var margin = 50;
var container = document.getElementById('container');
var clickPosition = [imageWidth * 0.5, imageHeight * 0.5];
window.onload = function() {
TweenMax.set(container, {
perspective: 500
});
// images from http://www.hdwallpapers.in
var urls = [
'http://i.imgur.com/QddsEpk.jpg',
'http://i.imgur.com/OeDykaH.jpg',
'http://i.imgur.com/lLHspCj.jpg',
'http://i.imgur.com/tCz9GQS.jpg'
],
image,
loaded = 0;
// very quick and dirty hack to load and display the first image asap
images[0] = image = new Image();
image.onload = function() {
if (++loaded === 1) {
for (var i = 1; i < 4; i++) {
images[i] = image = new Image();
image.src = urls[i];
}
placeImage();
}
};
image.src = urls[0];
};
function placeImage(transitionIn) {
image = images[imageIndex];
if (++imageIndex === images.length) imageIndex = 0;
var num = Math.random();
if (num < .25) {
image.direction = "left";
} else if (num < .5) {
image.direction = "top";
} else if (num < .75) {
image.direction = "bottom";
} else {
image.direction = "right";
}
container.appendChild(image);
image.style.opacity = 0;
if (transitionIn !== false) {
triangulateIn();
}
}
function triangulateIn(event) {
var box = image.getBoundingClientRect(),
top = box.top,
left = box.left;
if (image.direction == "left") {
clickPosition[0] = 0;
clickPosition[1] = imageHeight / 2;
} else if (image.direction == "top") {
clickPosition[0] = imageWidth / 2;
clickPosition[1] = 0;
} else if (image.direction == "bottom") {
clickPosition[0] = imageWidth / 2;
clickPosition[1] = imageHeight;
} else if (image.direction == "right") {
clickPosition[0] = imageWidth;
clickPosition[1] = imageHeight / 2;
}
triangulate();
build();
}
function triangulate() {
for (var i = 0; i < 40; i++) {
x = -margin + Math.random() * (imageWidth + margin * 2);
y = -margin + Math.random() * (imageHeight + margin * 2);
vertices.push([x, y]);
}
vertices.push([0, 0]);
vertices.push([imageWidth, 0]);
vertices.push([imageWidth, imageHeight]);
vertices.push([0, imageHeight]);
vertices.forEach(function(v) {
v[0] = clamp(v[0], 0, imageWidth);
v[1] = clamp(v[1], 0, imageHeight);
});
indices = Delaunay.triangulate(vertices);
}
function build() {
var p0, p1, p2,
fragment;
var tl0 = new TimelineMax({
onComplete: buildCompleteHandler
});
for (var i = 0; i < indices.length; i += 3) {
p0 = vertices[indices[i + 0]];
p1 = vertices[indices[i + 1]];
p2 = vertices[indices[i + 2]];
fragment = new Fragment(p0, p1, p2);
var dx = fragment.centroid[0] - clickPosition[0],
dy = fragment.centroid[1] - clickPosition[1],
d = Math.sqrt(dx * dx + dy * dy),
rx = 30 * sign(dy),
ry = 90 * -sign(dx),
delay = d * 0.003 * randomRange(0.9, 1.1);
fragment.canvas.style.zIndex = Math.floor(d).toString();
var tl1 = new TimelineMax();
if (image.direction == "left") {
rx = Math.abs(rx);
ry = 0;
} else if (image.direction == "top") {
rx = 0;
ry = Math.abs(ry);
} else if (image.direction == "bottom") {
rx = 0;
ry = -Math.abs(ry);
} else if (image.direction == "right") {
rx = -Math.abs(rx);
ry = 0;
}
tl1.from(fragment.canvas, 1, {
z: -50,
rotationX: rx,
rotationY: ry,
scaleX: 0,
scaleY: 0,
ease: Cubic.easeIn
});
tl1.from(fragment.canvas, 0.4, {
alpha: 0
}, 0.6);
tl0.insert(tl1, delay);
fragments.push(fragment);
container.appendChild(fragment.canvas);
}
}
function buildCompleteHandler() {
// add pooling?
image.style.opacity = 1;
image.addEventListener('transitionend', function catchTrans() {
fragments.forEach(function(f) {
container.removeChild(f.canvas);
});
fragments.length = 0;
vertices.length = 0;
indices.length = 0;
placeImage();
this.removeEventListener('transitionend', catchTrans, false);
}, false);
}
//////////////
// MATH UTILS
//////////////
function randomRange(min, max) {
return min + (max - min) * Math.random();
}
function clamp(x, min, max) {
return x < min ? min : (x > max ? max : x);
}
function sign(x) {
return x < 0 ? -1 : 1;
} < script >
//////////////
// FRAGMENT
//////////////
Fragment = function(v0, v1, v2) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
this.computeBoundingBox();
this.computeCentroid();
this.createCanvas();
this.clip();
};
Fragment.prototype = {
computeBoundingBox: function() {
var xMin = Math.min(this.v0[0], this.v1[0], this.v2[0]),
xMax = Math.max(this.v0[0], this.v1[0], this.v2[0]),
yMin = Math.min(this.v0[1], this.v1[1], this.v2[1]),
yMax = Math.max(this.v0[1], this.v1[1], this.v2[1]);
this.box = {
x: Math.round(xMin),
y: Math.round(yMin),
w: Math.round(xMax - xMin),
h: Math.round(yMax - yMin)
};
},
computeCentroid: function() {
var x = (this.v0[0] + this.v1[0] + this.v2[0]) / 3,
y = (this.v0[1] + this.v1[1] + this.v2[1]) / 3;
this.centroid = [x, y];
},
createCanvas: function() {
this.canvas = document.createElement('canvas');
this.canvas.width = this.box.w;
this.canvas.height = this.box.h;
this.canvas.style.width = this.box.w + 'px';
this.canvas.style.height = this.box.h + 'px';
this.canvas.style.left = this.box.x + 'px';
this.canvas.style.top = this.box.y + 'px';
this.ctx = this.canvas.getContext('2d');
},
clip: function() {
this.ctx.save();
this.ctx.translate(-this.box.x, -this.box.y);
this.ctx.beginPath();
this.ctx.moveTo(this.v0[0], this.v0[1]);
this.ctx.lineTo(this.v1[0], this.v1[1]);
this.ctx.lineTo(this.v2[0], this.v2[1]);
this.ctx.closePath();
this.ctx.clip();
this.ctx.drawImage(image, 0, 0);
this.ctx.restore();
}
};
body {
background-color: #000;
margin: 0;
overflow: hidden;
}
canvas {
position: absolute;
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
}
img {
position: absolute;
-webkit-transition: opacity .3s;
transition: opacity .3s;
}
#container {
position: absolute;
width: 25px;
height: 25px;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
<div id="container"></div>
Main code for this is from here but I want glassbreak like this using CSS
So while I don't have a specific example of glass breaking with CSS3, I do recommend this kind of effect would probably work very well with glass breaking:
http://www.species-in-pieces.com/
As you notice they use CSS3 polygons to render out shapes of animals. Here's an example snip from the site:
-webkit-clip-path: polygon(44.65% 39.571%, 35.85% 59.429%, 52.85% 50.857%);
-webkit-transition-duration: .8s;
-moz-transition-duration: .8s;
transition-duration: .8s;
Essentially you'd define webkit transforms through each defined polygon for the effect. Drawbacks of using a feature like this is it's currently only supported in webkit browsers, but at the same time that kind of animation affect would be pretty hard to do cross browser support for in CSS.
If I have some time I'll come around and do a quick glass breaking fiddle tomorrow

Why the Text Header doesn't move when i scroll the grid?

i´m using Obout grid - Column Sets
There are two possibles scenarios.
1.- The Text Header moves when i scroll the grid and i set in the scrollingSetting
the property NumberOfFixedColumns to 0 , here everything works fine.
2.- The Text Header doesn´t moves when i scroll the grid and i set in the scrollingSetting
the property NumberOfFixedColumns different to 0
So i wanna use a fixed column but it doesn´t work properly.
The code use for this in the web page:
<link href="../App_Themes/Theme7/styles/oboutgrid/column-set.css" rel="stylesheet"
type="text/css" />
<script type="text/javascript">
window.onload = function () {
//Grid1.addColumnSet(level, startColumnIndex, endColumnIndex, text);
grdCatalogo.addColumnSetScrollGrid(0, 0, 8, '');
grdCatalogo.addColumnSetScrollGrid(0, 9, 10, 'Cliente');
}
</script>
the code of the column-set.js:
oboutGrid.prototype.addColumnSetScrollGrid = function (level, startColumnIndex, endColumnIndex, text) {
if (typeof (this.GridColumnSetsContainer) == 'undefined') {
this.GridColumnSetsContainer = document.createElement('DIV');
this.GridColumnSetsContainer.className = 'ob_gCSContScroll';
this.GridColumnSetsContainer.style.width = this.GridMainContainer.style.width;
this.GridMainContainer.appendChild(this.GridColumnSetsContainer);
}
if (typeof (this.ColumnSets) == 'undefined') {
this.ColumnSets = new Array();
}
if (typeof (this.ColumnSets[level]) == 'undefined') {
this.ColumnSets[level] = new Array();
this.GridHeaderContainer.firstChild.style.marginTop = (level + 1) * 25 + 'px';
var levelContainer = document.createElement('DIV');
levelContainer.className = "ob_gCSContLevel";
levelContainer.style.width = this.GridHeaderContainer.firstChild.firstChild.offsetWidth + 'px';
this.GridColumnSetsContainer.appendChild(levelContainer);
}
// if ($(this.GridMainContainer).css('width') <= $(this.GridColumnSetsContainer).css('width')) {
// var newWidth = $(this.GridColumnSetsContainer).css('width') - $(this.GridMainContainer).css('width');
// $(this.GridColumnSetsContainer).css('width',newWidth);
// }
var columnSet = document.createElement('DIV');
columnSet.className = 'ob_gCSet';
this.GridColumnSetsContainer.childNodes[level].appendChild(columnSet);
//var position = this.GridHeaderContainer.position();
var position = this.GridHeaderContainer.getBoundingClientRect()
var top = position.top;
var left = position.left;
$(this.GridColumnSetsContainer).css({ "top": top });
$(this.GridColumnSetsContainer).css({ "margin-left": left });
var columnSetContent = document.createElement('DIV');
columnSetContent.innerHTML = text;
columnSet.appendChild(columnSetContent);
columnSet.style.width = columnSetWidth + 'px';
if (endColumnIndex < this.ColumnsCollection.length - 1) {
var tempLevel = level;
if (!(level == 0 || this.GridHeaderContainer.firstChild.childNodes[endColumnIndex + 1].style.top)) {
tempLevel -= 1;
}
var newTop = (-1 - tempLevel) * (25);
this.GridHeaderContainer.firstChild.childNodes[endColumnIndex + 1].style.top = newTop + 'px';
}
if (this.ColumnsCollection.lenght != 0) {
var columnSetWidth = 0;
for (var i = startColumnIndex; i <= endColumnIndex; i++) {
if (this.ColumnsCollection[i] != null && this.ColumnsCollection[i] != 'undefined' && this.ColumnsCollection[i].Visible) {
columnSetWidth += this.ColumnsCollection[i].Width;
}
}
}
columnSet.style.width = columnSetWidth + 'px';
var columnSetObject = new Object();
columnSetObject.Level = level;
columnSetObject.StartColumnIndex = startColumnIndex;
columnSetObject.EndColumnIndex = endColumnIndex;
columnSetObject.ColumnSet = columnSet;
this.ColumnSets[level].push(columnSetObject);
}
oboutGrid.prototype.resizeColumnSets = function () {
for (var level = 0; level < this.ColumnSets.length; level++) {
for (var i = 0; i < this.ColumnSets[level].length; i++) {
var columnSetWidth = 0;
for (var j = this.ColumnSets[level][i].StartColumnIndex; j <= this.ColumnSets[level] [i].EndColumnIndex; j++) {
if (this.ColumnsCollection[j].Visible) {
columnSetWidth += this.ColumnsCollection[j].Width;
}
}
this.ColumnSets[level][i].ColumnSet.style.width = columnSetWidth + 'px';
}
}
}
oboutGrid.prototype.resizeColumnOld = oboutGrid.prototype.resizeColumn;
oboutGrid.prototype.resizeColumn = function (columnIndex, amountToResize, keepGridWidth) {
this.resizeColumnOld(columnIndex, amountToResize, keepGridWidth);
this.resizeColumnSets();
}
oboutGrid.prototype.synchronizeBodyHorizontalScrollingOld = oboutGrid.prototype.synchronizeBodyHorizontalScrolling;
oboutGrid.prototype.synchronizeBodyHorizontalScrolling = function () {
this.synchronizeBodyHorizontalScrollingOld();
//this.GridColumnSetsContainer.style.marginLeft = -1 * this.GridBodyContainer.firstChild.scrollLeft + 'px';
this.GridHeaderContainer.firstChild.style.marginLeft = -1 * this.GridBodyContainer.firstChild.scrollLeft + 'px';
this.GridColumnSetsContainer.scrollLeft = this.GridBodyContainer.firstChild.scrollLeft;
}
The css file:
.ob_gCSContScroll
{
position: absolute !important;
/*top: 17px !important;*/
left: 0px !important;
right: 0px !important;
overflow: hidden;
width: 1179px;
margin-left: 47px;
}
/* A column set row (level) */
.ob_gCSContLevel
{
height: 25px !important;
background-image: url(column-set.png);
background-repeat: repeat-x;
background-color: #A8AEBD;
}
/* The column set for a number of columns */
.ob_gCSet
{
color: #01354D;
font-family: Verdana;
font-size: 12px;
font-weight: bold;
float: left;
text-align: center;
}
/* The text of a column set */
.ob_gCSet div
{
margin-left: 20px;
margin-top: 3px;
}
/* The separator between two column sets */
.ob_gCSetSep
{
top: -25px !important;
}
.ob_gHCont, .ob_gHContWG
{
z-index: 10 !important;
}
.ob_gHICont
{
overflow: visible !important;
}
I have found the solution, i had to modify the next files:
1.- I had to add the next line in the method addColumnSet of the column-set.js file :
this.GridColumnSetsContainer.setAttribute("id","BarraScroll");
I added it just after this line:
this.GridColumnSetsContainer.className = 'ob_gCSCont';
2.- i had to modify the next function : oboutGrid.prototype.synchronizeFixedColumns that is in the oboutgrid_scrolling.js file, and the method with all the changes is the next one, this method is called every time that that the user scroll the grid and the property NumberOrFixedColumns of the tag ScrollSetting that is inside of the grids tag is different of zero:
oboutGrid.prototype.synchronizeFixedColumns = function() {
var b = this.HorizontalScroller.firstChild.scrollLeft;
if (this.PreviousScrollLeft == b) return;
else this.PreviousScrollLeft = b;
var g = parseInt(this.ScrollWidth);
if (this.FixedColumnsPosition == 1) {
for (var f = this.getVisibleColumnsWidth(false), c = false, a = this.NumberOfFixedColumns; a < this.ColumnsCollection.length - 1; a++)
if (f > g) {
if (this.ColumnsCollection[a].Visible)
if (b >= this.ScrolledColumnsWidth) {
this.hideColumn(a, false, false);
this.markColumnsAsScrolled(a);
f = this.getVisibleColumnsWidth(false);
document.getElementById("BarraScroll").scrollLeft = b + this.ColumnsCollection[a].Width;
. c = true
} else break
} else break;
if (!c)
for (var a = this.ColumnsCollection.length - 2; a >= this.NumberOfFixedColumns; a--)
if (this.ColumnsCollection[a].RealVisible == true && this.ColumnsCollection[a].Visible == false)
if (b <= this.ScrolledColumnsWidth - this.ColumnsCollection[a].Width) {
document.getElementById("BarraScroll").scrollLeft = b;
this.showColumn(a, false, false);
this.unmarkColumnsAsScrolled(a)
} else break
} else {
for (var e = false, c = false, d = this.getFixedColumnsWidth() - this.PreviousFixedColumnsWidth, a = 1; a < this.ColumnsCollection.length - this.NumberOfFixedColumns; a++)
if (this.ColumnsCollection[a].RealVisible == true && this.ColumnsCollection[a].Visible == false)
if (b - d >= this.ScrolledColumnsWidth) {
document.getElementById("BarraScroll").scrollLeft = b + this.ColumnsCollection[a].Width;
this.showColumn(a, false, false);
this.markColumnsAsScrolled(a);
e = true
} else break;
var h = false;
if (!e)
for (var a = this.ColumnsCollection.length - this.NumberOfFixedColumns - 1; a > 0; a--)
if (this.ColumnsCollection[a].Visible)
if (b - d <= this.ScrolledColumnsWidth - this.ColumnsCollection[a].Width) {
document.getElementById("BarraScroll").scrollLeft = b;
this.hideColumn(a, false, false);
this.unmarkColumnsAsScrolled(a);
d = this.getFixedColumnsWidth() - this.PreviousFixedColumnsWidth;
c = true
} else break;
if (e || c) {
this.rightAlignGridContent();
if (b == 0) {
this.PreviousFixedColumnsWidth = this.getFixedColumnsWidth();
this.ScrolledColumnsIndexes = ",";
this.updateScrolledColumnsWidth()
}
}
}
};

Resources