add a button that turns led on/off

This commit is contained in:
MegiDervishi 2021-01-05 14:48:33 +01:00
parent aab6172a36
commit 18e7769c96
14 changed files with 237 additions and 33 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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 ()

View file

@ -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

View file

@ -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;
() = lcd_blink_msg ((2, 1, "hello world!", 0) when not start);
() = lcd_counter ((1, 1) when not start);
i = 0 fby (i+1);
value = dread(bpin);
() = dwrite((ledpin, value));
tel
node main() returns ()
let
() = button (8, 13);
tel

42
led_button/Makefile Normal file
View file

@ -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

55
led_button/arduinolib.c Normal file
View file

@ -0,0 +1,55 @@
#include <avr/io.h>
#include <util/delay.h>
#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;
}

View file

@ -0,0 +1,3 @@
fun dwrite(p:int; v:int) returns ()
fun dread(p:int) returns (v: int)

1
led_button/arduinolib.h Normal file
View file

@ -0,0 +1 @@
#include "arduinolib_types.h"

View file

@ -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

15
led_button/main.c Normal file
View file

@ -0,0 +1,15 @@
#include <avr/io.h>
#include <util/delay.h>
#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;
}

4
led_button/main.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef MAIN_H
#define MAIN_H
#include "prog_c/prog.h"
#endif

16
led_button/prog.ept Normal file
View file

@ -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

View file

@ -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