Skip to content

Commit

Permalink
Adds an Arduino template that generate constant arrays in program mem…
Browse files Browse the repository at this point in the history
…ory.
  • Loading branch information
modlfo committed Aug 28, 2022
1 parent 860b2b7 commit 8167652
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/generators/codeC.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ let rec printExp (params : params) (e : cexp) : Pla.t =
let telems = Pla.map_sep sop (printExp params) elems in
[%pla {|(<#telems#>)|}]
| CEVar (name, _) -> Pla.string name
| CEIndex (e, index, _) when params.template = "arduino" ->
let index = printExp params index in
let e = printExp params e in
[%pla {|pgm_read_word_near(<#e#> + <#index#>)|}]
| CEIndex (e, index, _) ->
let index = printExp params index in
let e = printExp params e in
Expand Down Expand Up @@ -273,7 +277,8 @@ and printStmt (params : params) (stmt : cstmt) : Pla.t option =
then (
let tlhs = printLhsExp params true lhs in
let te = printExp params value in
Some [%pla {|static const <#tlhs#> = <#te#>;|}])
let progmem = if params.template = "arduino" then Pla.string "PROGMEM " else Pla.unit in
Some [%pla {|static const <#tlhs#> <#progmem#>= <#te#>;|}])
else None
(* All other cases should be errors *)
| CSConst _ -> failwith "printStmt: invalid constant declaration"
Expand Down
2 changes: 1 addition & 1 deletion src/generators/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ let checksForVCVPrototype (config : config) (args : args) =

let checkConfig (config : config) (args : args) =
let () = checksForVCVPrototype config args in
if (args.code = CCode && args.template <> "default")
if (args.code = CCode && args.template <> "default" && args.template <> "arduino")
|| (args.code = LuaCode && args.template = "default")
|| args.code = JSCode
then
Expand Down
56 changes: 56 additions & 0 deletions src/generators/templates/templates.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,61 @@ module Default = struct
#define <#file#s>_TABLES_H


<#code#>

#endif // <#file#s>_TABLES_H
|}]
;;

let header (params : params) (code : Pla.t) : Pla.t =
let file = String.uppercase params.output in
let tables = params.output in
[%pla
{|
/* Code automatically generated by Vult https://github.com/modlfo/vult */
#ifndef <#file#s>_H
#define <#file#s>_H
#include <stdint.h>
#include <math.h>
#include "vultin.h"
#include "<#tables#s>.tables.h"

<#code#>

#endif // <#file#s>_H
|}]
;;

let implementation (params : params) (code : Pla.t) : Pla.t =
let output = params.output in
[%pla
{|
/* Code automatically generated by Vult https://github.com/modlfo/vult */
#include "<#output#s>.h"

<#code#>
|}]
;;

let get (params : params) (header_code : Pla.t) (impl_code : Pla.t) (tables_code : Pla.t) : (Pla.t * FileKind.t) list =
[ header params header_code, FileKind.ExtOnly "h"
; implementation params impl_code, FileKind.ExtOnly "cpp"
; tables params tables_code, FileKind.ExtOnly "tables.h"
]
;;
end

module Arduino = struct
let tables (params : params) (code : Pla.t) : Pla.t =
let file = String.uppercase params.output in
[%pla
{|
/* Code automatically generated by Vult https://github.com/modlfo/vult */
#ifndef <#file#s>_TABLES_H
#define <#file#s>_TABLES_H

#include <avr/pgmspace.h>

<#code#>

#endif // <#file#s>_TABLES_H
Expand Down Expand Up @@ -58,6 +113,7 @@ let apply (params : params) (header_code : Pla.t) (impl_code : Pla.t) (tables :
let template =
match params.template with
| "none" -> Default.get
| "arduino" -> Arduino.get
| "default" -> Default.get
| "teensy" -> TeensyAudio.get
| "pd" -> Pd.get
Expand Down

0 comments on commit 8167652

Please sign in to comment.