diff --git a/lcd_hept/Makefile b/lcd_hept/Makefile index 7d5678e..b5b61fd 100644 --- a/lcd_hept/Makefile +++ b/lcd_hept/Makefile @@ -2,7 +2,7 @@ CC = avr-gcc OBJCOPY = avr-objcopy AVRDUDE = avrdude CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p -SERIAL = /dev/cu.usbmodem143401 +SERIAL = /dev/cu.usbmodem141401 ADDR = $(shell heptc -where)/c TARGET = prog LIB = arduinolib diff --git a/lcd_hept/arduinolib.c b/lcd_hept/arduinolib.c index 4757f5d..357a6eb 100644 --- a/lcd_hept/arduinolib.c +++ b/lcd_hept/arduinolib.c @@ -54,12 +54,6 @@ void Arduinolib__lcd_init_step(Arduinolib__lcd_init_out *out){ Arduinolib__lcd_cmd_step(LCD_DISPLAY_ON, o9); _delay_us(80); //more than 39us - - // if we use this code the msg is printed /// - /*uint8_t msg[] = "Arduino"; - Arduinolib__lcd_string_out *o; - Arduinolib__lcd_string_step(msg, o);*/ - } /* send an 8bit information to the LCD register in order to be written */ @@ -104,9 +98,9 @@ void Arduinolib__lcd_cmd_step(uint8_t cmd, Arduinolib__lcd_cmd_out *out){ LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //low LCD_E_PORT &= ~(1 << LCD_E_PIN); //low Arduinolib__lcd_write_out *o1; - Arduinolib__lcd_write_step(cmd, o1); // upper 4 bits + Arduinolib__lcd_write_step(cmd, o1); // write upper 4 bits Arduinolib__lcd_write_out *o2; - Arduinolib__lcd_write_step(cmd << 4, o2); // lower 4 bits + Arduinolib__lcd_write_step(cmd << 4, o2); // write lower 4 bits } /* send an 8bit data character to the LCD register */ @@ -115,9 +109,9 @@ void Arduinolib__lcd_char_step(uint8_t char_data, Arduinolib__lcd_char_out *out) LCD_RS_PORT |= (1 << LCD_RS_PIN); //high LCD_E_PORT &= ~(1 << LCD_E_PIN); //low Arduinolib__lcd_write_out *o1; - Arduinolib__lcd_write_step(char_data, o1); // upper 4 bits + Arduinolib__lcd_write_step(char_data, o1); //write upper 4 bits Arduinolib__lcd_write_out *o2; - Arduinolib__lcd_write_step(char_data << 4, o2); // lower 4 bits + Arduinolib__lcd_write_step(char_data << 4, o2); //write lower 4 bits } /* give a string to display to the LCD*/ @@ -126,10 +120,10 @@ void Arduinolib__lcd_string_step(uint8_t lstring[], int line, Arduinolib__lcd_st Arduinolib__lcd_cmd_out *o2; if (line == 1) { Arduinolib__lcd_cmd_step(LCD_CURSOR_SET | LCD_START_LINE2, o1); - _delay_us(80); //more than + _delay_us(80); //more than 39us } else { Arduinolib__lcd_cmd_step(LCD_CURSOR_SET | LCD_START_LINE1, o2); - _delay_us(80); + _delay_us(80); //more than 39us } volatile int i = 0; for (i = 0; lstring[i] != 0 ; i++) { @@ -141,9 +135,61 @@ void Arduinolib__lcd_string_step(uint8_t lstring[], int line, Arduinolib__lcd_st /* gice a number to display to the LCD */ void Arduinolib__lcd_int_step(uint8_t numb, int line, Arduinolib__lcd_int_out *out){ - char lstring[80]; + char lstring[80]; //buffer itoa(numb, lstring, 10); Arduinolib__lcd_string_out *o; Arduinolib__lcd_string_step(lstring, line, o); } +/* a general read pin function. it is assuming the user uses distinct pins to read/write */ +void Arduinolib__dread_step(int p, Arduinolib__dread_out *out){ + if (8 <= p && p <= 13){ + int pin = p - 8; + DDRB &= ~(1 << pin); + if ((PINB & (1 << pin)) == (1 << pin) ) { + out -> v = 1; //pin high so it is pressed + } + else { + out -> v = 0; //pin low so it is not pressed + } + } + else if ( 0 <= p && p <= 7){ + DDRD &= ~(1 << p); + if ((PIND & (1 << p)) == (1 << p) ) { + out -> v = 1; //pin high so it is pressed + } else { + out -> v = 0; //pin low so it is not pressed + } + } + else { //by default to pin 13 of arduino + DDRB &= ~(1 << 5); + if ((PINB & (1 << 5)) == (1 << 5) ) { + out -> v = 1; //pin high so it is pressed + } + else { + out -> v = 0; //pin low so it is not pressed + } + } + return; +} + +void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out){ + + if (8 <= p && p <= 13){ + int pin = p - 8; + DDRB |= (1 << pin); + PORTB |= (1 << pin); + if (v == 0) PORTB &= ~(1 << pin); + } + else if ( 0 <= p && p <= 7){ + DDRD |= (1 << p); + PORTD |= (1 << p); + if (v == 0) PORTD &= ~(1 << p); + } + else { //by default + DDRB |= (1 << 5); + PORTB |= (1 << 5); + if (v == 0) PORTB &= ~(1 << 5); + } + return; +} diff --git a/lcd_hept/arduinolib.epi b/lcd_hept/arduinolib.epi index 067bb6e..8f86a7f 100644 --- a/lcd_hept/arduinolib.epi +++ b/lcd_hept/arduinolib.epi @@ -6,3 +6,5 @@ fun lcd_cmd(cmd:uint8_t) returns () fun lcd_char(char_data:uint8_t) returns () fun lcd_string (lstring:string; line:int) returns () fun lcd_int (numb:uint8_t; line:int) returns () +fun dread(buttonpin: int) returns (v : int) +fun dwrite(p:int; v:int) returns () \ No newline at end of file diff --git a/lcd_hept/arduinolib_types.h b/lcd_hept/arduinolib_types.h index 7180505..8f2c4fa 100644 --- a/lcd_hept/arduinolib_types.h +++ b/lcd_hept/arduinolib_types.h @@ -1,16 +1,6 @@ #ifndef ARDUINOLIB_H #define ARDUINOLIB_H -//typedef struct string { -// const char *msg -//} Arduinolib__string_out; - -typedef struct byte_out { - uint8_t value ; -} byte_out ; - -typedef byte_out Arduinolib__bitOr_out ; - typedef struct Arduinolib__lcd_init { } Arduinolib__lcd_init_out; @@ -29,6 +19,13 @@ typedef struct Arduinolib__lcd_string { typedef struct Arduinolib__lcd_int { } Arduinolib__lcd_int_out; +typedef struct Arduinolib__dread { + int v; +} Arduinolib__dread_out; + +typedef struct Arduinolib__dwrite { +} Arduinolib__dwrite_out; + void Arduinolib__lcd_init_step(Arduinolib__lcd_init_out *o); void Arduinolib__lcd_write_step(uint8_t info, Arduinolib__lcd_write_out *o); @@ -41,4 +38,8 @@ void Arduinolib__lcd_string_step(uint8_t lstring[], int line, Arduinolib__lcd_st void Arduinolib__lcd_int_step(uint8_t numb, int line, Arduinolib__lcd_int_out *o); +void Arduinolib__dread_step(int p, Arduinolib__dread_out *out); + +void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out); + #endif \ No newline at end of file diff --git a/lcd_hept/prog.ept b/lcd_hept/prog.ept index be1444e..18a6d9e 100644 --- a/lcd_hept/prog.ept +++ b/lcd_hept/prog.ept @@ -23,14 +23,18 @@ let () = lcd_int(count/period, line); tel -node main() returns () -var start : bool; +node button (bpin, ledpin : int) returns () +var value : int; + i : int; let - start = true fby false; - () = lcd_init() when start; + i = 0 fby (i+1); + value = dread(bpin); + () = dwrite((ledpin, value)); +tel - () = lcd_blink_msg ((2, 1, "hello world!", 0) when not start); - () = lcd_counter ((1, 1) when not start); +node main() returns () +let + () = button (8, 13); tel diff --git a/led_button/Makefile b/led_button/Makefile new file mode 100644 index 0000000..b5b61fd --- /dev/null +++ b/led_button/Makefile @@ -0,0 +1,42 @@ +CC = avr-gcc +OBJCOPY = avr-objcopy +AVRDUDE = avrdude +CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p +SERIAL = /dev/cu.usbmodem141401 +ADDR = $(shell heptc -where)/c +TARGET = prog +LIB = arduinolib +MAIN = main + + +.PHONY: all clean flash + +all: $(TARGET).hex + +flash: $(TARGET).hex + $(AVRDUDE) -F -V -c arduino -p ATMEGA328P -P $(SERIAL) -b 115200 \ + -U flash:w:$< + + +$(TARGET).hex: $(TARGET) + $(OBJCOPY) -O ihex -R .eeprom $< $@ + + +$(TARGET): $(TARGET).o + $(CC) $(CFLAGS) -o $@ *.o + +$(TARGET).o : $(TARGET).c + $(CC) -g $(CFLAGS) -c -I $(ADDR) -I . $(LIB).c $(TARGET)_c/*.c $(MAIN).c + + +$(TARGET).c: + heptc $(LIB).epi + heptc -target c $(TARGET).ept + + +clean: + rm -f $(TARGET) $(TARGET).hex $(TARGET).o $(TARGET).log $(TARGET).mls + rm -f *.o *.epci + rm -r $(TARGET)_c + + diff --git a/led_button/arduinolib.c b/led_button/arduinolib.c new file mode 100644 index 0000000..fad2a12 --- /dev/null +++ b/led_button/arduinolib.c @@ -0,0 +1,55 @@ +#include +#include +#include "arduinolib.h" + +void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out){ + + if (8 <= p && p <= 13){ + int pin = p - 8; + DDRB |= (1 << pin); + PORTB |= (1 << pin); + if (v == 0) PORTB &= ~(1 << pin); + } + else if ( 0 <= p && p <= 7){ + DDRD |= (1 << p); + PORTD |= (1 << p); + if (v == 0) PORTD &= ~(1 << p); + } + else { //by default + DDRB |= (1 << 5); + PORTB |= (1 << 5); + if (v == 0) PORTB &= ~(1 << 5); + } + return; +} + +void Arduinolib__dread_step(int p, Arduinolib__dread_out *out){ + if (8 <= p && p <= 13){ + int pin = p - 8; + DDRB &= ~(1 << pin); + if ((PINB & (1 << pin)) == (1 << pin) ) { + out -> v = 1; //pin high so it is pressed + } + else { + out -> v = 0; //pin low so it is not pressed + } + } + else if ( 0 <= p && p <= 7){ + DDRD &= ~(1 << p); + if ((PIND & (1 << p)) == (1 << p) ) { + out -> v = 1; //pin high so it is pressed + } else { + out -> v = 0; //pin low so it is not pressed + } + } + else { //by default to pin 13 of arduino + DDRB &= ~(1 << 5); + if ((PINB & (1 << 5)) == (1 << 5) ) { + out -> v = 1; //pin high so it is pressed + } + else { + out -> v = 0; //pin low so it is not pressed + } + } + return; +} diff --git a/led_button/arduinolib.epi b/led_button/arduinolib.epi new file mode 100644 index 0000000..515e324 --- /dev/null +++ b/led_button/arduinolib.epi @@ -0,0 +1,3 @@ +fun dwrite(p:int; v:int) returns () +fun dread(p:int) returns (v: int) + diff --git a/led_button/arduinolib.h b/led_button/arduinolib.h new file mode 100644 index 0000000..74ec448 --- /dev/null +++ b/led_button/arduinolib.h @@ -0,0 +1 @@ +#include "arduinolib_types.h" diff --git a/led_button/arduinolib_types.h b/led_button/arduinolib_types.h new file mode 100644 index 0000000..0d89945 --- /dev/null +++ b/led_button/arduinolib_types.h @@ -0,0 +1,15 @@ +#ifndef ARDUINOLIB_H +#define ARDUINOLIB_H + +typedef struct Arduinolib__dwrite { +} Arduinolib__dwrite_out; + +typedef struct Arduinolib__dread { + int v; +} Arduinolib__dread_out; + +void Arduinolib__dwrite_step(int p, int v,Arduinolib__dwrite_out *o); + +void Arduinolib__dread_step(int p, Arduinolib__dread_out *o); + +#endif diff --git a/led_button/main.c b/led_button/main.c new file mode 100644 index 0000000..5ac2302 --- /dev/null +++ b/led_button/main.c @@ -0,0 +1,15 @@ +#include +#include +#include "main.h" + +int main (void) +{ + Prog__main_mem mem; + Prog__main_reset(&mem); + while(1){ + Prog__main_out _res; + Prog__main_step(&_res, &mem); + _delay_ms(1); +} +return 0; +} \ No newline at end of file diff --git a/led_button/main.h b/led_button/main.h new file mode 100644 index 0000000..7269ffa --- /dev/null +++ b/led_button/main.h @@ -0,0 +1,4 @@ +#ifndef MAIN_H +#define MAIN_H +#include "prog_c/prog.h" +#endif diff --git a/led_button/prog.ept b/led_button/prog.ept new file mode 100644 index 0000000..ffd79c1 --- /dev/null +++ b/led_button/prog.ept @@ -0,0 +1,16 @@ +open Arduinolib + +node button (bpin, ledpin : int) returns () +var value : int; + i : int; +let + i = 0 fby (i+1); + value = dread(bpin); + () = dwrite((ledpin, value)); +tel + +node main() returns () +let + () = button(8, 13); +tel + diff --git a/parallel_led/Makefile b/parallel_led/Makefile index 7d5678e..b5b61fd 100644 --- a/parallel_led/Makefile +++ b/parallel_led/Makefile @@ -2,7 +2,7 @@ CC = avr-gcc OBJCOPY = avr-objcopy AVRDUDE = avrdude CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p -SERIAL = /dev/cu.usbmodem143401 +SERIAL = /dev/cu.usbmodem141401 ADDR = $(shell heptc -where)/c TARGET = prog LIB = arduinolib