Skip to content

Commit

Permalink
sm8150-common: Move camera motor handling to OnePlusCameraHelper
Browse files Browse the repository at this point in the history
Change-Id: Ie5f8bcdc80ad38a48fc9f1984f02efc2ebd85b91
  • Loading branch information
luk1337 committed Mar 31, 2021
1 parent 9f5cac0 commit dceb335
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 274 deletions.
5 changes: 4 additions & 1 deletion camera_helper/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
</intent-filter>
</receiver>

<service android:name="org.lineageos.camerahelper.FallSensorService"
<service android:name="org.lineageos.camerahelper.CameraMotorService"
android:permission="OnePlusCameraHelperService">
</service>

<service android:name="org.lineageos.camerahelper.FallSensorService"
android:permission="OnePlusCameraHelperService">
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Log.d(TAG, "Starting");
context.startService(new Intent(context, CameraMotorService.class));
context.startService(new Intent(context, FallSensorService.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ public class CameraMotorController {
// Camera motor paths
private static final String CAMERA_MOTOR_ENABLE_PATH =
"/sys/devices/platform/vendor/vendor:motor_pl/enable";
public static final String CAMERA_MOTOR_HALL_CALIBRATION =
"/sys/devices/platform/vendor/vendor:motor_pl/hall_calibration";
private static final String CAMERA_MOTOR_DIRECTION_PATH =
"/sys/devices/platform/vendor/vendor:motor_pl/direction";
private static final String CAMERA_MOTOR_POSITION_PATH =
"/sys/devices/platform/vendor/vendor:motor_pl/position";

// Motor calibration data path
public static final String CAMERA_PERSIST_HALL_CALIBRATION =
"/mnt/vendor/persist/engineermode/hall_calibration";

// Motor fallback calibration data
public static final String HALL_CALIBRATION_DEFAULT =
"170,170,480,0,0,480,500,0,0,500,1500";

// Motor control values
public static final String DIRECTION_DOWN = "0";
public static final String DIRECTION_UP = "1";
Expand All @@ -45,6 +55,23 @@ private CameraMotorController() {
// This class is not supposed to be instantiated
}

public static void calibrate() {
String calibrationData = HALL_CALIBRATION_DEFAULT;

try {
calibrationData = FileUtils.readTextFile(
new File(CAMERA_PERSIST_HALL_CALIBRATION), 0, null);
} catch (IOException e) {
Log.e(TAG, "Failed to read " + CAMERA_PERSIST_HALL_CALIBRATION, e);
}

try {
FileUtils.stringToFile(CAMERA_MOTOR_HALL_CALIBRATION, calibrationData);
} catch (IOException e) {
Log.e(TAG, "Failed to write to " + CAMERA_MOTOR_HALL_CALIBRATION, e);
}
}

public static void setMotorDirection(String direction) {
try {
FileUtils.stringToFile(CAMERA_MOTOR_DIRECTION_PATH, direction);
Expand All @@ -65,7 +92,7 @@ public static String getMotorPosition() {
try {
return FileUtils.readTextFile(new File(CAMERA_MOTOR_POSITION_PATH), 1, null);
} catch (IOException e) {
Log.e(TAG, "Failed to read to " + CAMERA_MOTOR_POSITION_PATH, e);
Log.e(TAG, "Failed to read " + CAMERA_MOTOR_POSITION_PATH, e);
}
return null;
}
Expand Down
117 changes: 117 additions & 0 deletions camera_helper/src/org/lineageos/camerahelper/CameraMotorService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (C) 2019 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.lineageos.camerahelper;

import android.annotation.NonNull;
import android.app.Service;
import android.content.Intent;
import android.hardware.camera2.CameraManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

public class CameraMotorService extends Service implements Handler.Callback {
private static final boolean DEBUG = true;
private static final String TAG = "CameraMotorService";

public static final int CAMERA_EVENT_DELAY_TIME = 100; // ms

public static final String FRONT_CAMERA_ID = "1";

public static final int MSG_CAMERA_CLOSED = 1000;
public static final int MSG_CAMERA_OPEN = 1001;

private Handler mHandler = new Handler(this);

private long mClosedEvent;
private long mOpenEvent;

private CameraManager.AvailabilityCallback mAvailabilityCallback =
new CameraManager.AvailabilityCallback() {
@Override
public void onCameraAvailable(@NonNull String cameraId) {
super.onCameraAvailable(cameraId);

if (cameraId.equals(FRONT_CAMERA_ID)) {
mClosedEvent = SystemClock.elapsedRealtime();
if (SystemClock.elapsedRealtime() - mOpenEvent < CAMERA_EVENT_DELAY_TIME
&& mHandler.hasMessages(MSG_CAMERA_OPEN)) {
mHandler.removeMessages(MSG_CAMERA_OPEN);
}
mHandler.sendEmptyMessageDelayed(MSG_CAMERA_CLOSED,
CAMERA_EVENT_DELAY_TIME);
}
}

@Override
public void onCameraUnavailable(@NonNull String cameraId) {
super.onCameraAvailable(cameraId);

if (cameraId.equals(FRONT_CAMERA_ID)) {
mOpenEvent = SystemClock.elapsedRealtime();
if (SystemClock.elapsedRealtime() - mClosedEvent < CAMERA_EVENT_DELAY_TIME
&& mHandler.hasMessages(MSG_CAMERA_CLOSED)) {
mHandler.removeMessages(MSG_CAMERA_CLOSED);
}
mHandler.sendEmptyMessageDelayed(MSG_CAMERA_OPEN,
CAMERA_EVENT_DELAY_TIME);
}
}
};

@Override
public void onCreate() {
CameraMotorController.calibrate();

CameraManager cameraManager = getSystemService(CameraManager.class);
cameraManager.registerAvailabilityCallback(mAvailabilityCallback, null);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (DEBUG) Log.d(TAG, "Starting service");
return START_STICKY;
}

@Override
public void onDestroy() {
if (DEBUG) Log.d(TAG, "Destroying service");
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_CAMERA_CLOSED:
CameraMotorController.setMotorDirection(CameraMotorController.DIRECTION_DOWN);
CameraMotorController.setMotorEnabled();
break;
case MSG_CAMERA_OPEN:
CameraMotorController.setMotorDirection(CameraMotorController.DIRECTION_UP);
CameraMotorController.setMotorEnabled();
break;
}
return true;
}
}
33 changes: 0 additions & 33 deletions camera_motor/Android.bp

This file was deleted.

131 changes: 0 additions & 131 deletions camera_motor/CameraMotor.cpp

This file was deleted.

Loading

0 comments on commit dceb335

Please sign in to comment.