#include #include #include #include #include #include #include #include #include #include #include #define TAG "BuzzerApp" #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args) #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args) #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args) static char pwm_path[100]; extern "C" JNIEXPORT jint JNICALL Java_com_example_serial_BuzzerControl_openPwm(JNIEnv *env, jclass clazz, jstring id) { // TODO: implement openPwm() char *id_native; char temp[100]; int fd; id_native = (char *)env->GetStringUTFChars(id, 0); memset(pwm_path, 0, sizeof (pwm_path)); snprintf(pwm_path, sizeof (pwm_path), "/sys/class/pwm/pwmchip%s/pwm0", id_native); memset(temp, 0, sizeof (temp)); if(access(pwm_path, F_OK)) { snprintf(temp, sizeof (temp), "/sys/class/pwm/pwmchip%s/export", id_native); fd = open(temp, O_WRONLY); if(fd < 0) { LOGE("open %s error:%s\n", temp, strerror(errno)); return -1; } if(1 != write(fd, "0", 1)) { LOGE("Write '0' to pwmchip%s/export error\n", id_native); close(fd); return -2; } close(fd); } env->ReleaseStringUTFChars(id, id_native); return 0; }