Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyingcai committed Dec 8, 2020
1 parent 9d23b5f commit 911526a
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Replace model in `/etc/default/pisugar-server`
sed -e "s|--model '.*' |--model '<model>' |"
-i /etc/default/pisugar-server

**NOTE** UPS mode would prevent PiSugar falling into sleep, it could be useful in some cases. (since v1.4.8, `/etc/pisugar-server/config.json`)
**NOTE** `auto_power_on` mode would prevent PiSugar falling into sleep, it could be useful in some cases. (since v1.4.8, `/etc/pisugar-server/config.json`)

## Prerequisites

Expand Down
2 changes: 1 addition & 1 deletion pisugar-core/src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Instant;
/// Battery chip controller
pub trait Battery {
/// Init battery chip
fn init(&mut self, ups: bool) -> Result<()>;
fn init(&mut self, auto_power_on: bool) -> Result<()>;

/// Model
fn model(&self) -> String;
Expand Down
10 changes: 5 additions & 5 deletions pisugar-core/src/ip5209.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl IP5209 {
}

/// Shutdown under light load (144mA and 8s)
pub fn enable_auto_shutdown(&self) -> Result<()> {
pub fn enable_light_load_auto_shutdown(&self) -> Result<()> {
let threshold = PI_ZERO_IDLE_INTENSITY * 1000.0;
let threshold = (threshold / 12.0) as u64;
let threshold = if threshold > 0b0001_1111 {
Expand Down Expand Up @@ -228,7 +228,7 @@ impl IP5209 {
/// Force shutdown
pub fn force_shutdown(&self) -> Result<()> {
// enable auto shutdown
self.enable_auto_shutdown()?;
self.enable_light_load_auto_shutdown()?;

// force shutdown
let mut t = self.i2c.smbus_read_byte(0x01)?;
Expand Down Expand Up @@ -263,16 +263,16 @@ impl IP5209Battery {
}

impl Battery for IP5209Battery {
fn init(&mut self, ups: bool) -> Result<()> {
fn init(&mut self, auto_power_on: bool) -> Result<()> {
if self.model.led_amount() == 2 {
self.ip5209.init_gpio_2led()?;
self.ip5209.toggle_allow_charging_2led(true)?;
} else {
self.ip5209.init_gpio()?;
}
// NOTE: Disable auto shutdown in UPS
self.ip5209.enable_auto_shutdown()?;
if ups {
self.ip5209.enable_light_load_auto_shutdown()?;
if auto_power_on {
self.ip5209.disable_light_load_shutdown()?;
}

Expand Down
10 changes: 5 additions & 5 deletions pisugar-core/src/ip5312.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl IP5312 {
}

/// Shutdown under light load (126mA and 8s)
pub fn enable_auto_shutdown(&self) -> Result<()> {
pub fn enable_light_load_auto_shutdown(&self) -> Result<()> {
let threshold = PI_PRO_IDLE_INTENSITY * 1000.0;
let threshold = (threshold / 4.3) as u64;
let threshold = if threshold > 0b0011_1111 {
Expand Down Expand Up @@ -231,7 +231,7 @@ impl IP5312 {
/// Force shutdown
pub fn force_shutdown(&self) -> Result<()> {
// enable auto shutdown
self.enable_auto_shutdown()?;
self.enable_light_load_auto_shutdown()?;

// enable force shutdown
let mut t = self.i2c.smbus_read_byte(0x01)?;
Expand Down Expand Up @@ -266,7 +266,7 @@ impl IP5312Battery {
}

impl Battery for IP5312Battery {
fn init(&mut self, ups: bool) -> Result<()> {
fn init(&mut self, auto_power_on: bool) -> Result<()> {
if self.model.led_amount() == 2 {
self.ip5312.init_gpio_2led()?;
self.ip5312.toggle_allow_charging_2led(true)?;
Expand All @@ -275,8 +275,8 @@ impl Battery for IP5312Battery {
}
self.ip5312.init_boost_intensity()?;
// NOTE: Disable auto shutdown in UPS
self.ip5312.enable_auto_shutdown()?;
if ups {
self.ip5312.enable_light_load_auto_shutdown()?;
if auto_power_on {
self.ip5312.disable_light_load_shutdown()?;
}

Expand Down
8 changes: 4 additions & 4 deletions pisugar-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub struct PiSugarConfig {
pub full_charge_duration: Option<u64>,

#[serde(default)]
pub ups: Option<bool>,
pub auto_power_on: Option<bool>,
}

impl PiSugarConfig {
Expand Down Expand Up @@ -289,7 +289,7 @@ impl PiSugarCore {
fn init_battery(&mut self) -> Result<()> {
if self.battery.is_none() {
let mut battery = self.model.bind(I2C_ADDR_BAT)?;
battery.init(self.config.ups.unwrap_or(false))?;
battery.init(self.config.auto_power_on.unwrap_or(false))?;
self.battery = Some(battery);
}
Ok(())
Expand All @@ -298,7 +298,7 @@ impl PiSugarCore {
fn init_rtc(&mut self) -> Result<()> {
if self.rtc.is_none() {
let rtc = SD3078::new(I2C_ADDR_RTC)?;
rtc.init(self.config.ups.unwrap_or(false))?;
rtc.init(self.config.auto_power_on.unwrap_or(false))?;
self.rtc = Some(rtc);
}
Ok(())
Expand Down Expand Up @@ -450,7 +450,7 @@ impl PiSugarCore {
}

pub fn set_alarm(&self, t: SD3078Time, weekday_repeat: u8) -> Result<()> {
if self.config.ups == Some(true) {
if self.config.auto_power_on == Some(true) {
return Err(Error::Other("UPS is in conflict with alarm function".to_string()));
}
call_rtc!(&self.rtc, set_alarm, t, weekday_repeat)
Expand Down
6 changes: 3 additions & 3 deletions pisugar-core/src/sd3078.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ impl SD3078 {
}

/// Init
pub fn init(&self, ups: bool) -> Result<()> {
pub fn init(&self, auto_power_on: bool) -> Result<()> {
self.clear_alarm_flag()?;

// NOTE enable UPS frequency alarm
if ups {
// NOTE enable frequency alarm
if auto_power_on {
self.set_frequency_alarm()?;
}

Expand Down
1 change: 1 addition & 0 deletions pisugar-module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
18 changes: 18 additions & 0 deletions pisugar-module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
obj-m := rtc-sd3078.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL)
BUILD_DIR := $(shell pwd)/build
BUILD_DIR_MAKEFILE := $(PWD)/build/Makefile

all: $(BUILD_DIR_MAKEFILE)
$(MAKE) -C $(LINUX_KERNEL_PATH) M=$(BUILD_DIR) src=$(PWD) modules

$(BUILD_DIR):
mkdir -p "$@"

$(BUILD_DIR_MAKEFILE): $(BUILD_DIR)
touch "$@"

clean:
$(MAKE) -C $(LINUX_KERNEL_PATH) M=$(BUILD_DIR) src=$(PWD) clean
9 changes: 9 additions & 0 deletions pisugar-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Linux kernel modules

Make kernel modules:

make

## RTC

rtc-sd3078.c https://github.com/torvalds/linux/blob/master/drivers/rtc/rtc-sd3078.c
Loading

0 comments on commit 911526a

Please sign in to comment.