File: vibrator.cpp

package info (click to toggle)
literki 0.0.0+20100113.git1da40724-1.2
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 3,444 kB
  • ctags: 1,153
  • sloc: cpp: 5,263; makefile: 72
file content (65 lines) | stat: -rw-r--r-- 1,406 bytes parent folder | download | duplicates (2)
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
#include <pthread.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <cstring>

#include "vibrator.h"
#include "external/callback.h"
#include "logger.h"

using namespace std;

struct RunArg {
    Closure* c;
    int delay_micros;
    RunArg(Closure* c_, int d):c(c_), delay_micros(d){}
};

static void* RunFun(void *arg){
    RunArg* a = (RunArg*) arg;
    usleep(a->delay_micros);
    a->c->Run();
    delete a;
}

static void RunLater(Closure* c, int delay_micros){
    pthread_attr_t tattr;
    pthread_attr_init(&tattr);
    pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);
    pthread_t p;
    int err = pthread_create(&p, &tattr, RunFun, new RunArg(c, delay_micros));
    if (err != 0) {
	LOG(FATAL) << "pthread_create error: " << strerror(err);
    }
}

void Vibrator::SetVibTime(int time){
    vibtime = time;
}

void Vibrator::SetVibPower(int power){
    vibpower = power;
}

static void VibrateNow(int power, int time){
    LOG(INFO) << "VIB " << time << " " << power;
    ofstream fout("/sys/class/leds/neo1973:vibrator/brightness");
    if (!fout.good())
	return;
    fout << power <<endl;
    fout.flush();

    usleep(time *1000);

    fout << 0 << endl;
    fout.close();
} 

Vibrator::Vibrator(int time, int power):vibtime(time), vibpower(power){
}

void Vibrator::Vibrate(){
    RunLater(NewCallback(VibrateNow, vibpower, vibtime), 0);
}