Skip to content

Commit

Permalink
Add HTC charging LED support
Browse files Browse the repository at this point in the history
Original implementation by Tom Hite, only slightly modified by Matt
Mower.

Change-Id: Ibc73bf04f122f5de1702f8beeccc468a9a1a2e95
  • Loading branch information
Tom Hite authored and mdmower committed Apr 17, 2014
1 parent ed71fa3 commit 8d37e48
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 1 deletion.
17 changes: 16 additions & 1 deletion gui/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ LOCAL_SRC_FILES := \
blanktimer.cpp \
partitionlist.cpp \
mousecursor.cpp

ifneq ($(TW_HTC_LED),)
LOCAL_SRC_FILES += batteryled.cpp
endif
ifneq ($(TWRP_CUSTOM_KEYBOARD),)
LOCAL_SRC_FILES += $(TWRP_CUSTOM_KEYBOARD)
else
Expand Down Expand Up @@ -63,6 +65,19 @@ endif
ifeq ($(TW_OEM_BUILD),true)
LOCAL_CFLAGS += -DTW_OEM_BUILD
endif
ifneq ($(TW_HTC_LED),)
LOCAL_CFLAGS += -DTW_HTC_LED
ifneq ($(TW_HTC_CHARGING_LED_PATH),)
LOCAL_CFLAGS += -DTW_HTC_CHARGING_LED_PATH=$(TW_HTC_CHARGING_LED_PATH)
else
LOCAL_CFLAGS += -DTW_HTC_CHARGING_LED_PATH="/sys/class/leds/amber/brightness"
endif
ifneq ($(TW_HTC_CHARGED_LED_PATH),)
LOCAL_CFLAGS += -DTW_HTC_CHARGED_LED_PATH=$(TW_HTC_CHARGED_LED_PATH)
else
LOCAL_CFLAGS += -DTW_HTC_CHARGED_LED_PATH="/sys/class/leds/green/brightness"
endif
endif

ifeq ($(DEVICE_RESOLUTION),)
$(warning ********************************************************************************)
Expand Down
140 changes: 140 additions & 0 deletions gui/batteryled.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2014 Tom Hite (for TeamWin)
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/

using namespace std;

#include <string>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

extern "C"
{
#include "../twcommon.h"
}
#include "../twrp-functions.hpp"
#include "batteryled.hpp"

static char getChargingStatus() {
char cap_s[2];

#ifdef TW_CUSTOM_BATTERY_PATH
string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
status_file += "/status";
FILE * cap = fopen(status_file.c_str(),"rt");
#else
FILE * cap = fopen("/sys/class/power_supply/battery/status","rt");
#endif
if (cap) {
fgets(cap_s, 2, cap);
fclose(cap);
} else {
cap_s[0] = 'N';
}

return cap_s[0];
}

static bool setCharging() {
bool charging = false;
int success = -1;

char status = getChargingStatus();

string charging_file = EXPAND(TW_HTC_CHARGING_LED_PATH);
string full_file = EXPAND(TW_HTC_CHARGED_LED_PATH);
string status_charging;
string status_full;

switch (status) {
case 'C':
status_charging = "1";
status_full = "0";
charging = true;
break;
case 'F':
status_charging = "0";
status_full = "1";
break;
default:
status_charging = "0";
status_full = "0";
break;
}

#ifdef _EVENT_LOGGING
LOGINFO("Writing '%s' to battery led: %s\n", status_charging.c_str(), charging_file.c_str());
LOGINFO("Writing '%s' to battery led: %s\n", status_full.c_str(), full_file.c_str());
#endif
success = TWFunc::write_file(charging_file, status_charging);
if (success != 0) {
LOGERR("Failed writing to battery led: %s\n", charging_file.c_str());
} else {
success = TWFunc::write_file(full_file, status_full);
if (success != 0) {
LOGERR("Failed writing to battery led: %s\n", full_file.c_str());
}
}

return (success == 0) ? charging : false;
}

batteryled::batteryled(void) {
setDelay(DEFAULT_CHECK_DELAY_SECONDS);
pthread_mutex_init(&delaymutex, NULL);
}

void batteryled::setDelay(int newdelay) {
pthread_mutex_lock(&delaymutex);

// Restrict range of delays
if (newdelay < MIN_CHECK_DELAY_SECONDS) {
newdelay = MIN_CHECK_DELAY_SECONDS;
} else if (newdelay > MAX_CHECK_DELAY_SECONDS) {
newdelay = MAX_CHECK_DELAY_SECONDS;
}
checkDelay = newdelay;

pthread_mutex_unlock(&delaymutex);
}

int batteryled::setTimerThread(void) {
int success;
pthread_t thread;
ThreadPtr battptr = &batteryled::startBatteryCheckLoop;
PThreadPtr p = *(PThreadPtr*)&battptr;
success = pthread_create(&thread, NULL, p, this);
if (success != 0) {
LOGERR("TWRP battery check thread cannot start; pthread_create failed with return %d\n", success);
}
return 0;
}

int batteryled::startBatteryCheckLoop(void) {
LOGINFO("Entering TWRP battery check thread loop with delay timer set to %d sec\n", checkDelay);

// sanity check checkDelay
while (checkDelay != 0 && checkDelay >= MIN_CHECK_DELAY_SECONDS && checkDelay <= MAX_CHECK_DELAY_SECONDS) {
usleep(checkDelay * 1000000);
setCharging();
}

LOGERR("Exiting TWRP battery check thread because delay timer (%d sec) set outside of allowed range (%d-%d sec)\n", checkDelay, MIN_CHECK_DELAY_SECONDS, MAX_CHECK_DELAY_SECONDS);
return 0;
}

49 changes: 49 additions & 0 deletions gui/batteryled.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2014 Tom Hite (for TeamWin)
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __BATTERYLED_HEADER_HPP
#define __BATTERYLED_HEADER_HPP

#include <pthread.h>
#include <sys/time.h>

#define MIN_CHECK_DELAY_SECONDS 2
#define DEFAULT_CHECK_DELAY_SECONDS 2
#define MAX_CHECK_DELAY_SECONDS 10

using namespace std;

class batteryled
{
public:
batteryled(void);

int setTimerThread(void);
void setDelay(int newdelay);

private:
typedef int (batteryled::*ThreadPtr)(void);
typedef void* (*PThreadPtr)(void*);

int startBatteryCheckLoop(void);

pthread_mutex_t delaymutex;
unsigned int checkDelay;
};

#endif // __BATTERYLED_HEADER_HPP
11 changes: 11 additions & 0 deletions gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ extern "C"
#ifndef TW_NO_SCREEN_TIMEOUT
#include "blanktimer.hpp"
#endif
#ifdef TW_HTC_LED
#include "batteryled.hpp"
#endif

// Enable to print render time of each frame to the log file
//#define PRINT_RENDER_TIME 1
Expand All @@ -76,6 +79,9 @@ static int gGuiInputRunning = 0;
#ifndef TW_NO_SCREEN_TIMEOUT
blanktimer blankTimer;
#endif
#ifdef TW_HTC_LED
batteryled batteryLed;
#endif

// Needed by pages.cpp too
int gGuiRunning = 0;
Expand Down Expand Up @@ -199,6 +205,11 @@ static void * input_thread(void *cookie)
#else
LOGINFO("Skipping screen timeout threads: TW_NO_SCREEN_TIMEOUT is set\n");
#endif
#ifdef TW_HTC_LED
/* setup the battery led indicator thread */
batteryLed.setDelay(2); // in seconds
batteryLed.setTimerThread();
#endif

for (;;)
{
Expand Down

0 comments on commit 8d37e48

Please sign in to comment.