19 #include "TextCommand.h" 20 #include "DCCpp_Uno.h" 29 void Output::begin(
int id,
int pin,
int iFlag) {
30 #if defined(USE_EEPROM) || defined(USE_TEXTCOMMAND) 31 #if defined(USE_EEPROM) && defined(DCCPP_DEBUG_MODE) 32 if (strncmp(
EEStore::data.
id, EESTORE_ID,
sizeof(EESTORE_ID)) != 0) {
33 DCCPP_INTERFACE.println(F(
"Output::begin() must be called BEFORE DCCpp.begin() !"));
36 if (firstOutput == NULL) {
39 else if ((get(
id)) == NULL) {
40 Output *tt = firstOutput;
41 while (tt->nextOutput != NULL)
43 tt->nextOutput =
this;
47 this->set(
id, pin, iFlag);
49 #ifdef USE_TEXTCOMMAND 50 DCCPP_INTERFACE.print(
"<O>");
51 #if !defined(USE_ETHERNET) 52 DCCPP_INTERFACE.println(
"");
59 void Output::set(
int id,
int pin,
int iFlag) {
62 this->data.iFlag = iFlag;
63 this->data.oStatus = 0;
66 this->data.oStatus = bitRead(this->data.iFlag, 1) ? bitRead(this->data.iFlag, 2) : 0;
68 dontCheckNextPinAccess =
true;
70 digitalWrite(this->data.pin, this->data.oStatus ^ bitRead(this->data.iFlag, 0));
72 dontCheckNextPinAccess =
true;
74 pinMode(this->data.pin, OUTPUT);
79 void Output::activate(
int s){
81 digitalWrite(data.pin,data.oStatus ^ bitRead(data.iFlag,0));
85 EEPROM.put(num, (
void *)&data.oStatus, 1);
87 EEPROM.put(num, data.oStatus);
90 #ifdef USE_TEXTCOMMAND 91 DCCPP_INTERFACE.print(
"<Y");
92 DCCPP_INTERFACE.print(data.id);
94 DCCPP_INTERFACE.print(
" 0>");
96 DCCPP_INTERFACE.print(
" 1>");
97 #if !defined(USE_ETHERNET) 98 DCCPP_INTERFACE.println(
"");
103 #if defined(USE_EEPROM) || defined(USE_TEXTCOMMAND) 106 Output* Output::get(
int n){
108 for(tt=firstOutput;tt!=NULL && tt->data.id!=n;tt=tt->nextOutput);
113 void Output::remove(
int n){
116 for(tt=firstOutput, pp = NULL;tt!=NULL && tt->data.id!=n;pp=tt,tt=tt->nextOutput);
119 #ifdef USE_TEXTCOMMAND 120 DCCPP_INTERFACE.print(
"<X>");
121 #if !defined(USE_ETHERNET) 122 DCCPP_INTERFACE.println(
"");
129 firstOutput=tt->nextOutput;
131 pp->nextOutput=tt->nextOutput;
135 #ifdef USE_TEXTCOMMAND 136 DCCPP_INTERFACE.print(
"<O>");
137 #if !defined(USE_ETHERNET) 138 DCCPP_INTERFACE.println(
"");
145 int Output::count() {
148 for (tt = firstOutput; tt != NULL; tt = tt->nextOutput)
156 void Output::load() {
157 struct OutputData data;
166 #if defined(USE_TEXTCOMMAND) 167 tt = create(data.id, data.pin, data.iFlag);
170 #ifdef DCCPP_DEBUG_MODE 172 DCCPP_INTERFACE.println(F(
"Output::begin() must be called BEFORE Output::load() !"));
175 tt->set(data.id, data.pin, data.iFlag);
178 tt->data.oStatus = bitRead(tt->data.iFlag, 1) ? bitRead(tt->data.iFlag, 2) : data.oStatus;
180 dontCheckNextPinAccess =
true;
182 digitalWrite(tt->data.pin, tt->data.oStatus ^ bitRead(tt->data.iFlag, 0));
184 dontCheckNextPinAccess =
true;
186 pinMode(tt->data.pin, OUTPUT);
194 void Output::store() {
216 #if defined(USE_TEXTCOMMAND) 219 bool Output::parse(
char *c){
223 switch(sscanf(c,
"%d %d %d",&n,&s,&m)){
231 DCCPP_INTERFACE.print(
"<X>");
232 #if !defined(USE_ETHERNET) 233 DCCPP_INTERFACE.println(
"");
246 #ifdef DCCPP_PRINT_DCCPP 257 Output *Output::create(
int id,
int pin,
int iFlag){
258 Output *tt =
new Output();
261 DCCPP_INTERFACE.print(
"<X>");
262 #if !defined(USE_ETHERNET) 263 DCCPP_INTERFACE.println(
"");
268 tt->begin(
id, pin, iFlag);
275 #if defined(USE_EEPROM) || defined(USE_TEXTCOMMAND) 276 #ifdef DCCPP_PRINT_DCCPP 280 void Output::show() {
283 if (firstOutput == NULL) {
284 DCCPP_INTERFACE.print(
"<X>");
285 #if !defined(USE_ETHERNET) 286 DCCPP_INTERFACE.println(
"");
291 for (tt = firstOutput; tt != NULL; tt = tt->nextOutput) {
292 DCCPP_INTERFACE.print(
"<Y");
293 DCCPP_INTERFACE.print(tt->data.id);
294 DCCPP_INTERFACE.print(
" ");
295 DCCPP_INTERFACE.print(tt->data.pin);
296 DCCPP_INTERFACE.print(
" ");
297 DCCPP_INTERFACE.print(tt->data.iFlag);
299 if (tt->data.oStatus == 0)
300 DCCPP_INTERFACE.print(
" 0>");
302 DCCPP_INTERFACE.print(
" 1>");
303 #if !defined(USE_ETHERNET) 304 DCCPP_INTERFACE.println(
"");
312 Output *Output::firstOutput=NULL;
static void advance(int inIncrement)