| @@ -0,0 +1,310 @@ | ||
| /* | ||
| include/comedilib.h | ||
| header file for the comedi library routines | ||
| COMEDI - Linux Control and Measurement Device Interface | ||
| Copyright (C) 1998-2002 David A. Schleef <ds@schleef.org> | ||
| This program is free software; you can redistribute it and/or modify | ||
| it under the terms of the GNU Lesser 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. | ||
| You should have received a copy of the GNU General Public License | ||
| along with this program; if not, write to the Free Software | ||
| Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| */ | ||
|
|
||
|
|
||
| #ifndef _COMEDILIB_H | ||
| #define _COMEDILIB_H | ||
|
|
||
| #include <comedi.h> | ||
| #include <comedilib_version.h> | ||
| #include <sys/ioctl.h> | ||
| #include <fcntl.h> | ||
| #include <stdio.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /* Macros for swig to %include this file. */ | ||
| #ifdef SWIG | ||
| #define SWIG_OUTPUT(x) OUTPUT | ||
| #define SWIG_INPUT(x) INPUT | ||
| #define SWIG_INOUT(x) INOUT | ||
| #else | ||
| #define SWIG_OUTPUT(x) x | ||
| #define SWIG_INPUT(x) x | ||
| #define SWIG_INOUT(x) x | ||
| #endif | ||
|
|
||
| typedef struct comedi_t_struct comedi_t; | ||
|
|
||
| typedef struct{ | ||
| double min; | ||
| double max; | ||
| unsigned int unit; | ||
| }comedi_range; | ||
|
|
||
| typedef struct comedi_sv_struct{ | ||
| comedi_t *dev; | ||
| unsigned int subdevice; | ||
| unsigned int chan; | ||
|
|
||
| /* range policy */ | ||
| int range; | ||
| int aref; | ||
|
|
||
| /* number of measurements to average (for ai) */ | ||
| int n; | ||
|
|
||
| lsampl_t maxdata; | ||
| }comedi_sv_t; | ||
|
|
||
| enum comedi_oor_behavior { | ||
| COMEDI_OOR_NUMBER = 0, | ||
| COMEDI_OOR_NAN | ||
| }; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| comedi_t *comedi_open(const char *fn); | ||
| int comedi_close(comedi_t *it); | ||
|
|
||
| /* logging */ | ||
| int comedi_loglevel(int loglevel); | ||
| void comedi_perror(const char *s); | ||
| const char *comedi_strerror(int errnum); | ||
| int comedi_errno(void); | ||
| int comedi_fileno(comedi_t *it); | ||
|
|
||
| /* global behavior */ | ||
| enum comedi_oor_behavior comedi_set_global_oor_behavior(enum comedi_oor_behavior behavior); | ||
|
|
||
| /* device queries */ | ||
| int comedi_get_n_subdevices(comedi_t *it); | ||
| #define COMEDI_VERSION_CODE(a,b,c) (((a)<<16) | ((b)<<8) | (c)) | ||
| int comedi_get_version_code(comedi_t *it); | ||
| const char *comedi_get_driver_name(comedi_t *it); | ||
| const char *comedi_get_board_name(comedi_t *it); | ||
| int comedi_get_read_subdevice(comedi_t *dev); | ||
| int comedi_get_write_subdevice(comedi_t *dev); | ||
|
|
||
| /* subdevice queries */ | ||
| int comedi_get_subdevice_type(comedi_t *it,unsigned int subdevice); | ||
| int comedi_find_subdevice_by_type(comedi_t *it,int type,unsigned int subd); | ||
| int comedi_get_subdevice_flags(comedi_t *it,unsigned int subdevice); | ||
| int comedi_get_n_channels(comedi_t *it,unsigned int subdevice); | ||
| int comedi_range_is_chan_specific(comedi_t *it,unsigned int subdevice); | ||
| int comedi_maxdata_is_chan_specific(comedi_t *it,unsigned int subdevice); | ||
|
|
||
| /* channel queries */ | ||
| lsampl_t comedi_get_maxdata(comedi_t *it,unsigned int subdevice, | ||
| unsigned int chan); | ||
| int comedi_get_n_ranges(comedi_t *it,unsigned int subdevice, | ||
| unsigned int chan); | ||
| comedi_range * comedi_get_range(comedi_t *it,unsigned int subdevice, | ||
| unsigned int chan,unsigned int range); | ||
| int comedi_find_range(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int unit,double min,double max); | ||
|
|
||
| /* buffer queries */ | ||
| int comedi_get_buffer_size(comedi_t *it,unsigned int subdevice); | ||
| int comedi_get_max_buffer_size(comedi_t *it,unsigned int subdevice); | ||
| int comedi_set_buffer_size(comedi_t *it,unsigned int subdevice, | ||
| unsigned int len); | ||
|
|
||
| /* low-level stuff */ | ||
| #ifdef _COMEDILIB_DEPRECATED | ||
| int comedi_trigger(comedi_t *it,comedi_trig *trig); /* deprecated */ | ||
| #endif | ||
| int comedi_do_insnlist(comedi_t *it,comedi_insnlist *il); | ||
| int comedi_do_insn(comedi_t *it,comedi_insn *insn); | ||
| int comedi_lock(comedi_t *it,unsigned int subdevice); | ||
| int comedi_unlock(comedi_t *it,unsigned int subdevice); | ||
|
|
||
| /* physical units */ | ||
| double comedi_to_phys(lsampl_t data,comedi_range *rng,lsampl_t maxdata); | ||
| lsampl_t comedi_from_phys(double data,comedi_range *rng,lsampl_t maxdata); | ||
| int comedi_sampl_to_phys(double *dest, int dst_stride, sampl_t *src, | ||
| int src_stride, comedi_range *rng, lsampl_t maxdata, int n); | ||
| int comedi_sampl_from_phys(sampl_t *dest,int dst_stride,double *src, | ||
| int src_stride, comedi_range *rng, lsampl_t maxdata, int n); | ||
|
|
||
| /* syncronous stuff */ | ||
| int comedi_data_read(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int range,unsigned int aref,lsampl_t *SWIG_OUTPUT(data)); | ||
| int comedi_data_read_n(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int range,unsigned int aref,lsampl_t *SWIG_OUTPUT(data), unsigned int n); | ||
| int comedi_data_read_hint(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int range,unsigned int aref); | ||
| int comedi_data_read_delayed(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int range,unsigned int aref,lsampl_t *SWIG_OUTPUT(data), unsigned int nano_sec); | ||
| int comedi_data_write(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int range,unsigned int aref,lsampl_t data); | ||
| int comedi_dio_config(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int dir); | ||
| int comedi_dio_get_config(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int *SWIG_OUTPUT(dir)); | ||
| int comedi_dio_read(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int *SWIG_OUTPUT(bit)); | ||
| int comedi_dio_write(comedi_t *it,unsigned int subd,unsigned int chan, | ||
| unsigned int bit); | ||
| int comedi_dio_bitfield2(comedi_t *it,unsigned int subd, | ||
| unsigned int write_mask, unsigned int *SWIG_INOUT(bits), unsigned int base_channel); | ||
| /* Should be moved to _COMEDILIB_DEPRECATED once bindings for other languages are updated | ||
| * to use comedi_dio_bitfield2() instead.*/ | ||
| int comedi_dio_bitfield(comedi_t *it,unsigned int subd, | ||
| unsigned int write_mask, unsigned int *SWIG_INOUT(bits)); | ||
|
|
||
| /* slowly varying stuff */ | ||
| int comedi_sv_init(comedi_sv_t *it,comedi_t *dev,unsigned int subd,unsigned int chan); | ||
| int comedi_sv_update(comedi_sv_t *it); | ||
| int comedi_sv_measure(comedi_sv_t *it,double *SWIG_OUTPUT(data)); | ||
|
|
||
| /* streaming I/O (commands) */ | ||
|
|
||
| int comedi_get_cmd_src_mask(comedi_t *dev,unsigned int subdevice, | ||
| comedi_cmd *SWIG_INOUT(cmd)); | ||
| int comedi_get_cmd_generic_timed(comedi_t *dev,unsigned int subdevice, | ||
| comedi_cmd *SWIG_INOUT(cmd), unsigned chanlist_len, unsigned scan_period_ns); | ||
| int comedi_cancel(comedi_t *it,unsigned int subdevice); | ||
| int comedi_command(comedi_t *it,comedi_cmd *cmd); | ||
| int comedi_command_test(comedi_t *it,comedi_cmd *SWIG_INOUT(cmd)); | ||
| int comedi_poll(comedi_t *dev,unsigned int subdevice); | ||
|
|
||
| /* buffer control */ | ||
|
|
||
| int comedi_set_max_buffer_size(comedi_t *it, unsigned int subdev, | ||
| unsigned int max_size); | ||
| int comedi_get_buffer_contents(comedi_t *it, unsigned int subdev); | ||
| int comedi_mark_buffer_read(comedi_t *it, unsigned int subdev, | ||
| unsigned int bytes); | ||
| int comedi_mark_buffer_written(comedi_t *it, unsigned int subdev, | ||
| unsigned int bytes); | ||
| int comedi_get_buffer_offset(comedi_t *it, unsigned int subdev); | ||
|
|
||
| #ifdef _COMEDILIB_DEPRECATED | ||
| /* | ||
| * The following functions are deprecated and should not be used. | ||
| */ | ||
| int comedi_get_timer(comedi_t *it,unsigned int subdev,double freq, | ||
| unsigned int *trigvar,double *actual_freq); | ||
| int comedi_timed_1chan(comedi_t *it,unsigned int subdev,unsigned int chan, | ||
| unsigned int range, unsigned int aref,double freq, | ||
| unsigned int n_samples,double *data); | ||
| int comedi_get_rangetype(comedi_t *it,unsigned int subdevice, | ||
| unsigned int chan); | ||
| #endif | ||
|
|
||
|
|
||
| #ifndef _COMEDILIB_STRICT_ABI | ||
| /* | ||
| The following prototypes are _NOT_ part of the Comedilib ABI, and | ||
| may change in future versions without regard to source or binary | ||
| compatibility. In practice, this is a holding place for the next | ||
| library ABI version change. | ||
| */ | ||
| /* structs and functions used for parsing calibration files */ | ||
| typedef struct | ||
| { | ||
| unsigned int subdevice; | ||
| unsigned int channel; | ||
| unsigned int value; | ||
| } comedi_caldac_t; | ||
| #define COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS 4 | ||
| typedef struct | ||
| { | ||
| double coefficients[COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS]; | ||
| double expansion_origin; | ||
| unsigned order; | ||
| } comedi_polynomial_t; | ||
| typedef struct | ||
| { | ||
| comedi_polynomial_t *to_phys; | ||
| comedi_polynomial_t *from_phys; | ||
| } comedi_softcal_t; | ||
| #define CS_MAX_AREFS_LENGTH 4 | ||
| typedef struct | ||
| { | ||
| unsigned int subdevice; | ||
| unsigned int *channels; | ||
| unsigned int num_channels; | ||
| unsigned int *ranges; | ||
| unsigned int num_ranges; | ||
| unsigned int arefs[ CS_MAX_AREFS_LENGTH ]; | ||
| unsigned int num_arefs; | ||
| comedi_caldac_t *caldacs; | ||
| unsigned int num_caldacs; | ||
| comedi_softcal_t soft_calibration; | ||
| } comedi_calibration_setting_t; | ||
|
|
||
| typedef struct | ||
| { | ||
| char *driver_name; | ||
| char *board_name; | ||
| comedi_calibration_setting_t *settings; | ||
| unsigned int num_settings; | ||
| } comedi_calibration_t; | ||
|
|
||
| comedi_calibration_t* comedi_parse_calibration_file( const char *cal_file_path ); | ||
| int comedi_apply_parsed_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel, | ||
| unsigned int range, unsigned int aref, const comedi_calibration_t *calibration ); | ||
| char* comedi_get_default_calibration_path( comedi_t *dev ); | ||
| void comedi_cleanup_calibration( comedi_calibration_t *calibration ); | ||
| int comedi_apply_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel, | ||
| unsigned int range, unsigned int aref, const char *cal_file_path); | ||
|
|
||
| /* New stuff to provide conversion between integers and physical values that | ||
| * can support software calibrations. */ | ||
| enum comedi_conversion_direction | ||
| { | ||
| COMEDI_TO_PHYSICAL, | ||
| COMEDI_FROM_PHYSICAL | ||
| }; | ||
| int comedi_get_softcal_converter( | ||
| unsigned subdevice, unsigned channel, unsigned range, | ||
| enum comedi_conversion_direction direction, | ||
| const comedi_calibration_t *calibration, comedi_polynomial_t* SWIG_OUTPUT(polynomial)); | ||
| int comedi_get_hardcal_converter( | ||
| comedi_t *dev, unsigned subdevice, unsigned channel, unsigned range, | ||
| enum comedi_conversion_direction direction, comedi_polynomial_t* SWIG_OUTPUT(polynomial)); | ||
| double comedi_to_physical(lsampl_t data, | ||
| const comedi_polynomial_t *conversion_polynomial); | ||
| lsampl_t comedi_from_physical(double data, | ||
| const comedi_polynomial_t *conversion_polynomial); | ||
|
|
||
| int comedi_internal_trigger(comedi_t *dev, unsigned subd, unsigned trignum); | ||
| /* INSN_CONFIG wrappers */ | ||
| int comedi_arm(comedi_t *device, unsigned subdevice, unsigned source); | ||
| int comedi_reset(comedi_t *device, unsigned subdevice); | ||
| int comedi_get_clock_source(comedi_t *device, unsigned subdevice, unsigned channel, unsigned *SWIG_OUTPUT(clock), unsigned *SWIG_OUTPUT(period_ns)); | ||
| int comedi_get_gate_source(comedi_t *device, unsigned subdevice, unsigned channel, | ||
| unsigned gate, unsigned *SWIG_OUTPUT(source)); | ||
| int comedi_get_routing(comedi_t *device, unsigned subdevice, unsigned channel, unsigned *SWIG_OUTPUT(routing)); | ||
| int comedi_set_counter_mode(comedi_t *device, unsigned subdevice, unsigned channel, unsigned mode_bits); | ||
| int comedi_set_clock_source(comedi_t *device, unsigned subdevice, unsigned channel, unsigned clock, unsigned period_ns); | ||
| int comedi_set_filter(comedi_t *device, unsigned subdevice, unsigned channel, unsigned filter); | ||
| int comedi_set_gate_source(comedi_t *device, unsigned subdevice, unsigned channel, unsigned gate_index, unsigned gate_source); | ||
| int comedi_set_other_source(comedi_t *device, unsigned subdevice, unsigned channel, | ||
| unsigned other, unsigned source); | ||
| int comedi_set_routing(comedi_t *device, unsigned subdevice, unsigned channel, unsigned routing); | ||
| int comedi_get_hardware_buffer_size(comedi_t *device, unsigned subdevice, enum comedi_io_direction direction); | ||
|
|
||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif | ||
|
|
| @@ -0,0 +1,150 @@ | ||
|
|
||
|
|
||
| #include "elev.h" | ||
|
|
||
| #include "channels.h" | ||
| #include "io.h" | ||
|
|
||
| #include <assert.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #define MOTOR_SPEED 2800 | ||
|
|
||
|
|
||
| static const int lamp_channel_matrix[N_FLOORS][N_BUTTONS] = { | ||
| {LIGHT_UP1, LIGHT_DOWN1, LIGHT_COMMAND1}, | ||
| {LIGHT_UP2, LIGHT_DOWN2, LIGHT_COMMAND2}, | ||
| {LIGHT_UP3, LIGHT_DOWN3, LIGHT_COMMAND3}, | ||
| {LIGHT_UP4, LIGHT_DOWN4, LIGHT_COMMAND4}, | ||
| }; | ||
|
|
||
|
|
||
| static const int button_channel_matrix[N_FLOORS][N_BUTTONS] = { | ||
| {BUTTON_UP1, BUTTON_DOWN1, BUTTON_COMMAND1}, | ||
| {BUTTON_UP2, BUTTON_DOWN2, BUTTON_COMMAND2}, | ||
| {BUTTON_UP3, BUTTON_DOWN3, BUTTON_COMMAND3}, | ||
| {BUTTON_UP4, BUTTON_DOWN4, BUTTON_COMMAND4}, | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| void elev_init(void) { | ||
| int init_success = io_init(); | ||
| assert(init_success && "Unable to initialize elevator hardware!"); | ||
|
|
||
| for (int f = 0; f < N_FLOORS; f++) { | ||
| for (elev_button_type_t b = 0; b < N_BUTTONS; b++){ | ||
| elev_set_button_lamp(b, f, 0); | ||
| } | ||
| } | ||
|
|
||
| elev_set_stop_lamp(0); | ||
| elev_set_door_open_lamp(0); | ||
| elev_set_floor_indicator(0); | ||
| } | ||
|
|
||
|
|
||
| void elev_set_motor_direction(elev_motor_direction_t dirn) { | ||
| if (dirn == 0){ | ||
| io_write_analog(MOTOR, 0); | ||
| } else if (dirn > 0) { | ||
| io_clear_bit(MOTORDIR); | ||
| io_write_analog(MOTOR, MOTOR_SPEED); | ||
| } else if (dirn < 0) { | ||
| io_set_bit(MOTORDIR); | ||
| io_write_analog(MOTOR, MOTOR_SPEED); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_button_lamp(elev_button_type_t button, int floor, int value) { | ||
| assert(floor >= 0); | ||
| assert(floor < N_FLOORS); | ||
| assert(button >= 0); | ||
| assert(button < N_BUTTONS); | ||
|
|
||
| if (value) { | ||
| io_set_bit(lamp_channel_matrix[floor][button]); | ||
| } else { | ||
| io_clear_bit(lamp_channel_matrix[floor][button]); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_floor_indicator(int floor) { | ||
| assert(floor >= 0); | ||
| assert(floor < N_FLOORS); | ||
|
|
||
| // Binary encoding. One light must always be on. | ||
| if (floor & 0x02) { | ||
| io_set_bit(LIGHT_FLOOR_IND1); | ||
| } else { | ||
| io_clear_bit(LIGHT_FLOOR_IND1); | ||
| } | ||
|
|
||
| if (floor & 0x01) { | ||
| io_set_bit(LIGHT_FLOOR_IND2); | ||
| } else { | ||
| io_clear_bit(LIGHT_FLOOR_IND2); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_door_open_lamp(int value) { | ||
| if (value) { | ||
| io_set_bit(LIGHT_DOOR_OPEN); | ||
| } else { | ||
| io_clear_bit(LIGHT_DOOR_OPEN); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_stop_lamp(int value) { | ||
| if (value) { | ||
| io_set_bit(LIGHT_STOP); | ||
| } else { | ||
| io_clear_bit(LIGHT_STOP); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| int elev_get_button_signal(elev_button_type_t button, int floor) { | ||
| assert(floor >= 0); | ||
| assert(floor < N_FLOORS); | ||
| assert(button >= 0); | ||
| assert(button < N_BUTTONS); | ||
|
|
||
|
|
||
| return io_read_bit(button_channel_matrix[floor][button]); | ||
| } | ||
|
|
||
|
|
||
| int elev_get_floor_sensor_signal(void) { | ||
| if (io_read_bit(SENSOR_FLOOR1)) { | ||
| return 0; | ||
| } else if (io_read_bit(SENSOR_FLOOR2)) { | ||
| return 1; | ||
| } else if (io_read_bit(SENSOR_FLOOR3)) { | ||
| return 2; | ||
| } else if (io_read_bit(SENSOR_FLOOR4)) { | ||
| return 3; | ||
| } else { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| int elev_get_stop_signal(void) { | ||
| return io_read_bit(STOP); | ||
| } | ||
|
|
||
|
|
||
| int elev_get_obstruction_signal(void) { | ||
| return io_read_bit(OBSTRUCTION); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| @@ -0,0 +1,39 @@ | ||
| // Wrapper for libComedi Elevator control. | ||
| // These functions provide an interface to the elevators in the real time lab | ||
| #pragma once | ||
|
|
||
| // Number of floors. Hardware-dependent, do not modify. | ||
| #define N_FLOORS 4 | ||
|
|
||
| // Number of buttons (and corresponding lamps) on a per-floor basis | ||
| #define N_BUTTONS 3 | ||
|
|
||
| typedef enum tag_elev_motor_direction { | ||
| DIRN_DOWN = -1, | ||
| DIRN_STOP = 0, | ||
| DIRN_UP = 1 | ||
| } elev_motor_direction_t; | ||
|
|
||
| typedef enum tag_elev_lamp_type { | ||
| BUTTON_CALL_UP = 0, | ||
| BUTTON_CALL_DOWN = 1, | ||
| BUTTON_COMMAND = 2 | ||
| } elev_button_type_t; | ||
|
|
||
|
|
||
|
|
||
| void elev_init(void); | ||
|
|
||
| void elev_set_motor_direction(elev_motor_direction_t dirn); | ||
| void elev_set_button_lamp(elev_button_type_t button, int floor, int value); | ||
| void elev_set_floor_indicator(int floor); | ||
| void elev_set_door_open_lamp(int value); | ||
| void elev_set_stop_lamp(int value); | ||
|
|
||
| int elev_get_button_signal(elev_button_type_t button, int floor); | ||
| int elev_get_floor_sensor_signal(void); | ||
| int elev_get_stop_signal(void); | ||
| int elev_get_obstruction_signal(void); | ||
|
|
||
|
|
||
|
|
| @@ -0,0 +1,211 @@ | ||
| Simulator mkII | ||
| ============== | ||
|
|
||
| This simulator is an extension to `elev.c` that lets you use both the actual elevator at the real-time lab, and a simulated elevator. | ||
|
|
||
| The simulated elevator is a server which is intended to be run in its own window. Communication with the server is done over TCP. `elev.c` has been modified to act as a client, where the network portion is written with linux in mind (as this is what you use at the lab, and libcomedi also only works on linux). However, it is possible to write a client in another language, thus avoiding the OS-specific C code entirely. | ||
|
|
||
| The server is written in D, so you will need a D compiler to run it. I recommend using the dmd compiler (since this is the only one I have tested it with), which you can get from [The D lang website](http://dlang.org/download.html#dmd). | ||
|
|
||
| Main features in mkII: | ||
| - 2 to 9 floors: Test the elevator with a different number of floors | ||
| - Fully customizable controls: Dvorak and qwertz users rejoice! | ||
| - Full-time manual motor override: Control the motor directly, simulating motor stop or unexpected movement. | ||
| - Button hold: Hold down buttons by using uppercase letters. | ||
|
|
||
| Regressions in mkII: | ||
| - It is no longer possible to read the current value of the "outputs" (like the direction bit, lights, etc.) | ||
|
|
||
|
|
||
|
|
||
| Usage | ||
| ===== | ||
|
|
||
| API Changes for `elev.c` | ||
| ------------------------ | ||
|
|
||
| The only difference is: | ||
| - `elev_init(elev_type e);` | ||
| - Takes either `ET_Comedi` or `ET_Simulation` | ||
|
|
||
|
|
||
| Running the server | ||
| ------------------ | ||
|
|
||
| The server is intended to run in its own window, as it also takes keyboard input to simulate button presses. The server should not need to be restarted if the client is restarted. | ||
|
|
||
| Running: | ||
| - `rdmd sim_server.d`, if `simulator.con` is in the folder you are running `rdmd` from, or | ||
| - `rdmd sim_server.d [configfile]`, to specify a another config file. | ||
|
|
||
|
|
||
| Configuration options | ||
| --------------------- | ||
|
|
||
| See [simulator.con](simulator.con) for all the config options. | ||
|
|
||
| Default keyboard controls | ||
| ------------------------- | ||
|
|
||
| - Up: `qwertyui` | ||
| - Down: `sdfghjkl` | ||
| - Cab: `zxcvbnm,.` | ||
| - Stop: `p` | ||
| - Obstruction: `-` | ||
| - Motor manual override: Down: `7`, Stop: `8`, Up: `9` | ||
|
|
||
| Up, down, cab and stop button can be toggled (and thereby held down) by using uppercase letters. | ||
|
|
||
|
|
||
| Display | ||
| ------- | ||
|
|
||
| ``` | ||
| +-----------+-----------------+ | ||
| | | #> | | ||
| | Floor | 0 1* 2 3 | | ||
| +-----------+-----------------+-----------+ | ||
| | Hall Up | * - - | Door: - | | ||
| | Hall Down | - - * | Stop: - | | ||
| | Cab | - - * - | Obstr: ^ | | ||
| +-----------+-----------------+---------43+ | ||
| ``` | ||
|
|
||
| The ascii-art-style display is updated whenever the state of the simulated elevator is updated. | ||
|
|
||
| A print count (number of times a new state is printed) is shown in the lower right corner of the display. Try to avoid writing to the (simulated) hardware if nothing has happened. A jump of 20-50 in the printcount is fine (even expected), but if there are larger jumps or there is a continuous upward count, it may be time to re-evaluate some design choices. | ||
|
|
||
|
|
||
|
|
||
| Creating your own client | ||
| ======================== | ||
|
|
||
| You can use the simulator entirely without the C client code by creating your own client in the language of your choice. The binary protocol for interfacing with the server is outlined below: | ||
|
|
||
| Protocol | ||
| -------- | ||
|
|
||
| - All TCP messages must have a length of 4 bytes | ||
| - The instructions for reading from the hardware send replies that are 4 bytes long, where the last byte is always 0 | ||
| - The instructions for writing to the hardware do not send any replies | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td><strong>Writing</strong></td> | ||
| <td align="center" colspan="4">Instruction</td> | ||
| <td align="center" colspan="0" rowspan="7"></td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Reload config</em></td> | ||
| <td> 0 </td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Motor direction</em></td> | ||
| <td> 1 </td> | ||
| <td>direction<br>[-1 (<em>255</em>),0,1]</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Order button light</em></td> | ||
| <td> 2 </td> | ||
| <td>button<br>[0,1,2]</td> | ||
| <td>floor<br>[0..NF]</td> | ||
| <td>value<br>[0,1]</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Floor indicator</em></td> | ||
| <td> 3 </td> | ||
| <td>floor<br>[0..NF]</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Door open light</em></td> | ||
| <td> 4 </td> | ||
| <td>value<br>[0,1]</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Stop button light</em></td> | ||
| <td> 5 </td> | ||
| <td>value<br>[0,1]</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| </tr> | ||
| <tr> | ||
| <td><strong>Reading</strong></td> | ||
| <td align="center" colspan="4">Instruction</td> | ||
| <td></td> | ||
| <td align="center" colspan="4">Output</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Order button</em></td> | ||
| <td> 6 </td> | ||
| <td>button<br>[0,1,2]</td> | ||
| <td>floor<br>[0..NF]</td> | ||
| <td>X</td> | ||
| <td align="right"><em>Returns:</em></td> | ||
| <td>6</td> | ||
| <td>pressed<br>[0,1]</td> | ||
| <td>0</td> | ||
| <td>0</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Floor sensor</em></td> | ||
| <td> 7 </td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td align="right"><em>Returns:</em></td> | ||
| <td>7</td> | ||
| <td>at floor<br>[0,1]</td> | ||
| <td>floor<br>[0..NF]</td> | ||
| <td>0</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Stop button</em></td> | ||
| <td> 8 </td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td align="right"><em>Returns:</em></td> | ||
| <td>8</td> | ||
| <td>pressed<br>[0,1]</td> | ||
| <td>0</td> | ||
| <td>0</td> | ||
| </tr> | ||
| <tr> | ||
| <td><em>Obstruction switch</em></td> | ||
| <td> 9 </td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td>X</td> | ||
| <td align="right"><em>Returns:</em></td> | ||
| <td>9</td> | ||
| <td>active<br>[0,1]</td> | ||
| <td>0</td> | ||
| <td>0</td> | ||
| </tr> | ||
| <tr> | ||
| <td colspan="0"><em>NF = Num floors. X = Don't care.</em></td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
|
|
||
| Since interfacing is done over TCP, you can also use command-line utilities to interface with the simulator server. For example, to read the current floor: | ||
| ```bash | ||
| echo -e '\x07\x00\x00\x00' | netcat localhost 15657 | od -tx1 | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| @@ -0,0 +1,82 @@ | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| // Load values from a config file | ||
| // | ||
| // Key-value pairs in the config file are assumed to be of the form: | ||
| // "--key value" | ||
| // Lines not starting in "--" are ignored. | ||
| // Keys are *not* case-sensitive | ||
| // Enum values are *not* case-sensitive | ||
| // | ||
| // keyuments: | ||
| // file: Name of the file to load. | ||
| // cases: One or more instance of `con_val()` or `con_enum()` | ||
| // The cases must *not* be separated by commas. | ||
| // | ||
| // Example: | ||
| // /* Content of "config.con": | ||
| // ``` | ||
| // --integer 5 | ||
| // --greeting hello | ||
| // --enumeration En2 | ||
| // ``` | ||
| // */ | ||
| // | ||
| // typedef enum { En1, En2, En3 } En; | ||
| // int i; | ||
| // char s[16]; | ||
| // En en; | ||
| // | ||
| // con_load("config.con", | ||
| // con_val("integer", &i, "%d") | ||
| // con_val("greeting", s, "%[^\n]") | ||
| // con_enum("enumeration", &en, | ||
| // con_match(En1) | ||
| // con_match(En2) | ||
| // con_match(En3) | ||
| // ) | ||
| // ) | ||
| // printf("%s, %d, %d\n", s, i, en); // Should print "hello, 5, 1" | ||
| // | ||
| #define con_load(file, cases) \ | ||
| { \ | ||
| FILE* _f = fopen(file, "r"); \ | ||
| if(_f){ \ | ||
| char _line[128] = {0}; \ | ||
| while(fgets(_line, 128, _f)){ \ | ||
| if(!strncmp(_line, "--", 2)){ \ | ||
| char _key[64]; \ | ||
| char _val[64]; \ | ||
| sscanf(_line, "--%s %s", _key, _val); \ | ||
| cases \ | ||
| } \ | ||
| } \ | ||
| } else { \ | ||
| printf("Unable to open config file %s\n", file); \ | ||
| } \ | ||
| } | ||
|
|
||
|
|
||
| #define con_val(key, var, fmt) \ | ||
| if(!strcasecmp(_key, key)){ \ | ||
| sscanf(_val, fmt, var); \ | ||
| } | ||
|
|
||
|
|
||
| #define con_enum(key, var, match_cases) \ | ||
| if(!strcasecmp(_key, key)){ \ | ||
| typeof(*var) _v; \ | ||
| match_cases \ | ||
| *var = _v; \ | ||
| } | ||
|
|
||
| #define con_match(id) \ | ||
| if(!strcasecmp(_val, #id)){ \ | ||
| _v = id; \ | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
| @@ -0,0 +1,280 @@ | ||
| #include "elev.h" | ||
|
|
||
| #include <assert.h> | ||
| #include <stdlib.h> | ||
| #include <sys/socket.h> | ||
| #include <netdb.h> | ||
| #include <stdio.h> | ||
| #include <pthread.h> | ||
|
|
||
| #include "channels.h" | ||
| #include "io.h" | ||
| #include "con_load.h" | ||
|
|
||
| #define MOTOR_SPEED 2800 | ||
|
|
||
|
|
||
| static const int lamp_channel_matrix[N_FLOORS][N_BUTTONS] = { | ||
| {LIGHT_UP1, LIGHT_DOWN1, LIGHT_COMMAND1}, | ||
| {LIGHT_UP2, LIGHT_DOWN2, LIGHT_COMMAND2}, | ||
| {LIGHT_UP3, LIGHT_DOWN3, LIGHT_COMMAND3}, | ||
| {LIGHT_UP4, LIGHT_DOWN4, LIGHT_COMMAND4}, | ||
| }; | ||
|
|
||
|
|
||
| static const int button_channel_matrix[N_FLOORS][N_BUTTONS] = { | ||
| {BUTTON_UP1, BUTTON_DOWN1, BUTTON_COMMAND1}, | ||
| {BUTTON_UP2, BUTTON_DOWN2, BUTTON_COMMAND2}, | ||
| {BUTTON_UP3, BUTTON_DOWN3, BUTTON_COMMAND3}, | ||
| {BUTTON_UP4, BUTTON_DOWN4, BUTTON_COMMAND4}, | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| static elev_type elevatorType = ET_Comedi; | ||
| static int sockfd; | ||
| static pthread_mutex_t sockmtx; | ||
|
|
||
| void elev_init(elev_type e) { | ||
| elevatorType = e; | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| ; | ||
| int init_success = io_init(); | ||
| assert(init_success && "Unable to initialize elevator hardware!"); | ||
|
|
||
| for(int f = 0; f < N_FLOORS; f++) { | ||
| for(elev_button_type_t b = 0; b < N_BUTTONS; b++) { | ||
| elev_set_button_lamp(b, f, 0); | ||
| } | ||
| } | ||
|
|
||
| elev_set_stop_lamp(0); | ||
| elev_set_door_open_lamp(0); | ||
| elev_set_floor_indicator(0); | ||
| break; | ||
|
|
||
| case ET_Simulation: | ||
| ; | ||
| char ip[16] = {0}; | ||
| char port[8] = {0}; | ||
| con_load("simulator.con", | ||
| con_val("com_ip", ip, "%s") | ||
| con_val("com_port", port, "%s") | ||
| ) | ||
|
|
||
| pthread_mutex_init(&sockmtx, NULL); | ||
|
|
||
| sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
| assert(sockfd != -1 && "Unable to set up socket"); | ||
|
|
||
| struct addrinfo hints = { | ||
| .ai_family = AF_UNSPEC, | ||
| .ai_socktype = SOCK_STREAM, | ||
| .ai_protocol = IPPROTO_TCP, | ||
| }; | ||
| struct addrinfo* res; | ||
| getaddrinfo(ip, port, &hints, &res); | ||
|
|
||
| int fail = connect(sockfd, res->ai_addr, res->ai_addrlen); | ||
| assert(fail == 0 && "Unable to connect to simulator server"); | ||
|
|
||
| freeaddrinfo(res); | ||
|
|
||
| send(sockfd, (char[4]) {0}, 4, 0); | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| void elev_set_motor_direction(elev_motor_direction_t dirn) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| if(dirn == 0) { | ||
| io_write_analog(MOTOR, 0); | ||
| } else if(dirn > 0) { | ||
| io_clear_bit(MOTORDIR); | ||
| io_write_analog(MOTOR, MOTOR_SPEED); | ||
| } else if(dirn < 0) { | ||
| io_set_bit(MOTORDIR); | ||
| io_write_analog(MOTOR, MOTOR_SPEED); | ||
| } | ||
| break; | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {1, dirn}, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_button_lamp(elev_button_type_t button, int floor, int value) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| assert(floor >= 0); | ||
| assert(floor < N_FLOORS); | ||
| assert(button >= 0); | ||
| assert(button < N_BUTTONS); | ||
|
|
||
| if(value) { | ||
| io_set_bit(lamp_channel_matrix[floor][button]); | ||
| } else { | ||
| io_clear_bit(lamp_channel_matrix[floor][button]); | ||
| } | ||
| break; | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {2, button, floor, value}, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_floor_indicator(int floor) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| assert(floor >= 0); | ||
| assert(floor < N_FLOORS); | ||
|
|
||
| // Binary encoding. One light must always be on. | ||
| if(floor & 0x02) { | ||
| io_set_bit(LIGHT_FLOOR_IND1); | ||
| } else { | ||
| io_clear_bit(LIGHT_FLOOR_IND1); | ||
| } | ||
|
|
||
| if(floor & 0x01) { | ||
| io_set_bit(LIGHT_FLOOR_IND2); | ||
| } else { | ||
| io_clear_bit(LIGHT_FLOOR_IND2); | ||
| } | ||
| break; | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {3, floor}, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_door_open_lamp(int value) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| if(value) { | ||
| io_set_bit(LIGHT_DOOR_OPEN); | ||
| } else { | ||
| io_clear_bit(LIGHT_DOOR_OPEN); | ||
| } | ||
| break; | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {4, value}, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void elev_set_stop_lamp(int value) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| if(value) { | ||
| io_set_bit(LIGHT_STOP); | ||
| } else { | ||
| io_clear_bit(LIGHT_STOP); | ||
| } | ||
| break; | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {5, value}, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| int elev_get_button_signal(elev_button_type_t button, int floor) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| assert(floor >= 0); | ||
| assert(floor < N_FLOORS); | ||
| assert(button >= 0); | ||
| assert(button < N_BUTTONS); | ||
|
|
||
| return (io_read_bit(button_channel_matrix[floor][button])); | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {6, button, floor}, 4, 0); | ||
| char buf[4]; | ||
| recv(sockfd, buf, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| return buf[1]; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| int elev_get_floor_sensor_signal(void) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| if(io_read_bit(SENSOR_FLOOR1)) { | ||
| return 0; | ||
| } else if(io_read_bit(SENSOR_FLOOR2)) { | ||
| return 1; | ||
| } else if(io_read_bit(SENSOR_FLOOR3)) { | ||
| return 2; | ||
| } else if(io_read_bit(SENSOR_FLOOR4)) { | ||
| return 3; | ||
| } else { | ||
| return -1; | ||
| } | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {7}, 4, 0); | ||
| char buf[4]; | ||
| recv(sockfd, buf, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| return buf[1] ? buf[2] : -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| int elev_get_stop_signal(void) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| return io_read_bit(STOP); | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {8}, 4, 0); | ||
| char buf[4]; | ||
| recv(sockfd, buf, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| return buf[1]; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| int elev_get_obstruction_signal(void) { | ||
| switch(elevatorType) { | ||
| case ET_Comedi: | ||
| return io_read_bit(OBSTRUCTION); | ||
| case ET_Simulation: | ||
| pthread_mutex_lock(&sockmtx); | ||
| send(sockfd, (char[4]) {9}, 4, 0); | ||
| char buf[4]; | ||
| recv(sockfd, buf, 4, 0); | ||
| pthread_mutex_unlock(&sockmtx); | ||
| return buf[1]; | ||
| } | ||
| return 0; | ||
| } |
| @@ -0,0 +1,42 @@ | ||
| // Wrapper for libComedi Elevator control. | ||
| // These functions provide an interface to the elevators in the real time lab | ||
| #pragma once | ||
|
|
||
| // Number of floors. Hardware-dependent, do not modify. | ||
| #define N_FLOORS 4 | ||
|
|
||
| // Number of buttons (and corresponding lamps) on a per-floor basis | ||
| #define N_BUTTONS 3 | ||
|
|
||
| typedef enum tag_elev_motor_direction { | ||
| DIRN_DOWN = -1, | ||
| DIRN_STOP = 0, | ||
| DIRN_UP = 1 | ||
| } elev_motor_direction_t; | ||
|
|
||
| typedef enum tag_elev_lamp_type { | ||
| BUTTON_CALL_UP = 0, | ||
| BUTTON_CALL_DOWN = 1, | ||
| BUTTON_COMMAND = 2 | ||
| } elev_button_type_t; | ||
|
|
||
| typedef enum { | ||
| ET_Comedi, | ||
| ET_Simulation | ||
| } elev_type; | ||
|
|
||
| void elev_init(elev_type e); | ||
|
|
||
| void elev_set_motor_direction(elev_motor_direction_t dirn); | ||
| void elev_set_button_lamp(elev_button_type_t button, int floor, int value); | ||
| void elev_set_floor_indicator(int floor); | ||
| void elev_set_door_open_lamp(int value); | ||
| void elev_set_stop_lamp(int value); | ||
|
|
||
| int elev_get_button_signal(elev_button_type_t button, int floor); | ||
| int elev_get_floor_sensor_signal(void); | ||
| int elev_get_stop_signal(void); | ||
| int elev_get_obstruction_signal(void); | ||
|
|
||
|
|
||
|
|
| @@ -0,0 +1,22 @@ | ||
| simulator.con | ||
|
|
||
| --travelTimeBetweenFloors_ms 2000 | ||
| --travelTimePassingFloor_ms 500 | ||
| --btnDepressedTime_ms 200 | ||
|
|
||
| --numFloors 4 // Minimum: 2, maximum: 9 | ||
|
|
||
| --com_ip localhost | ||
| --com_port 15657 | ||
|
|
||
| --light_off - | ||
| --light_on * | ||
|
|
||
| --key_ordersUp qwertyui // 2 to 8 characters | ||
| --key_ordersDown sdfghjkl // 2 to 8 characters | ||
| --key_ordersCab zxcvbnm,. // 2 to 9 characters | ||
| --key_stopButton p | ||
| --key_obstruction - | ||
| --key_moveUp 9 | ||
| --key_moveStop 8 | ||
| --key_moveDown 7 |
| @@ -0,0 +1,206 @@ | ||
| import std.stdio, | ||
| std.conv, | ||
| std.variant, | ||
| std.concurrency, | ||
| std.algorithm, | ||
| std.range, | ||
| std.typecons, | ||
| std.typetuple, | ||
| std.datetime, | ||
| std.traits, | ||
| core.time, | ||
| core.thread, | ||
| core.sync.mutex; | ||
|
|
||
| /+ | ||
| struct DeleteEvent(T){ | ||
| alias T type; | ||
| } | ||
| void testThread(){ | ||
| writeln("testThread thisTid: ", thisTid); | ||
| while(true){ | ||
| receive( | ||
| (int i){ | ||
| writeln(" testThread received int: ", i); | ||
| if(i > 5){ | ||
| i = 1; | ||
| addEvent(ownerTid, 50.msecs, "should not arrive"); | ||
| ownerTid.send(DeleteEvent!string()); | ||
| } | ||
| addEvent(thisTid, i.seconds, i+1); | ||
| }, | ||
| (string s){ | ||
| writeln(" testThread received string: ", s); | ||
| }, | ||
| (Variant v){ | ||
| writeln(" testThread received Variant: ", v); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| void main(){ | ||
| writeln("main thisTid: ", thisTid); | ||
| Clock.currTime.writeln; | ||
| auto testTid = spawn(&testThread); | ||
| assert(testTid != thisTid); | ||
| writeln("spawned ", testTid); | ||
| addEvent(testTid, 1.seconds, 1); | ||
| addEvent(thisTid, 2.seconds, 500, Yes.periodic); | ||
| addEvent(thisTid, 2.seconds + 400.msecs, 600, Yes.periodic); | ||
| addEvent(testTid, Clock.currTime + 2.seconds, "hello from main"); | ||
| while(true){ | ||
| receive( | ||
| (int i){ | ||
| writeln(" main received int: ", i); | ||
| }, | ||
| (string s){ | ||
| writeln(" main received string: ", s); | ||
| }, | ||
| (DeleteEvent!string d){ | ||
| deleteEvent(thisTid, typeid(string), Delete.all); | ||
| }, | ||
| (Variant v){ | ||
| writeln(" main received Variant: ", v); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| +/ | ||
|
|
||
| void addEvent(T)(Tid receiver, SysTime time, T value){ | ||
| //writeln(" Adding event: ", Event(receiver, Variant(value), t, false, Duration.init)); | ||
| synchronized(events_lock){ | ||
| events ~= Event(receiver, Variant(value), time, false, Duration.init); | ||
| } | ||
| t.send(EventsModified()); | ||
| } | ||
|
|
||
|
|
||
| void addEvent(T)(Tid receiver, Duration dt, T value, Flag!"periodic" periodic = No.periodic){ | ||
| //writeln(" Adding event: ", Event(receiver, Variant(value), Clock.currTime + dt, periodic, dt)); | ||
| synchronized(events_lock){ | ||
| events ~= Event(receiver, Variant(value), Clock.currTime + dt, periodic, dt); | ||
| } | ||
| t.send(EventsModified()); | ||
| } | ||
|
|
||
| void deleteEvent(Tid receiver, TypeInfo type, Delete which){ | ||
| //writeln(" Deleting event: ", receiver, " ", type, " ", which); | ||
| synchronized(events_lock){ | ||
| final switch(which) with(Delete){ | ||
| case all: | ||
| events = events.remove!(a => a.receiver == receiver && a.value.type == type)(); | ||
| break; | ||
| case first: | ||
| auto idx = events.countUntil!(a => a.receiver == receiver && a.value.type == type); | ||
| if(idx != -1){ | ||
| events = events.remove(idx); | ||
| } | ||
| break; | ||
| case last: | ||
| auto idx = events.length - 1 - events.retro.countUntil!(a => a.receiver == receiver && a.value.type == type); | ||
| if(idx != -1){ | ||
| events = events.remove(idx); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| t.send(EventsModified()); | ||
| } | ||
|
|
||
| enum Delete { | ||
| all, | ||
| first, | ||
| last | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| private: | ||
|
|
||
| shared static this(){ | ||
| events_lock = new Mutex; | ||
| t = spawn(&proc); | ||
| } | ||
|
|
||
|
|
||
| struct Event { | ||
| Tid receiver; | ||
| Variant value; | ||
| SysTime triggerTime; | ||
| bool periodic; | ||
| Duration period; | ||
| } | ||
|
|
||
| struct EventsModified {} | ||
|
|
||
|
|
||
| __gshared Tid t; | ||
| __gshared Mutex events_lock; | ||
| __gshared Event[] events; | ||
|
|
||
|
|
||
|
|
||
| void proc(){ | ||
| Duration timeUntilNext = 1.hours; | ||
|
|
||
| while(true){ | ||
| //writeln("Time until next: ", timeUntilNext); | ||
| receiveTimeout( timeUntilNext, | ||
| (EventsModified n){ | ||
| }, | ||
| (OwnerTerminated o){ | ||
| }, | ||
| (Variant v){ | ||
| } | ||
| ); | ||
| timeUntilNext = 1.hours; | ||
| synchronized(events_lock){ | ||
| events.sort!(q{a.triggerTime < b.triggerTime})(); | ||
| //events.map!(a => a.to!string ~ "\n").reduce!((a, b) => a ~ b).writeln; | ||
| iter: | ||
| foreach(idx, ref item; events){ | ||
| if(Clock.currTime >= item.triggerTime){ | ||
| item.receiver.send(item.value); | ||
| if(item.periodic){ | ||
| item.triggerTime += item.period; | ||
| } else { | ||
| events = events.remove(idx); | ||
| goto iter; | ||
| } | ||
| } | ||
| } | ||
| auto now = Clock.currTime; | ||
| timeUntilNext = events.length ? | ||
| max(events.map!(a => a.triggerTime - now).reduce!min, 0.seconds) : | ||
| 1.hours; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|