12 #include "TextCommand.h" 13 #include "DCCpp_Uno.h" 22 void Output::begin(
int id,
int pin,
int iFlag) {
23 #if defined(USE_EEPROM) || defined(USE_TEXTCOMMAND) 24 #ifdef DCCPP_DEBUG_MODE 25 if (EEStore::eeStore != NULL)
27 INTERFACE.println(F(
"Output::begin() must be called BEFORE DCCpp.begin() !"));
30 if (firstOutput == NULL) {
33 else if ((
get(
id)) == NULL) {
34 Output *tt = firstOutput;
35 while (tt->nextOutput != NULL)
37 tt->nextOutput =
this;
41 this->
set(id, pin, iFlag);
43 #ifdef DCCPP_DEBUG_MODE 44 INTERFACE.println(
"<O>");
50 void Output::set(
int id,
int pin,
int iFlag) {
53 this->data.iFlag = iFlag;
54 this->data.oStatus = 0;
57 this->data.oStatus = bitRead(this->data.iFlag, 1) ? bitRead(this->data.iFlag, 2) : 0;
59 ArduiEmulator::Arduino::dontCheckNextPinAccess =
true;
61 digitalWrite(this->data.pin, this->data.oStatus ^ bitRead(this->data.iFlag, 0));
63 ArduiEmulator::Arduino::dontCheckNextPinAccess =
true;
65 pinMode(this->data.pin, OUTPUT);
70 void Output::activate(
int s){
72 digitalWrite(data.pin,data.oStatus ^ bitRead(data.iFlag,0));
76 EEPROM.put(num, (
void *)&data.oStatus, 1);
78 EEPROM.put(num, data.oStatus);
81 #ifdef DCCPP_DEBUG_MODE 82 INTERFACE.print(
"<Y");
83 INTERFACE.print(data.id);
85 INTERFACE.println(
" 0>");
87 INTERFACE.println(
" 1>");
91 #if defined(USE_EEPROM) || defined(USE_TEXTCOMMAND) 94 Output* Output::get(
int n){
96 for(tt=firstOutput;tt!=NULL && tt->data.id!=n;tt=tt->nextOutput);
101 void Output::remove(
int n){
104 for(tt=firstOutput;tt!=NULL && tt->data.id!=n;pp=tt,tt=tt->nextOutput);
107 #ifdef DCCPP_DEBUG_MODE 108 INTERFACE.println(
"<X>");
114 firstOutput=tt->nextOutput;
116 pp->nextOutput=tt->nextOutput;
120 #ifdef DCCPP_DEBUG_MODE 121 INTERFACE.println(
"<O>");
127 int Output::count() {
130 for (tt = firstOutput; tt != NULL; tt = tt->nextOutput)
138 void Output::load() {
139 struct OutputData data;
142 for (
int i = 0; i<EEStore::eeStore->data.nOutputs; i++) {
144 EEPROM.get(EEStore::pointer(), (
void *)&data,
sizeof(OutputData));
146 EEPROM.get(EEStore::pointer(), data);
149 tt->set(data.id, data.pin, data.iFlag);
150 tt->data.oStatus = bitRead(tt->data.iFlag, 1) ? bitRead(tt->data.iFlag, 2) : data.oStatus;
152 ArduiEmulator::Arduino::dontCheckNextPinAccess =
true;
154 digitalWrite(tt->data.pin, tt->data.oStatus ^ bitRead(tt->data.iFlag, 0));
156 ArduiEmulator::Arduino::dontCheckNextPinAccess =
true;
158 pinMode(tt->data.pin, OUTPUT);
159 tt->num = EEStore::pointer();
160 EEStore::advance(
sizeof(tt->data));
166 void Output::store() {
170 EEStore::eeStore->data.nOutputs = 0;
173 tt->num = EEStore::pointer();
175 EEPROM.put(EEStore::pointer(), (
void *)&(tt->data),
sizeof(OutputData));
177 EEPROM.put(EEStore::pointer(), tt->data);
179 EEStore::advance(
sizeof(tt->data));
181 EEStore::eeStore->data.nOutputs++;
188 #if defined(USE_TEXTCOMMAND) 191 void Output::parse(
char *c){
195 switch(sscanf(c,
"%d %d %d",&n,&s,&m)){
201 #ifdef DCCPP_PRINT_DCCPP 203 INTERFACE.println(
"<X>");
215 #ifdef DCCPP_PRINT_DCCPP 225 Output *Output::create(
int id,
int pin,
int iFlag){
226 Output *tt =
new Output();
229 #ifdef DCCPP_PRINT_DCCPP 230 INTERFACE.println(
"<X>");
235 tt->begin(
id, pin, iFlag);
240 #endif USE_TEXTCOMMAND 242 #if defined(USE_EEPROM) || defined(USE_TEXTCOMMAND) 243 #ifdef DCCPP_PRINT_DCCPP 247 void Output::show() {
250 if (firstOutput == NULL) {
251 INTERFACE.print(
"<X>");
255 for (tt = firstOutput; tt != NULL; tt = tt->nextOutput) {
256 INTERFACE.print(
"<Y");
257 INTERFACE.print(tt->data.id);
258 INTERFACE.print(
" ");
259 INTERFACE.print(tt->data.pin);
260 INTERFACE.print(
" ");
261 INTERFACE.print(tt->data.iFlag);
263 if (tt->data.oStatus == 0)
264 INTERFACE.println(
" 0>");
266 INTERFACE.println(
" 1>");
273 Output *Output::firstOutput=NULL;