summaryrefslogtreecommitdiff
path: root/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c
blob: 701c0c0d31d5f188750663551ed128e9b104fa9f (plain)
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
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h> /* memset */
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include "../Include/serial.h"
#include "../Include/analogv.h"

uint16_t cmd_analog_in_volt(int h,int pin_no)
{	
    char pin[5]="A",v1[2];
    int stat;
    int read[3];
    sprintf(v1,"%c",pin_no+48);
    //strcpy(pin,"A");
    strcat(pin,v1);
    int wr=write_serial(h,pin,2);
    stat=status_serial(h,read);
    //printf("Read:%d\n",read[0] );
    while (read[0] < 2)
        stat=status_serial(h,read);
    char values[5];
    int a_rd=read_serial(h,values,2);
    values[2]='\0';
    printf("%s\n",values);
    int l=strlen(values);
    //printf("%d\n",l );
    uint8_t temp[l+1];
    int i;
    for (i = 0; i < l; ++i)
    {
        temp[i]=(uint8_t)(values[i]);
    }
    //printf("%u %u\n",temp[0],temp[1]);
    uint16_t result=(uint16_t)(256*temp[1]+temp[0]);
    result/=1023;
    //printf("%u\n",result);
    return result;
}

int cmd_analog_out_volt(int h,int pin_no,double val)
{
	char v1[2],v2[2];
	if(val > 5)
    	val = 5;
    else if(val < 0)
        val = 0;
    val=val*255/5;
    //printf("%f\n",val);
    char code_sent[10];
    strcpy(code_sent,"W");
    sprintf(v1,"%c",pin_no+48);
    sprintf(v2,"%c",abs(ceil(val)));
    strcat(code_sent,v1);
    strcat(code_sent,v2);
    //printf("%s\n",code_sent);
    return write_serial(h,code_sent,3);

}