Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
elecfreaks1 authored May 28, 2020
1 parent 3ad12ea commit 5d6da48
Show file tree
Hide file tree
Showing 15 changed files with 7,813 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (c) Microsoft Corporation

All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all: deploy

build:
pxt build

deploy:
pxt deploy

test:
pxt test
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
for PXT/microbit

how do i real time clock??????


known issues:

- none so far


we've also got the DateTime class from arduino's RTClib here, maybe find some way to expose that as well?
13 changes: 13 additions & 0 deletions _locales/ds1307-jsdoc-strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"RTC_DS1307": "Use this file to define custom functions and blocks.\r\nRead more at https://makecode.microbit.org/blocks/custom\r\n\r\nRTC_DS1307 block",
"RTC_DS1307.DateTime": "set Date and Time",
"RTC_DS1307.DateTime|param|day": "is the Day will be set, eg: 17",
"RTC_DS1307.DateTime|param|hour": "is the Hour will be set, eg: 12",
"RTC_DS1307.DateTime|param|minute": "is the Minute will be set, eg: 31",
"RTC_DS1307.DateTime|param|month": "is the Month will be set, eg: 1",
"RTC_DS1307.DateTime|param|second": "is the Second will be set, eg: 19",
"RTC_DS1307.DateTime|param|year": "is the Year will be set, eg: 2019",
"RTC_DS1307.getTime": "get time",
"RTC_DS1307.getWeekday": "get time\r\n\r\nget weekday\r\n0 means Sunday\r\n1 means Monday\r\n2 means Tuesday\r\n3 means Wednesday\r\n4 means Thursday\r\n5 means Friday\r\n6 means Saturday",
"RTC_DS1307.setTime": "set time"
}
14 changes: 14 additions & 0 deletions _locales/ds1307-strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"RTC_DS1307.DateTime|block": "set year %year| month %month| day %day| hour %hour| minute %minute| second %second",
"RTC_DS1307.TimeType.DAY|block": "day",
"RTC_DS1307.TimeType.HOUR|block": "hour",
"RTC_DS1307.TimeType.MINUTE|block": "minute",
"RTC_DS1307.TimeType.MONTH|block": "month",
"RTC_DS1307.TimeType.SECOND|block": "second",
"RTC_DS1307.TimeType.YEAR|block": "year",
"RTC_DS1307.getTime|block": "%data",
"RTC_DS1307.getWeekday|block": "weekday",
"RTC_DS1307.setTime|block": "set %datatype |%data",
"RTC_DS1307|block": "RTC1307",
"{id:category}RTC_DS1307": "RTC_DS1307"
}
13 changes: 13 additions & 0 deletions _locales/zh/ds1307-jsdoc-strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"RTC_DS1307": "Use this file to define custom functions and blocks.\r\nRead more at https://makecode.microbit.org/blocks/custom\r\n\r\nRTC_DS1307 block",
"RTC_DS1307.DateTime": "set Date and Time",
"RTC_DS1307.DateTime|param|day": "is the Day will be set, eg: 17",
"RTC_DS1307.DateTime|param|hour": "is the Hour will be set, eg: 12",
"RTC_DS1307.DateTime|param|minute": "is the Minute will be set, eg: 31",
"RTC_DS1307.DateTime|param|month": "is the Month will be set, eg: 1",
"RTC_DS1307.DateTime|param|second": "is the Second will be set, eg: 19",
"RTC_DS1307.DateTime|param|year": "is the Year will be set, eg: 2019",
"RTC_DS1307.getTime": "get time",
"RTC_DS1307.getWeekday": "get time\r\n\r\nget weekday\r\n0 means Sunday\r\n1 means Monday\r\n2 means Tuesday\r\n3 means Wednesday\r\n4 means Thursday\r\n5 means Friday\r\n6 means Saturday",
"RTC_DS1307.setTime": "set time"
}
14 changes: 14 additions & 0 deletions _locales/zh/ds1307-strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"RTC_DS1307.DateTime|block": "设置 年 %year| 月 %month| 日 %day| 时 %hour| 分 %minute| 秒 %second",
"RTC_DS1307.TimeType.DAY|block": "",
"RTC_DS1307.TimeType.HOUR|block": "",
"RTC_DS1307.TimeType.MINUTE|block": "",
"RTC_DS1307.TimeType.MONTH|block": "",
"RTC_DS1307.TimeType.SECOND|block": "",
"RTC_DS1307.TimeType.YEAR|block": "",
"RTC_DS1307.getTime|block": "%data",
"RTC_DS1307.getWeekday|block": "星期",
"RTC_DS1307.setTime|block": "设置 %datatype |为 %data",
"RTC_DS1307|block": "实时时钟",
"{id:category}RTC_DS1307": "实时时钟"
}
211 changes: 211 additions & 0 deletions ds1307.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@

/**
* Use this file to define custom functions and blocks.
* Read more at https://makecode.microbit.org/blocks/custom
*/

/**
* RTC_DS1307 block
*/
//% weight=0 color=#f3555a icon="\uf017" block="RTC1307"
namespace RTC_DS1307 {
export enum TimeType {
//% block="second" enumval=0
SECOND,
//% block="minute" enumval=1
MINUTE,
//% block="hour" enumval=2
HOUR,
//% block="day" enumval=3
DAY,
//% block="month" enumval=4
MONTH,
//% block="year" enumval=5
YEAR
}


let DS1307_I2C_ADDR = 0x68;
let DS1307_REG_SECOND = 0
let DS1307_REG_MINUTE = 1
let DS1307_REG_HOUR = 2
let DS1307_REG_WEEKDAY = 3
let DS1307_REG_DAY = 4
let DS1307_REG_MONTH = 5
let DS1307_REG_YEAR = 6
let DS1307_REG_CTRL = 7
let DS1307_REG_RAM = 8

/**
* set ds1307's reg
*/
function setReg(reg: number, dat: number): void {
let buf = pins.createBuffer(2);
buf[0] = reg;
buf[1] = dat;
pins.i2cWriteBuffer(DS1307_I2C_ADDR, buf);
}

/**
* get ds1307's reg
*/
function getReg(reg: number): number {
pins.i2cWriteNumber(DS1307_I2C_ADDR, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(DS1307_I2C_ADDR, NumberFormat.UInt8BE);
}

/**
* convert a Hex data to Dec
*/
function HexToDec(dat: number): number {
return (dat >> 4) * 10 + (dat % 16);
}

/**
* convert a Dec data to Hex
*/
function DecToHex(dat: number): number {
return Math.idiv(dat, 10) * 16 + (dat % 10)
}

/**
* set time
*/
//% blockId="DS1307_SET_TIME"
//% block="set %datatype |%data"
export function setTime(datatype: TimeType, data: number): void {
switch (datatype) {
case 0:
setReg(DS1307_REG_SECOND, DecToHex(data % 60))
break
case 1:
setReg(DS1307_REG_MINUTE, DecToHex(data % 60))
break
case 2:
setReg(DS1307_REG_HOUR, DecToHex(data % 24))
break
case 3:
setReg(DS1307_REG_DAY, DecToHex(data % 32))
break
case 4:
setReg(DS1307_REG_MONTH, DecToHex(data % 13))
break
case 5:
setReg(DS1307_REG_YEAR, DecToHex(data % 100))
break
default:
break

}
}


/**
* get time
*/
//% blockId="DS1307_GET_TIME"
//% block="%data"
export function getTime(data: TimeType): number {
switch (data) {
case 0:
return HexToDec(getReg(DS1307_REG_SECOND))
break
case 1:
return HexToDec(getReg(DS1307_REG_MINUTE))
break
case 2:
return HexToDec(getReg(DS1307_REG_HOUR))
break
case 3:
return HexToDec(getReg(DS1307_REG_DAY))
break
case 4:
return HexToDec(getReg(DS1307_REG_MONTH))
break
case 5:
return (HexToDec(getReg(DS1307_REG_YEAR)) + 2000)
break
default:
return 0

}
}



/**
* get time
*/
//% blockId="DS1307_GET_TIME"
//% block="%data"

/**
* get weekday
* 0 means Sunday
* 1 means Monday
* 2 means Tuesday
* 3 means Wednesday
* 4 means Thursday
* 5 means Friday
* 6 means Saturday
*/
//% blockId="DS1307_GET_WEEKDAY"
//% block="weekday"
export function getWeekday(): number {
// (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
let d = HexToDec(getReg(DS1307_REG_DAY))
let m = HexToDec(getReg(DS1307_REG_MONTH))
let y = (HexToDec(getReg(DS1307_REG_YEAR)) + 2000)
if (m < 3) {
y = y - 1
m = m + 12
}

let w = d
+ 2 * m
+ Math.idiv(3 * (m + 1), 5)
+ y
+ Math.idiv(y, 4)
- Math.idiv(y, 100)
+ Math.idiv(y, 400)
+ 1
return w % 7
}







/**
* set Date and Time
* @param year is the Year will be set, eg: 2019
* @param month is the Month will be set, eg: 1
* @param day is the Day will be set, eg: 17
* @param hour is the Hour will be set, eg: 12
* @param minute is the Minute will be set, eg: 31
* @param second is the Second will be set, eg: 19
*/
//% blockId="DS1307_SET_DATETIME"
//% block="set year %year| month %month| day %day| hour %hour| minute %minute| second %second"
export function DateTime(year: number, month: number, day: number, hour: number, minute: number, second: number): void {
let buf = pins.createBuffer(8);
buf[0] = DS1307_REG_SECOND;
buf[1] = DecToHex(second % 60);
buf[2] = DecToHex(minute % 60);
buf[3] = DecToHex(hour % 24);
//buf[4] = DecToHex(weekday % 8);
buf[5] = DecToHex(day % 32);
buf[6] = DecToHex(month % 13);
buf[7] = DecToHex(year % 100);
pins.i2cWriteBuffer(DS1307_I2C_ADDR, buf)
}







}
15 changes: 15 additions & 0 deletions enums.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Auto-generated. Do not edit.


declare enum DateTimePart {
Seconds = 0,
Minutes = 1,
Hours = 2,
Day = 3,
Month = 4,
Year = 5,
}
declare namespace Time {
}

// Auto-generated. Do not edit. Really.
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5d6da48

Please sign in to comment.