-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcs.c
112 lines (106 loc) · 3.07 KB
/
cs.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
cs is part of CurrSour, a serial command interface to control current sources.
Copyright (C) 2018 Ronald Sutherland
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License use
http://www.gnu.org/licenses/gpl-2.0.html
*/
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include "../lib/parse.h"
#include "../lib/timers.h"
#include "../lib/pin_num.h"
#include "../lib/pins_board.h"
#include "cs.h"
#define SERIAL_PRINT_DELAY_MILSEC 10000
static unsigned long serial_print_started_at;
// show the current source pin number used
void echo_cs_pin_in_json_rply(uint8_t cs)
{
switch(cs)
{
case 0 :
printf_P(PSTR("PB0")); // CS0
break;
case 1 :
printf_P(PSTR("PD4")); // CS1
break;
case 2 :
printf_P(PSTR("PB3")); // CS2
break;
case 3 :
printf_P(PSTR("PC7")); // CS3
break;
}
}
// pinMode( arg[0], arg[1] )
void CurrSour(void)
{
if ( (command_done == 10) )
{
// check that arg[0] is a digit
if ( ( !( isdigit(arg[0][0]) ) ) )
{
printf_P(PSTR("{\"err\":\"CSarg0NaN\"}\r\n"));
initCommandBuffer();
return;
}
// and arg[0] value is 0..3
uint8_t cs = atoi(arg[0]);
if ( ( cs < 0) || (cs > 3) )
{
printf_P(PSTR("{\"err\":\"CSarg0OutOfRng\"}\r\n"));
initCommandBuffer();
return;
}
// also arg[1] is not ('INPUT' or 'OUTPUT')
if ( !( (strcmp_P( arg[1], PSTR("ON")) == 0) || (strcmp_P( arg[1], PSTR("OFF")) == 0) ) )
{
printf_P(PSTR("{\"err\":\"CSarg1NaMode\"}\r\n"));
initCommandBuffer();
return;
}
serial_print_started_at = millis();
printf_P(PSTR("{\""));
if (strcmp_P( arg[1], PSTR("ON")) == 0 )
{
pinMode(cs_pin_map[cs].pin, OUTPUT);
digitalWrite(cs_pin_map[cs].pin, HIGH);
}
else
{
pinMode(cs_pin_map[cs].pin, OUTPUT);
digitalWrite(cs_pin_map[cs].pin, LOW);
}
command_done = 11;
}
else if ( (command_done == 11) )
{
uint8_t cs = atoi(arg[0]);
echo_cs_pin_in_json_rply( cs );
printf_P(PSTR("\":\""));
command_done = 12;
}
else if ( (command_done == 12) )
{
printf( arg[1] );
printf_P(PSTR("\"}\r\n"));
initCommandBuffer();
}
else
{
printf_P(PSTR("{\"err\":\"CSCmdDnWTF\"}\r\n"));
initCommandBuffer();
}
}