aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Embroidery/libembroidery/examples/barebones/barebones.ino
blob: 32c12fcf2e193476f1d6fc4c8f3b2cca0baf103d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//libembroidery barebones example
//TODO: This is a work in progress sketch using libembroidery
//TODO: NOTE: you may need to modify the SD_CS and/or supply your own sample file

#include <SD.h>
#include "emb-pattern.h"
#include "geom-arc.h" //TODO: remove this later

#define SD_CS 5 // Card select

//SPCR States (SPI Control Register)
uint8_t SPI_SDCARD = 0; // Set the SPCR to this before reading

uint32_t stitchCount = 0;

void setup(void)
{
    Serial.begin(9600);
    Serial.println(F("libembroidery barebones example"));

    Serial.print(F("Initializing SD card..."));
    if(!SD.begin(SD_CS))
    {
        Serial.println(F("failed!"));
        return;
    }
    Serial.println(F("OK!"));
    SPI_SDCARD = SPCR; //Save the state for later use
}

void loop(void)
{
    loadEmb("sample.exp");
}

void loadEmb(char* fileName)
{
    SPCR = SPI_SDCARD;

    if(!SD.exists(fileName))
    {
        Serial.println("file DOES NOT exist!.");
        return;
    }

    //==============================
    // Reading
    //==============================
    EmbPattern* p = 0;
    int successful = 0;

    p = embPattern_create();
    if(!p) { Serial.println(F("ERROR: Could not allocate memory for embroidery pattern.")); return; }

    successful = embPattern_read(p, fileName);
    if(!successful)
    {
        Serial.print("ERROR: Reading file was unsuccessful: ");
        Serial.println(fileName);
        embPattern_free(p);
        return;
    }

    //==============================
    // Report Design Info
    //==============================
    Serial.print(F("Total Stitches: "));
    Serial.println(stitchCount, DEC); //TODO: For some reason we are missing the last 2 stitches. Investigate embPattern_addStitchAbs() causing a small memory leak.
    stitchCount = 0;

    //==============================
    // Cleanup
    //==============================
    embPattern_free(p);
}

void eventHandler_addStitchAbs(EmbPattern* p, double x, double y, int flags, int isAutoColorIndex)
{
    stitchCount++;
    Serial.print(F("Adding Stitch: "));
    Serial.println(stitchCount, DEC);
}

/* kate: bom off; indent-mode cstyle; indent-width 4; replace-trailing-space-save on; */