1app
import android.os.Vibrator;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private Vibrator vibrator=null;
vibrator=(Vibrator)this.getSystemService(VIBRATOR_SERVICE);
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton1.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.i(TAG,"toggleButton1 enter vibrator.vibrate");
vibrator.vibrate(new long[]{1000, 10, 100, 1000},-1);
}else{
Log.i(TAG,"toggleButton1 enter vibrator.cancel()");
vibrator.cancel();
}
}
});
}
}
2fwk
@Override
public void vibrate(int uid, String opPkg, VibrationEffect effect, int usageHint,
IBinder token) {
Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "vibrate");
try {
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires VIBRATE permission");
}
。。。。
synchronized (mLock) {
。。。
Vibration vib = new Vibration(token, effect, usageHint, uid, opPkg);
linkVibration(vib);
long ident = Binder.clearCallingIdentity();
try {
doCancelVibrateLocked();
startVibrationLocked(vib);
addToPreviousVibrationsLocked(vib);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR);
}
}
startVibrationLocked->startVibrationInnerLocked->doVibratorOn->vibrateOn->VibratorThread->playWaveform->doVibratorOn->vibrateOn
3jni
static void vibratorOn(JNIEnv* , jobject , jlong timeout_ms)
{
Status retStatus = halCall(&V1_0::IVibrator::on, timeout_ms).withDefault(Status::UNKNOWN_ERROR);
if (retStatus != Status::OK) {
ALOGE("vibratorOn command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
}
}
static void vibratorOff(JNIEnv* , jobject )
{
Status retStatus = halCall(&V1_0::IVibrator::off).withDefault(Status::UNKNOWN_ERROR);
if (retStatus != Status::OK) {
ALOGE("vibratorOff command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
}
}
static const JNINativeMethod method_table[] = {
{ "vibratorExists", "()Z", (void*)vibratorExists },
{ "vibratorInit", "()V", (void*)vibratorInit },
{ "vibratorOn", "(J)V", (void*)vibratorOn },
{ "vibratorOff", "()V", (void*)vibratorOff },
{ "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl},
{ "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude},
{ "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect}
};
int register_android_server_VibratorService(JNIEnv *env)
{
return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
method_table, NELEM(method_table));
}
4 hidl
Return<Status> Vibrator::on(uint32_t timeout_ms) {
int32_t ret = mDevice->vibrator_on(mDevice, timeout_ms);
if (ret != 0) {
ALOGE("on command failed : %s", strerror(-ret));
return Status::UNKNOWN_ERROR;
}
return Status::OK;
}
Return<Status> Vibrator::off() {
int32_t ret = mDevice->vibrator_off(mDevice);
if (ret != 0) {
ALOGE("off command failed : %s", strerror(-ret));
return Status::UNKNOWN_ERROR;
}
return Status::OK;
}
5hal
static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable";
static int sendit(unsigned int timeout_ms)
{
char value[TIMEOUT_STR_LEN];
snprintf(value, sizeof(value), "%u", timeout_ms);
return write_value(THE_DEVICE, value);
}
static int vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
{
return sendit(timeout_ms);
}
static int vibra_off(vibrator_device_t* vibradev __unused)
{
return sendit(0);
}
static int vibra_open(const hw_module_t* module, const char* id __unused,
hw_device_t** device __unused) {
bool use_led;
if (vibra_exists()) {
ALOGD("Vibrator using timed_output");
use_led = false;
} else if (vibra_led_exists()) {
ALOGD("Vibrator using LED trigger");
use_led = true;
} else {
ALOGE("Vibrator device does not exist. Cannot start vibrator");
return -ENODEV;
}
vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t));
if (!vibradev) {
ALOGE("Can not allocate memory for the vibrator device");
return -ENOMEM;
}
vibradev->common.tag = HARDWARE_DEVICE_TAG;
vibradev->common.module = (hw_module_t *) module;
vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
vibradev->common.close = vibra_close;
if (use_led) {
vibradev->vibrator_on = vibra_led_on;
vibradev->vibrator_off = vibra_led_off;
} else {
vibradev->vibrator_on = vibra_on;
vibradev->vibrator_off = vibra_off;
}
*device = (hw_device_t *) vibradev;
return 0;
}
其实开启和关闭马达的工作很简单,就是往节点"/sys/class/timed_output/vibrator/enable"写入震动时间,所以可以想得到驱动层只需要提供一个节点供上层操作就好
4 driver
马达的驱动是基于kernel提供的timed_output框架完成的:
代码比较简单,提供接口给驱动在"/sys/class/timed_output/"路径下面建立自己的节点,并提供节点的device attribute的操作接口,当我们写节点的时候就会调用到enable_store函数,并调用注册驱动的enable函数。
static struct class *timed_output_class;
static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t size)
{
struct timed_output_dev *tdev = dev_get_drvdata(dev);
int value;
int rc;
rc = kstrtoint(buf, 0, &value);
if (rc != 0)
return -EINVAL;
tdev->enable(tdev, value);
return size;
}
static DEVICE_ATTR_RW(enable);
static struct attribute *timed_output_attrs[] = {
&dev_attr_enable.attr,
NULL,
};
ATTRIBUTE_GROUPS(timed_output);
static int create_timed_output_class(void)
{
if (!timed_output_class) {
timed_output_class = class_create(THIS_MODULE, "timed_output");
if (IS_ERR(timed_output_class))
return PTR_ERR(timed_output_class);
atomic_set(&device_count, 0);
timed_output_class->dev_groups = timed_output_groups;
}
return 0;
}
int timed_output_dev_register(struct timed_output_dev *tdev)
{
int ret;
if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
return -EINVAL;
ret = create_timed_output_class();
if (ret < 0)
return ret;
tdev->index = atomic_inc_return(&device_count);
tdev->dev = device_create(timed_output_class, NULL,
MKDEV(0, tdev->index), NULL, "%s", tdev->name);
if (IS_ERR(tdev->dev))
return PTR_ERR(tdev->dev);
dev_set_drvdata(tdev->dev, tdev);
tdev->state = 0;
return 0;
}
看一下基于上面框架书写的马达驱动:
static void vibrator_off(void)
{
gpio_direction_output(gpio, !en_value);
wake_unlock(&vibdata.wklock);
}
void motor_enable(struct timed_output_dev *sdev,int value)
{
mutex_lock(&vibdata.lock);
hrtimer_cancel(&vibdata.timer);
cancel_work_sync(&vibdata.work);
if(value)
{
wake_lock(&vibdata.wklock);
gpio_direction_output(gpio, en_value);
if(value > 0)
{
if(value > MAX_TIMEOUT)
value= MAX_TIMEOUT;
hrtimer_start(&vibdata.timer,
ktime_set(value / 1000, (value % 1000) * 1000000),
HRTIMER_MODE_REL);
}
}
else
vibrator_off();
mutex_unlock(&vibdata.lock);
}
struct timed_output_dev motot_driver = {
.name ="vibrator",
.enable= motor_enable,
.get_time= get_time,
};
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
schedule_work(&vibdata.work);
return HRTIMER_NORESTART;
}
static void vibrator_work(struct work_struct *work)
{
vibrator_off();
}
static int motor_probe(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
enum of_gpio_flags flags;
int ret =0;
hrtimer_init(&vibdata.timer,CLOCK_MONOTONIC, HRTIMER_MODE_REL);
vibdata.timer.function= vibrator_timer_func;
INIT_WORK(&vibdata.work,vibrator_work);
...
ret=timed_output_dev_register(&motot_driver);
if (ret< 0)
goto err_to_dev_reg;
return 0;
err_to_dev_reg:
mutex_destroy(&vibdata.lock);
wake_lock_destroy(&vibdata.wklock);
printk("vibrator err!:%d\n",ret);
return ret;
}
- 驱动接收上层传递过来的是震动时长,单位为毫秒。在驱动里注册一个定时器,定时器倒计时到期后会唤醒注册的工作队列,最终会执行vibrator_work()函数去关闭马达震动。
- 调用timed_output框架提供的timed_output_dev_register()接口将我们的马达驱动注册进系统,这里的关键就是我们需要自定义struct timed_output_dev结构体,填充enable和get_time函数。enable函数用来开启马达的震动:
void motor_enable(struct timed_output_dev *sdev,int value)
{
mutex_lock(&vibdata.lock);
hrtimer_cancel(&vibdata.timer);
cancel_work_sync(&vibdata.work);
if(value)
{
wake_lock(&vibdata.wklock);
gpio_direction_output(gpio, en_value);
if(value > 0)
{
if(value > MAX_TIMEOUT)
value= MAX_TIMEOUT;
hrtimer_start(&vibdata.timer,
ktime_set(value / 1000, (value % 1000) * 1000000),
HRTIMER_MODE_REL);
}
}
else
vibrator_off();
mutex_unlock(&vibdata.lock);
}
开启震动的操作也很简单,只是写一下GPIO,然后重新开启定时器,倒计时的时间就是写入节点的值,到时间再把马达关闭就好。
|