#include <msp/core/maputils.h>
+#include <msp/datafile/writer.h>
+#include <msp/fs/redirectedpath.h>
+#include <msp/fs/stat.h>
#include <msp/io/print.h>
#include <msp/time/utils.h>
#include "arducontrol.h"
ArduControl::ArduControl(const string &dev):
serial(dev),
debug(1),
+ state_file("arducontrol.state"),
power(false),
active_accessory(0),
s88(*this),
mfx_search(*this),
thread(*this)
{
+ if(FS::exists(state_file))
+ DataFile::load(*this, state_file.str());
+
+ unsigned max_address = 0;
+ for(MfxInfoArray::const_iterator i=mfx_info.begin(); i!=mfx_info.end(); ++i)
+ max_address = max(max_address, i->address);
+ mfx_search.set_next_address(max_address+1);
+
PendingCommand cmd;
cmd.command[0] = READ_POWER_STATE;
cmd.length = 1;
return loco.id;
}
+ArduControl::MfxInfoArray::iterator ArduControl::add_mfx_info(const MfxInfo &info)
+{
+ MfxInfoArray::iterator i;
+ for(i=mfx_info.begin(); (i!=mfx_info.end() && i->id!=info.id); ++i) ;
+ if(i==mfx_info.end())
+ {
+ mfx_info.push_back(info);
+ i = --mfx_info.end();
+ }
+ else
+ *i = info;
+ return i;
+}
+
void ArduControl::remove_loco(unsigned id)
{
Locomotive &loco = get_item(locomotives, id);
MfxInfo info;
if(mfx_search.pop_info(info))
{
- MfxInfoArray::iterator i;
- for(i=mfx_info.begin(); (i!=mfx_info.end() && i->id!=info.id); ++i) ;
- if(i==mfx_info.end())
- {
- mfx_info.push_back(info);
- i = --mfx_info.end();
- }
- else
- *i = info;
+ MfxInfoArray::iterator i = add_mfx_info(info);
+ save_state();
signal_locomotive_detected.emit(*i);
}
}
{
}
+void ArduControl::save_state() const
+{
+ FS::RedirectedPath tmp_file(state_file);
+ IO::BufferedFile out(tmp_file.str(), IO::M_WRITE);
+ DataFile::Writer writer(out);
+
+ writer.write((DataFile::Statement("mfx_announce_serial"), mfx_announce.get_serial()));
+ for(MfxInfoArray::const_iterator i=mfx_info.begin(); i!=mfx_info.end(); ++i)
+ {
+ DataFile::Statement st("mfx_locomotive");
+ st.append(i->id);
+ st.sub.push_back((DataFile::Statement("address"), i->address));
+ st.sub.push_back((DataFile::Statement("name"), i->name));
+ writer.write(st);
+ }
+}
+
ArduControl::Tag::Tag():
type(NONE),
}
}
+void ArduControl::MfxSearchTask::set_next_address(unsigned a)
+{
+ next_address = a;
+}
+
bool ArduControl::MfxSearchTask::pop_info(MfxInfo &info)
{
return queue.pop(info);
return 0;
}
+
+ArduControl::Loader::Loader(ArduControl &c):
+ DataFile::ObjectLoader<ArduControl>(c)
+{
+ add("mfx_announce_serial", &Loader::mfx_announce_serial);
+ add("mfx_locomotive", &Loader::mfx_locomotive);
+}
+
+void ArduControl::Loader::mfx_announce_serial(unsigned s)
+{
+ obj.mfx_announce.set_serial(s);
+}
+
+void ArduControl::Loader::mfx_locomotive(unsigned id)
+{
+ MfxInfo info;
+ info.id = id;
+ info.protocol = "MFX";
+ load_sub(info);
+ obj.add_mfx_info(info);
+}
+
+
+ArduControl::MfxInfo::Loader::Loader(MfxInfo &i):
+ DataFile::ObjectLoader<MfxInfo>(i)
+{
+ add("address", static_cast<unsigned MfxInfo::*>(&MfxInfo::address));
+ add("name", static_cast<string MfxInfo::*>(&MfxInfo::name));
+}
+
} // namespace R2C2
#include <msp/core/mutex.h>
#include <msp/core/thread.h>
+#include <msp/datafile/objectloader.h>
+#include <msp/fs/path.h>
#include <msp/io/serial.h>
#include <msp/time/timedelta.h>
#include <msp/time/timestamp.h>
class ArduControl: public Driver
{
+public:
+ class Loader: public Msp::DataFile::ObjectLoader<ArduControl>
+ {
+ public:
+ Loader(ArduControl &);
+
+ private:
+ void mfx_announce_serial(unsigned);
+ void mfx_locomotive(unsigned);
+ };
+
private:
enum Command
{
struct MfxInfo: public DetectedLocomotive
{
+ class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
+ {
+ public:
+ Loader(MfxInfo &);
+ };
+
unsigned id;
};
virtual void process_reply(const char *, unsigned) { }
};
+ class CommandQueueTask: public Task
+ {
+ private:
+ Queue<PendingCommand> queue;
+
+ public:
+ virtual bool get_work(PendingCommand &);
+ };
+
class RefreshTask: public Task
{
private:
virtual bool get_work(PendingCommand &);
void set_serial(unsigned);
+ unsigned get_serial() const { return serial; }
};
class MfxSearchTask: public Task
virtual bool get_work(PendingCommand &);
virtual void process_reply(const char *, unsigned);
+ void set_next_address(unsigned);
bool pop_info(MfxInfo &);
};
Msp::IO::Serial serial;
unsigned debug;
+ Msp::FS::Path state_file;
ControlledVariable<bool> power;
virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
+private:
+ MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
+public:
virtual void remove_loco(unsigned);
virtual void set_loco_speed(unsigned, unsigned);
virtual void set_loco_reverse(unsigned, bool);
virtual void tick();
virtual void flush();
+private:
+ void save_state() const;
};
} // namespace R2C2