Skip to content

Instantly share code, notes, and snippets.

@hiro88hyo
Created September 1, 2014 03:04
Show Gist options
  • Save hiro88hyo/01ca65a18876ef9c35c7 to your computer and use it in GitHub Desktop.
Save hiro88hyo/01ca65a18876ef9c35c7 to your computer and use it in GitHub Desktop.
SDB onDo Sensor
/*
*
* sdb_on_do.c SDB on DO Sensorのコントロールを行う
* 例によってエラー処理はあまりやってない
*
*/
#include "sdb_on_do.h"
static char *usage_msg =
"usage: %s [options] device\n"
"-l [R|G|B]\t\tLED color R=Red, G=Green, O=Orange\n"
"-p [on|off|fb|sb]\tLED pattern on=On, off=Off, fb=Fast Blink, sb=Slow Blink\n"
"-g [tmp|hum|ilm]\tGet Value tmp=Temperature, hum=Humidity, ilm=Illuminance\n"
"-i Sensor Information\n"
"-h Usgage\n";
/*
* めいん
*/
int main(int argc, char *argv[]){
int ch;
int show_usage = 0;
char *dev;
while((ch = getopt(argc, argv, "l:p:g:hi")) != EOF){
switch (ch) {
default:
case 'd':
break;
case 'l':
led_mode = 1;
switch (optarg[0]){
case 'R': case 'r':
led_color = C_RED;
break;
case 'G': case 'g':
led_color = C_GREEN;
break;
case 'B': case 'b':
led_color = C_BLUE;
break;
default:
show_usage = 1;
break;
}
break;
case 'p':
if (strcmp(optarg, "on") == 0)
led_pattern = P_ON;
else if (strcmp(optarg, "off") == 0)
led_pattern = P_OFF;
else if (strcmp(optarg, "fb") == 0)
led_pattern = P_FB;
else if (strcmp(optarg, "sb") == 0)
led_pattern = P_SB;
else
show_usage = 1;
break;
case 'g':
sense_mode = 1;
switch (optarg[0]){
case 't':
sense_value = S_TEMP;
break;
case 'h':
sense_value = S_HUMID;
break;
case 'i':
sense_value = S_ILM;
break;
}
break;
case 'h':
show_usage = 1;
break;
case 'i':
info_mode = 1;
break;
}
}
if (argc - optind != 1)
show_usage++;
if (show_usage) {
fprintf(stderr, usage_msg, argv[0]);
return -1;
}else{
dev = argv[optind];
sensor_ctrl(dev);
return 0;
}
}
/*
* センサーに接続する
*/
int open_sensor(const char *dev){
int fd;
struct termios tio_new;
fd = open (dev, O_RDWR);
if (fd < 0){
perror(dev);
exit (-1);
}
tcgetattr(fd, &tio_old);
memset(&tio_new, 0x00, sizeof(tio_new));
/*
BAUDRATE: ボーレートの設定
CRTSCTS : 出力のハードウェアフロー制御
CS8 : 8n1 (8 ビット,ストップビット 1)
CLOCAL : ローカル接続,モデム制御なし
CREAD : 受信文字(receiving characters)を有効にする.
*/
tio_new.c_cflag = (speed | CRTSCTS | CS8 | CLOCAL | CREAD);
/*
IGNPAR : パリティエラーのデータは無視する
ICRNL : CR を NL に対応させる(これを行わないと,他のコンピュータで
CR を入力しても,入力が終りにならない)
それ以外の設定では,デバイスは raw モードである(他の入力処理は行わない)
*/
tio_new.c_iflag = (IGNPAR | ICRNL);
tio_new.c_oflag = 0; /* Raw モード */
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio_new);
return fd;
}
/*
* センサーとの接続を解除する
*/
void close_sensor(int fd){
tcsetattr(fd, TCSANOW, &tio_old);
close(fd);
}
/*
* センサーにコマンドを送り、返事を待つ
*/
void write_sensor(int fd, unsigned char *buf, const char *cmd){
char sendchar[6];
int nbytes = 0;
memset(sendchar, 0x00, sizeof(sendchar));
memset(buf, 0x00, sizeof(buf));
sprintf(sendchar, "\x02%3s\x03", cmd);
/* printf("CMD:%s\n", sendchar); */
write(fd, sendchar, SND_SIZE);
while(nbytes < RCV_SIZE){
nbytes += read(fd, buf, RCV_SIZE);
}
}
/*
* センサーから測定値を取得する
*/
float get_sensor_value(int fd){
unsigned char buf[8];
float value;
float rhval;
switch(sense_value){
/* 温度 */
case S_TEMP:
write_sensor(fd, buf, "TMP");
value = (float)(((buf[4] & 0x3f)<<8)+buf[5]) * 0.01 - 40.0;
break;
/* 湿度 */
case S_HUMID:
write_sensor(fd, buf, "HUM");
rhval = (float)((buf[4] & 0x0f)<<8)+buf[5];
value = -4.0 + 0.045 * rhval - 0.0000028 * rhval * rhval;
break;
case S_ILM:
write_sensor(fd, buf, "ILM");
value = (float)(((buf[4] & 0x01)<<8)+buf[5]) * 7.14;
break;
}
/* printf("value: %d, %d\n", buf[4], buf[5]); */
return value;
}
/*
* センサーの情報を取得する
* 未実装
*/
void get_sensor_information(int fd){
}
/*
* LEDの制御を行う
*/
void set_led(int fd){
char cmd[4];
unsigned char buf[8];
memset(cmd, 0x00, sizeof(cmd));
/* LEDの色 */
switch(led_color){
case C_RED: /* 赤 */
strcat(cmd, "R");
break;
case C_GREEN: /* 緑 */
strcat(cmd, "G");
break;
case C_BLUE: /* 橙 */
strcat(cmd, "B");
break;
}
/* 発光パターン */
switch(led_pattern){
case P_ON: /* 点灯 */
strcat(cmd, "ON");
break;
case P_OFF: /* 消灯 */
strcat(cmd, "OF");
break;
case P_FB: /* 早い点滅 */
strcat(cmd, "FB");
break;
case P_SB: /* 遅い点滅 */
strcat(cmd, "SB");
break;
}
write_sensor(fd, buf, cmd);
}
/*
* センサーの全体的な制御
*/
int sensor_ctrl(const char *dev){
int fd;
float value;
fd = open_sensor(dev);
if(info_mode){
/* センサー情報の表示 */
get_sensor_information(fd);
}else{
if(sense_mode){
/* 測定モード */
value = get_sensor_value(fd);
printf("%.2f\n", value);
}
if(led_mode){
/* LEDの操作 */
set_led(fd);
}
}
close_sensor(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
/* LEDの色指定 */
enum led_color_t {
C_RED, /* 赤 */
C_GREEN, /* 緑 */
C_ORANGE, /* 橙 */
C_BLUE, /* 橙 */
};
/* LEDの点灯パターン */
enum led_pattern_t {
P_ON, /* 点灯 */
P_OFF, /* 消灯 */
P_FB, /* 早い点滅 */
P_SB, /* 遅い点滅 */
};
/* センサー値取得 */
enum sense_value_t {
S_TEMP, /* 温度 */
S_HUMID, /* 湿度 */
S_ILM, /* 照度 */
};
/* 受信データと送信データのサイズ(固定長) */
#define RCV_SIZE 7 /* 受信サイズ */
#define SND_SIZE 5 /* 送信サイズ */
static int speed = B19200;
static int info_mode = 0;
static int sense_mode = 0;
static int led_mode = 0;
static enum led_color_t led_color = C_GREEN;
static enum led_pattern_t led_pattern = P_ON;
static enum sense_value_t sense_value = S_TEMP;
struct termios tio_old;
int open_sensor(const char *dev);
void close_sensor(int fd);
void write_sensor(int fd, unsigned char *buf, const char *cmd);
float get_sensor_value(int fd);
void get_sensor_information(int fd);
void set_led(int fd);
int sensor_ctrl(const char *dev);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment