Skip to content

Commit

Permalink
Mudando programa para event-driven.
Browse files Browse the repository at this point in the history
  • Loading branch information
Milton Maldonado Jr committed Jul 17, 2019
1 parent aea0695 commit a1eb29c
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Debug/
25 changes: 25 additions & 0 deletions .settings/language.settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<configuration id="cdt.managedbuild.config.gnu.exe.debug.769796154" name="Debug">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1035940305784832089" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
</extension>
</configuration>
<configuration id="cdt.managedbuild.config.gnu.exe.release.391794649" name="Release">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1035940305784832089" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
</extension>
</configuration>
</project>
31 changes: 0 additions & 31 deletions and.c

This file was deleted.

23 changes: 0 additions & 23 deletions and.h

This file was deleted.

69 changes: 69 additions & 0 deletions bitand.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* and.c
*
* Created on: 16 de jul de 2019
* Author: milton
*/

#include <malloc.h>
#include "bitand.h"

////////////////////////////////////////////////////////////////////////////////
bitand *bitand_create(){

bitand *b = malloc(sizeof(bitand));
b->oldvalue = 2;
b->out_event_handler_root = NULL;
return b;
}

////////////////////////////////////////////////////////////////////////////////
void bitand_connect_out(bitand *a, void *obj, void (*event_handler)(void *obj, int val, int timestamp)){

new_ehandler(a->out_event_handler_root, obj, event_handler);
}

////////////////////////////////////////////////////////////////////////////////
int bitand_update(bitand *a){

a->value = a->ina & a->inb;

if (a->oldvalue != a->value){

a->oldvalue = a->value;
return 1;
}

return 0;
}

////////////////////////////////////////////////////////////////////////////////
void bitand_up(bitand *a, int val, int timestamp){

if (bitand_update(a)){

event e;
e.event_handler_root = a->out_event_handler_root;
e.value = a->value;
e.timestamp = timestamp+1;
event_insert(&e);
}
}

////////////////////////////////////////////////////////////////////////////////
void bitand_update_ina(bitand *a, int val, int timestamp){

if (val == a->ina) return;

a->ina = val;
bitand_up(a, val, timestamp);
}

////////////////////////////////////////////////////////////////////////////////
void bitand_update_inb(bitand *a, int val, int timestamp){

if (val == a->inb) return;

a->inb = val;
bitand_up(a, val, timestamp);
}
29 changes: 29 additions & 0 deletions bitand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* bitand.h
*
* Created on: 16 de jul de 2019
* Author: milton
*/

#ifndef BITAND_H_
#define BITAND_H_

#include "update.h"

typedef struct {

int ina;
int inb;
int oldvalue;
int value;
ehandler *out_event_handler_root;
} bitand;

bitand *bitand_create();

void bitand_connect_out(bitand *a, void *obj, void (*event_handler)(void *obj, int val, int timestamp));

void bitand_update_ina(bitand *a, int val, int timestamp);
void bitand_update_inb(bitand *a, int val, int timestamp);

#endif /* BITAND_H_ */
27 changes: 17 additions & 10 deletions bitswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@
* Author: milton
*/

#include <malloc.h>

#include "bitswitch.h"
#include "update.h"

////////////////////////////////////////////////////////////////////////////////
void bitswitch_init(bitswitch *s, int val){
bitswitch *bitswitch_create(){

s->value = val;
s->oldvalue = 1 - val;
bitswitch *b = malloc(sizeof(bitswitch));
b->oldvalue = 2;
b->out_event_handler_root = NULL;
return b;
}

////////////////////////////////////////////////////////////////////////////////
void bitswitch_connect_out(bitswitch *s, void *obj, void (*event_handler)(void *obj, int val, int timestamp)){

new_ehandler(s->out_event_handler_root, obj, event_handler);
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -24,12 +33,10 @@ void bitswitch_setval(bitswitch *s, int val){
if (s->oldvalue != s->value){

s->oldvalue = s->value;
update_run();
event e;
e.event_handler_root = s->out_event_handler_root;
e.value = s->value;
e.timestamp = 0;
event_insert(&e);
}
}

////////////////////////////////////////////////////////////////////////////////
int bitswitch_getval(bitswitch *s){

return s->value;
}
9 changes: 6 additions & 3 deletions bitswitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
#ifndef BITSWITCH_H_
#define BITSWITCH_H_

#include "update.h"

typedef struct{

int oldvalue;
int value;
ehandler *out_event_handler_root;
} bitswitch;

void bitswitch_init(bitswitch *s, int val);
bitswitch *bitswitch_create();

void bitswitch_setval(bitswitch *s, int val);
void bitswitch_connect_out(bitswitch *s, void *obj, void (*event_handler)(void *obj, int val, int timestamp));

int bitswitch_getval(bitswitch *s);
void bitswitch_setval(bitswitch *s, int val);

#endif /* BITSWITCH_H_ */
66 changes: 33 additions & 33 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,56 @@

#include <stdio.h>

#include "bitand.h"
#include "bitswitch.h"
#include "output.h"
#include "and.h"
#include "update.h"

////////////////////////////////////////////////////////////////////////////////
int main (int argc, char *argv[]){

bitswitch s1,s2;
bitand and1;
output os1,os2,oand;
bitswitch *s1,*s2;
bitand *and1;
output *os1,*os2,*oand;

bitswitch_init(&s1,0);
bitswitch_init(&s2,0);
s1 = bitswitch_create();
s2 = bitswitch_create();
and1 = bitand_create();
os1 = output_create("S1");
os2 = output_create("S2");
oand = output_create("AND");

bitand_connect(&and1,&s1.value,&s2.value);
bitswitch_connect_out(s1, and1, (void*)&bitand_update_ina);
bitswitch_connect_out(s2, and1, (void*)&bitand_update_inb);
bitand_connect_out(and1, oand, (void*)&output_update);

output_connect(&os1,"S1",&s1.value);
output_connect(&os2,"S2",&s2.value);
output_connect(&oand,"AND",&and1.value);
bitswitch_setval(s1, 0);
bitswitch_setval(s2, 0);

update_register(&and1,BITAND);
output_print(os1);
output_print(os2);
output_println(oand);

bitswitch_setval(&s1, 0);
bitswitch_setval(&s2, 0);
bitswitch_setval(s1, 1);
bitswitch_setval(s2, 0);

output_print(&os1);
output_print(&os2);
output_println(&oand);
output_print(os1);
output_print(os2);
output_println(oand);

bitswitch_setval(&s1, 1);
bitswitch_setval(&s2, 0);
bitswitch_setval(s1, 0);
bitswitch_setval(s2, 1);

output_print(&os1);
output_print(&os2);
output_println(&oand);
output_print(os1);
output_print(os2);
output_println(oand);

bitswitch_setval(&s1, 0);
bitswitch_setval(&s2, 1);
bitswitch_setval(s1, 1);
bitswitch_setval(s2, 1);

output_print(&os1);
output_print(&os2);
output_println(&oand);

bitswitch_setval(&s1, 1);
bitswitch_setval(&s2, 1);

output_print(&os1);
output_print(&os2);
output_println(&oand);
output_print(os1);
output_print(os2);
output_println(oand);

return 0;
}
20 changes: 14 additions & 6 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,33 @@
#include "output.h"

////////////////////////////////////////////////////////////////////////////////
void output_connect(output *o, char *name, int *getvalue){
output *output_create(char *name){

output *o = malloc(sizeof(output));
if (name){
o->name = malloc(strlen(name)+1);
strcpy(o->name,name);

o->name = malloc(1+strlen(name));
strcpy(o->name, name);
}
else
o->name = NULL;

o->getval = getvalue;
return o;
}

////////////////////////////////////////////////////////////////////////////////
void output_update(output *o, int val, int timestamp){

o->value = val;
}

////////////////////////////////////////////////////////////////////////////////
void output_print(output *o){

if (o->name)
printf("%s:%d ",o->name,*o->getval);
printf("%s:%d ",o->name,o->value);
else
printf("%d ",*o->getval);
printf("%d ",o->value);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 4 additions & 2 deletions output.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
typedef struct {

char *name;
int *getval;
int value;
} output;

void output_connect(output *o, char *name, int *getval);
output *output_create(char *name);

void output_update(output *o, int val, int timestamp);

void output_print(output *o);
void output_println(output *o);
Expand Down
Loading

0 comments on commit a1eb29c

Please sign in to comment.