Skip to content

Instantly share code, notes, and snippets.

@raveren
Created March 12, 2018 13:31
Show Gist options
  • Save raveren/00b118b647ac569b0241a4c6f0fb601c to your computer and use it in GitHub Desktop.
Save raveren/00b118b647ac569b0241a4c6f0fb601c to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// DEFINITIONS //////////////////////////////////////////////
///////////////// YOU CAN CHANGE CODE AT THE BOTTOM ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
const display = [
1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1,
1, 1,
1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1
]
let digit = [[1]]
const emptyDigit = function () {
return [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,]
}
//////////
//// 00:0X
//////////
digit[1] = emptyDigit()
digit[1][23] = 0
digit[1][24] = 0
digit[1][25] = 1
digit[1][26] = 0
digit[1][27] = 0
digit[1][28] = 0
digit[1][29] = 1
digit[2] = emptyDigit()
digit[2][23] = 1
digit[2][24] = 1
digit[2][25] = 0
digit[2][26] = 1
digit[2][27] = 0
digit[2][28] = 1
digit[2][29] = 1
digit[3] = emptyDigit()
digit[3][23] = 0
digit[3][24] = 1
digit[3][25] = 1
digit[3][26] = 1
digit[3][27] = 0
digit[3][28] = 1
digit[3][29] = 1
digit[4] = emptyDigit()
digit[4][23] = 0
digit[4][24] = 0
digit[4][25] = 1
digit[4][26] = 1
digit[4][27] = 1
digit[4][28] = 0
digit[4][29] = 1
digit[5] = emptyDigit()
digit[5][23] = 0
digit[5][24] = 1
digit[5][25] = 1
digit[5][26] = 1
digit[5][27] = 1
digit[5][28] = 1
digit[5][29] = 0
digit[6] = emptyDigit()
digit[6][23] = 1
digit[6][24] = 1
digit[6][25] = 1
digit[6][26] = 1
digit[6][27] = 1
digit[6][28] = 1
digit[6][29] = 0
digit[7] = emptyDigit()
digit[7][23] = 0
digit[7][24] = 0
digit[7][25] = 1
digit[7][26] = 0
digit[7][27] = 1
digit[7][28] = 1
digit[7][29] = 1
digit[8] = emptyDigit()
digit[8][23] = 1
digit[8][24] = 1
digit[8][25] = 1
digit[8][26] = 1
digit[8][27] = 1
digit[8][28] = 1
digit[8][29] = 1
digit[9] = emptyDigit()
digit[9][23] = 0
digit[9][24] = 1
digit[9][25] = 1
digit[9][26] = 1
digit[9][27] = 1
digit[9][28] = 1
digit[9][29] = 1
//////////
//// 00:X0
//////////
digit[11] = emptyDigit()
digit[11][16] = 0
digit[11][17] = 0
digit[11][18] = 1
digit[11][19] = 0
digit[11][20] = 0
digit[11][21] = 0
digit[11][22] = 1
digit[12] = emptyDigit()
digit[12][16] = 0
digit[12][17] = 1
digit[12][18] = 1
digit[12][19] = 1
digit[12][20] = 1
digit[12][21] = 1
digit[12][22] = 0
digit[13] = emptyDigit()
digit[13][16] = 0
digit[13][17] = 1
digit[13][18] = 1
digit[13][19] = 1
digit[13][20] = 0
digit[13][21] = 1
digit[13][22] = 1
digit[14] = emptyDigit()
digit[14][16] = 1
digit[14][17] = 0
digit[14][18] = 1
digit[14][19] = 1
digit[14][20] = 0
digit[14][21] = 0
digit[14][22] = 1
digit[15] = emptyDigit()
digit[15][16] = 1
digit[15][17] = 1
digit[15][18] = 0
digit[15][19] = 1
digit[15][20] = 0
digit[15][21] = 1
digit[15][22] = 1
// impossible numbers:
digit[16] = emptyDigit()
digit[16][16] = 1
digit[16][17] = 1
digit[16][18] = 0
digit[16][19] = 1
digit[16][20] = 1
digit[16][21] = 1
digit[16][22] = 1
digit[17] = emptyDigit()
digit[17][16] = 1
digit[17][17] = 1
digit[17][18] = 1
digit[17][19] = 0
digit[17][20] = 0
digit[17][21] = 0
digit[17][22] = 1
digit[18] = emptyDigit()
digit[18][16] = 1
digit[18][17] = 1
digit[18][18] = 1
digit[18][19] = 1
digit[18][20] = 1
digit[18][21] = 1
digit[18][22] = 1
digit[19] = emptyDigit()
digit[19][16] = 1
digit[19][17] = 1
digit[19][18] = 1
digit[19][19] = 1
digit[19][20] = 0
digit[19][21] = 1
digit[19][22] = 1
//////////
//// 0X:00
//////////
digit[21] = emptyDigit()
digit[21][7] = 0
digit[21][8] = 0
digit[21][9] = 1
digit[21][10] = 0
digit[21][11] = 0
digit[21][12] = 0
digit[21][13] = 1
digit[22] = emptyDigit()
digit[22][7] = 1
digit[22][8] = 1
digit[22][9] = 0
digit[22][10] = 1
digit[22][11] = 0
digit[22][12] = 1
digit[22][13] = 1
digit[23] = emptyDigit()
digit[23][7] = 0
digit[23][8] = 1
digit[23][9] = 1
digit[23][10] = 1
digit[23][11] = 0
digit[23][12] = 1
digit[23][13] = 1
digit[24] = emptyDigit()
digit[24][7] = 0
digit[24][8] = 0
digit[24][9] = 1
digit[24][10] = 1
digit[24][11] = 1
digit[24][12] = 0
digit[24][13] = 1
digit[25] = emptyDigit()
digit[25][7] = 0
digit[25][8] = 1
digit[25][9] = 1
digit[25][10] = 1
digit[25][11] = 1
digit[25][12] = 1
digit[25][13] = 0
digit[26] = emptyDigit()
digit[26][7] = 1
digit[26][8] = 1
digit[26][9] = 1
digit[26][10] = 1
digit[26][11] = 1
digit[26][12] = 1
digit[26][13] = 0
digit[27] = emptyDigit()
digit[27][7] = 0
digit[27][8] = 0
digit[27][9] = 1
digit[27][10] = 0
digit[27][11] = 1
digit[27][12] = 1
digit[27][13] = 1
digit[28] = emptyDigit()
digit[28][7] = 1
digit[28][8] = 1
digit[28][9] = 1
digit[28][10] = 1
digit[28][11] = 1
digit[28][12] = 1
digit[28][13] = 1
digit[29] = emptyDigit()
digit[29][7] = 0
digit[29][8] = 1
digit[29][9] = 1
digit[29][10] = 1
digit[29][11] = 1
digit[29][12] = 1
digit[29][13] = 1
//////////
//// X0:00
//////////
digit[31] = emptyDigit()
digit[31][0] = 0
digit[31][1] = 0
digit[31][2] = 1
digit[31][3] = 0
digit[31][4] = 0
digit[31][5] = 0
digit[31][6] = 1
digit[32] = emptyDigit()
digit[32][0] = 0
digit[32][1] = 1
digit[32][2] = 1
digit[32][3] = 1
digit[32][4] = 1
digit[32][5] = 1
digit[32][6] = 0
digit[33] = emptyDigit()
digit[33][0] = 0
digit[33][1] = 1
digit[33][2] = 1
digit[33][3] = 1
digit[33][4] = 0
digit[33][5] = 1
digit[33][6] = 1
digit[34] = emptyDigit()
digit[34][0] = 1
digit[34][1] = 0
digit[34][2] = 1
digit[34][3] = 1
digit[34][4] = 0
digit[34][5] = 0
digit[34][6] = 1
digit[35] = emptyDigit()
digit[35][0] = 1
digit[35][1] = 1
digit[35][2] = 0
digit[35][3] = 1
digit[35][4] = 0
digit[35][5] = 1
digit[35][6] = 1
// impossible numbers:
digit[36] = emptyDigit()
digit[36][0] = 1
digit[36][1] = 1
digit[36][2] = 0
digit[36][3] = 1
digit[36][4] = 1
digit[36][5] = 1
digit[36][6] = 1
digit[37] = emptyDigit()
digit[37][0] = 1
digit[37][1] = 1
digit[37][2] = 1
digit[37][3] = 0
digit[37][4] = 0
digit[37][5] = 0
digit[37][6] = 1
digit[38] = emptyDigit()
digit[38][0] = 1
digit[38][1] = 1
digit[38][2] = 1
digit[38][3] = 1
digit[38][4] = 1
digit[38][5] = 1
digit[38][6] = 1
digit[39] = emptyDigit()
digit[39][0] = 1
digit[39][1] = 1
digit[39][2] = 1
digit[39][3] = 1
digit[39][4] = 0
digit[39][5] = 1
digit[39][6] = 1
let ledStrip = neopixel.create(DigitalPin.P0, 30, NeoPixelMode.RGB)
let clockColor = neopixel.colors(NeoPixelColors.Red)
////////////////////////////////////////////////////////////////////
//////////////////////// FUNCTIONALITY /////////////////////////////
////////////////////////////////////////////////////////////////////
function startCountdown(timeInSeconds = 360, timeSpeed = 1000) {
let timeLeft = timeInSeconds
let remaining = 0
let hue = 0 // go from green to red
for (let i = 0; i <= timeInSeconds; i++) {
// go from green to red: 120 = green; 0 = red
remaining = timeInSeconds / timeLeft
if (remaining > 2) {
hue = 60
} else if (remaining > 3) {
hue = 30
} else if (remaining > 4) {
hue = 20
} else if (remaining > 5) {
hue = 0
} else {
hue = 120
}
clockColor = neopixel.hsl(hue, 100, 50)
setTime(timeLeft--)
basic.pause(timeSpeed)
}
}
function setTime(timeInSeconds = 0) {
let isOn = false
let mins = MathFloor(timeInSeconds / 60)
let secs = timeInSeconds % 60
let firstDigit = secs % 10
let secondDigit = (secs - firstDigit) / 10
let thirdDigit = mins % 10
let fourthDigit = (mins - thirdDigit) / 10
if (fourthDigit > 9) {
fourthDigit = 9
}
for (let i = 0; i < display.length; i++) {
if (firstDigit > 0 && digit[firstDigit][i] !== 2) {
isOn = digit[firstDigit][i] == 1
} else if (secondDigit > 0 && digit[secondDigit + 10][i] !== 2) {
isOn = digit[secondDigit + 10][i] == 1
} else if (thirdDigit > 0 && digit[thirdDigit + 20][i] !== 2) {
isOn = digit[thirdDigit + 20][i] == 1
} else if (fourthDigit > 0 && digit[fourthDigit + 30][i] !== 2) {
isOn = digit[fourthDigit + 30][i] == 1
} else {
isOn = display[i] == 1
}
ledStrip.setPixelColor(i, isOn ? clockColor : neopixel.colors(NeoPixelColors.Black))
}
ledStrip.show()
}
function MathFloor(n: number) {
return n - (n % 1)
}
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// END DEFINITIONS, BEGIN COMMANDS //////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
// example:
// setTime( 60 * 60 + 45 ) // 1h 45s - static display, no countdown
// startCountdown(100, 500) // set at 100 seconds, count down in double speed
let countDownTimeInSeconds = 60 * 35 // 35 min
let timeOfSpeed = 1000 // 1000 = 100%; 100 = 10%; 10 = 1% time speed
startCountdown(countDownTimeInSeconds, timeOfSpeed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment