#include <wiringPiSPI.h>
#include <stdio.h>

const unsigned char START = 1 << 2; // start bit
const unsigned char SGL   = 1 << 1; // SGL (絶対値)
const unsigned char D2    = 1;      // チャネル選択 (2ビット目)

const int CLOCK_SPEED = 1000000; // 1 MHz

int main()
{
    int fd;
    unsigned char buffer[3];
    unsigned short tmp;
    double result;

    fd = wiringPiSPISetup(0, CLOCK_SPEED); // 0はチャンネル番号

    buffer[0] = START | SGL; // 送信するデータ
    buffer[1] = 0;
    buffer[2] = 0;

    wiringPiSPIDataRW(0, buffer, 3); // SPIデータの送受信

    tmp = (unsigned short)(buffer[1] & 0x0f) << 8 | (unsigned short)buffer[0];
    result = (double)tmp / 4096.0 * 100.0;

    printf("Result: %lf\n", result);

    return 0;
}