-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput_module_10ch.c
More file actions
135 lines (109 loc) · 5.31 KB
/
Copy pathinput_module_10ch.c
File metadata and controls
135 lines (109 loc) · 5.31 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**************************************************************************************
* \file input_module_10ch.c
* \brief Read all 10 channels of a 10-channel input module on slot 1.
*
* Each channel is configured as:
* - Function : analog mV resolution (INPUTFUNC_MVANALOG)
* - Pull-up : 10 kΩ (INPUTPULLUP10CH_10K)
* - Pull-down : 3.3 kΩ (INPUTPULLDOWN10CH_3_3K)
*
* Sensor supply:
* - Supply 1 : INPUTSENSSUPPLYON (5 V output)
* - Supply 2 : INPUTSENSSUPPLYON (5 V output)
*
* The measured value of each channel is available in millivolts via
* inputModule.value[channel] after calling GO_module_input_receive_values().
*
* This example demonstrates:
* - Setting up a 10-channel input module (type, slot, channel config)
* - Enabling the 5 V sensor supply outputs
* - Sending the configuration to the module hardware
* - Reading channel values in the application loop
* - Printing all channels at a reduced rate (once per second)
* - Clean shutdown on SIGTERM / SIGINT (Ctrl+C)
**************************************************************************************/
#include <stdint.h>
#include <unistd.h>
#include "GO_board.h"
#include "GO_communication_modules.h"
#include "GO_module_input.h"
#include "print.h"
/* Number of channels on this module type */
#define NUM_CHANNELS 10
/* Print interval: 100 cycles × 10 ms = 1 s */
#define PRINT_INTERVAL_CYCLES 100
/* -------------------------------------------------------------------------
* Module instance — holds configuration and live measurement values.
* ------------------------------------------------------------------------- */
static _inputModule inputModule;
/* -------------------------------------------------------------------------
* Shutdown callback — registered via GO_board_exit_program().
* ------------------------------------------------------------------------- */
static void app_terminate(void)
{
info("Shutting down\n");
}
/* -------------------------------------------------------------------------
* main
* ------------------------------------------------------------------------- */
int main(void)
{
info("=== 10-channel input module example ===\n");
info("Slot 1 | Function: analog mV | Pull-up: 10k | Pull-down: 3.3k\n");
info("All 10 channels printed once per second. Press Ctrl+C to stop.\n\n");
/* Detect the hardware variant (required before any module function). */
GO_board_get_hardware_version();
/* --- Module setup ---------------------------------------------------- */
/* Assign module type and slot.
* The slot index matches the physical slot on the controller (MODULESLOT1 = slot 1). */
GO_module_input_set_module_type(&inputModule, INPUTMODULE10CHANNEL);
GO_module_input_set_module_slot(&inputModule, MODULESLOT1);
/* Initialise the SPI communication for this slot. */
GO_communication_modules_initialize(MODULESLOT1);
/* Configure all 10 channels identically:
* INPUTFUNC_MVANALOG — returns the measured voltage in millivolts
* INPUTPULLUP10CH_10K — enable 10 kΩ pull-up resistor (10-channel module)
* INPUTPULLDOWN10CH_3_3K — enable 3.3 kΩ pull-down resistor (10-channel module)
*
* Use the INPUTPULLUP10CH_* / INPUTPULLDOWN10CH_* macros for this module type.
* The INPUTPULLUP6CH_* / INPUTPULLDOWN6CH_* macros are for the 6-channel module only. */
for (uint8_t ch = INPUTCHANNEL1; ch <= INPUTCHANNEL10; ch++) {
GO_module_input_10ch_configure_channel(&inputModule, ch,
INPUTFUNC_MVANALOG,
INPUTPULLUP10CH_10K,
INPUTPULLDOWN10CH_3_3K);
}
/* Enable both 5 V sensor supply outputs.
* Must be called before GO_module_input_configuration() so the supply state
* is included in the first configuration frame sent to the module.
* Supply 2 is only active on modules with firmware >= VERSIONSECONDSUPPLY_10CHANNELIN. */
GO_module_input_10ch_configure_supply(&inputModule,
INPUTSENSSUPPLYON, /* supply 1 — active */
INPUTSENSSUPPLYON); /* supply 2 — active */
/* Send the full configuration to the module over SPI.
* Must be called once after all channels and supplies are configured and before
* the first GO_module_input_receive_values() call. */
GO_module_input_configuration(&inputModule);
info("Module configured and communication initialised\n\n");
/* Register the shutdown callback for SIGTERM and SIGINT. */
GO_board_exit_program(app_terminate);
/* --- Application loop (10 ms cycle) ---------------------------------- */
int cycle = 0;
while (1) {
/* Fetch the latest values from the module via SPI.
* Results are stored in inputModule.value[0..9] in millivolts. */
GO_module_input_receive_values(&inputModule);
if (++cycle >= PRINT_INTERVAL_CYCLES) {
cycle = 0;
/* Print a fixed-width header and value row so columns line up.
* Each column is 10 characters wide: " CHxx " / " 12345mV ". */
info(" CH01 CH02 CH03 CH04 CH05 CH06 CH07 CH08 CH09 CH10\n");
for (uint8_t ch = INPUTCHANNEL1; ch <= INPUTCHANNEL10; ch++) {
info("%6ldmV ", (long)inputModule.value[ch]);
}
info("\n");
}
usleep(10000); /* 10 ms */
}
return 0;
}