-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathi2c_ds1307.dart
More file actions
71 lines (62 loc) · 2.02 KB
/
i2c_ds1307.dart
File metadata and controls
71 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:dart_periphery/dart_periphery.dart';
bool confirm() {
while (true) {
stdout.write("Do you want to continue? (yes/no): ");
String? input = stdin.readLineSync()?.trim().toLowerCase();
if (input == 'yes' || input == 'y') {
return true;
} else if (input == 'no' || input == 'n') {
return false;
} else {
print("Invalid input. Please enter 'y(es)' or 'n(o)'.");
}
}
}
/// DS1307/DS3221 real time clock
///
/// https://wiki.seeedstudio.com/Grove-RTC
///
/// Hint: Be aware that many DS1307 modules in circulation operate
/// exclusively at 5V.
void main() {
// Select the right I2C bus number /dev/i2c-?
// 1 for Raspberry Pi, 0 for NanoPi (Armbian), 2 Banana Pi (Armbian), 4 BPI-F3
var i2c = I2C(1);
try {
print('Dart version: ${Platform.version}');
print("dart_periphery Version: $dartPeripheryVersion");
print("c-periphery Version : ${getCperipheryVersion()}");
print('I2C info: ${i2c.getI2Cinfo()}');
print("DS1307 real time clock");
var rtc = DS1307(i2c); // for DS3221 set DS1307(i2c, true)
print("Get current RTC date and time");
print(rtc.getDateTime());
if (rtc.isDS3231) {
print("RTC on board temperature sensor: ${rtc.getTemperature()}");
}
print("Set RTC to current system time?");
if (!confirm()) {
return;
}
var now = DateTime.now();
print(now);
rtc.setDateTime(now);
print("Get current RTC date and time");
print(rtc.getDateTime());
print("Set system time to RTC time? ROOT rights needed!");
if (!confirm()) {
return;
}
if (!setLinuxLocalTime(rtc.getDateTime())) {
print("Failed to set system time. Are you running as root?");
} else {
print("System time successfully set!");
}
} finally {
i2c.dispose();
}
}