Skip to content

Commit

Permalink
Added source
Browse files Browse the repository at this point in the history
  • Loading branch information
jaka committed Mar 7, 2018
1 parent 3e0d25c commit 26ed765
Show file tree
Hide file tree
Showing 8 changed files with 711 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
OPTS := -O2 -s -std=c90 -I./include
CFLAGS += -Wall -Werror -Wextra -fdata-sections -ffunction-sections -fno-strict-aliasing
LDFLAGS += -Wl,--gc-sections
SFLAGS := -R .comment -R .gnu.version -R .note -R .note.ABI-tag

CC = $(CROSS_COMPILE)cc
STRIP = $(CROSS_COMPILE)strip

SOURCES := $(wildcard src/*.c)
OBJECTS := $(patsubst %.c,%.o,$(SOURCES))
TARGET := usbtemp-cli

all: $(OBJECTS) $(TARGET)

%.o: %.c
$(CC) $(CFLAGS) $(OPTS) -c -o $@ $<

$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $^
$(STRIP) $(SFLAGS) $@

clean:
rm -fr $(OBJECTS)
rm -f $(TARGET)
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# usbtemp-cli
Command line interface for acquiring the temperature from usbtemp.com USB Thermometer
Command line interface for obtaining the temperature from usbtemp.com USB Thermometer

Compiled executables are available under Releases tab.

### Linux
This application could be compiled under GNU/Linux with executing `make` command.

### Windows
This application could be also compiled on Windows with a MinGW compiler.

## Usage
```
$ ./usbtemp-cli -h
-r Print ROM
-q Quiet mode
-s Set serial port
```
Serial port could be `/dev/ttyUSB0` or `COM6`.
200 changes: 200 additions & 0 deletions include/linux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>

#include "usbtemp.h"

#define TIMEOUT 1

int ut_errno;

int owReset(int fd)
{
int rv;
int wbytes, rbytes;
unsigned char wbuff, rbuff;
fd_set readset;
struct timeval timeout_tv;
struct termios term;

tcflush(fd, TCIOFLUSH);

if (tcgetattr(fd, &term) < 0) {
ut_errno = 1;
return -1;
}
term.c_cflag &= ~CSIZE | CS8;
cfsetispeed(&term, B9600);
cfsetospeed(&term, B9600);
tcsetattr(fd, TCSANOW, &term);

/* Send the reset pulse. */
wbuff = 0xf0;
wbytes = write(fd, &wbuff, 1);
if (wbytes != 1) {
ut_errno = 9;
return -1;
}

timeout_tv.tv_usec = 0;
timeout_tv.tv_sec = TIMEOUT;

FD_ZERO(&readset);
FD_SET(fd, &readset);

if (select(fd + 1, &readset, NULL, NULL, &timeout_tv) > 0) {

if (FD_ISSET(fd, &readset)) {
rbytes = read(fd, &rbuff, 1);
if (rbytes != 1) {
return -1;
}
switch (rbuff) {
case 0:
/* Ground. */
case 0xf0:
/* No response. */
rv = -1; break;
default:
/* Got a response */
rv = 0;
}
}
else {
rv = -1;
}
}
else {
rv = -1; /* Timed out or interrupt. */
}

term.c_cflag &= ~CSIZE | CS6;
cfsetispeed(&term, B115200);
cfsetospeed(&term, B115200);

tcsetattr(fd, TCSANOW, &term);

return rv;
}

static unsigned char owWriteByte(int fd, unsigned char wbuff)
{
char buf[8];
int wbytes;
unsigned char rbuff, i;
size_t remaining, rbytes;
fd_set readset;
struct timeval timeout_tv;

tcflush(fd, TCIOFLUSH);

for (i = 0; i < 8; i++) {
buf[i] = (wbuff & (1 << (i & 0x7))) ? 0xff : 0x00;
}
wbytes = write(fd, buf, 8);
if (wbytes != 8) {
ut_errno = 9;
return -1;
}

timeout_tv.tv_usec = 0;
timeout_tv.tv_sec = TIMEOUT;

FD_ZERO(&readset);
FD_SET(fd, &readset);

rbuff = 0;
remaining = 8;
while (remaining > 0) {

if (select(fd + 1, &readset, NULL, NULL, &timeout_tv) > 0) {

if (FD_ISSET(fd, &readset)) {
rbytes = read(fd, &buf, remaining);
for (i = 0; i < rbytes; i++) {
rbuff >>= 1;
rbuff |= (buf[i] & 0x01) ? 0x80 : 0x00;
remaining--;
}
}
else {
return 0xff;
}
}
else {
return 0xff;
}
}
return rbuff;
}

unsigned char owRead(int fd)
{
return owWriteByte(fd, 0xff);
}

int owWrite(int fd, unsigned char wbuff)
{
if (owWriteByte(fd, wbuff) != wbuff) {
return -1;
}
return 0;
}

static int file_exists(const char *filename)
{
struct stat st;
return (stat(filename, &st) == 0);
}

int owOpen(const char *serial_port)
{
int fd;
struct termios term;

if (!file_exists(serial_port)) {
ut_errno = 3;
return -1;
}

if (access(serial_port, R_OK|W_OK) < 0) {
ut_errno = 4;
return -1;
}

fd = open(serial_port, O_RDWR);
if (fd < 0) {
ut_errno = 5;
return -1;
}

memset(&term, 0, sizeof(term));

term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
term.c_cflag |= CS6 | CREAD | HUPCL | CLOCAL;

cfsetispeed(&term, B115200);
cfsetospeed(&term, B115200);

if (tcsetattr(fd, TCSANOW, &term) < 0) {
close(fd);
ut_errno = 0;
return -1;
}

tcflush(fd, TCIOFLUSH);

return fd;
}

void owClose(int fd)
{
close(fd);
}
11 changes: 11 additions & 0 deletions include/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _PLATFORM_H
#define _PLATFORM_H

#ifdef _WIN32
#include <windows.h>
#define WINDOWS 1
#else
#define HANDLE int
#endif

#endif
26 changes: 26 additions & 0 deletions include/usbtemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _USBTEMP
#define _USBTEMP

#include "platform.h"

#if WINDOWS
#define DEFAULT_SERIAL_PORT "COM3"
#else
#define DEFAULT_SERIAL_PORT "/dev/ttyUSB0"
#endif

#define DS18X20_GENERATOR 0x8c
#define DS18X20_ROM_SIZE 8
#define DS18X20_SP_SIZE 9

char *DS18B20_errmsg(void);
HANDLE DS18B20_open(const char *);
int DS18B20_measure(HANDLE);
int DS18B20_acquire(HANDLE, float *);
int DS18B20_rom(HANDLE, unsigned char *);
void DS18B20_close(HANDLE);

void wait_1s(void);
int is_fd_valid(HANDLE);

#endif
Loading

0 comments on commit 26ed765

Please sign in to comment.