Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions M5Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) M5Stack. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "M5Stack.h"

void M5Stack::begin()
{
Serial.begin(115200);

// Beep init
pinMode(BEEP_PIN, OUTPUT);
digitalWrite(BEEP_PIN, 0);

// Setup the button with an internal pull-up
btn_pins[BTN_A] = BUTTON_A_PIN;
btn_pins[BTN_B] = BUTTON_B_PIN;
btn_pins[BTN_C] = BUTTON_C_PIN;

// M5.lcd INIT
lcd.begin();
lcd.fillScreen(BLACK);
lcd.setCursor(0, 0);
lcd.setTextColor(WHITE);
lcd.setTextSize(1);

// m5.lcd.drawPicture(47, 38, 120, 96, gImage_logo);
if(!SD.begin(TFCARD_CS_PIN)) {
Serial.println("Card Mount Failed");
}
}

void M5Stack::loop()
{
// buttons reads each button btn_states and store it
for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
pinMode(btn_pins[thisButton], INPUT_PULLUP); //enable internal pull up resistors
if (digitalRead(btn_pins[thisButton]) == LOW) { //if button pressed
btn_states[thisButton]++; //increase button hold time
} else {
if (btn_states[thisButton] == 0)//button idle
continue;
if (btn_states[thisButton] == 0xFF)//if previously released
btn_states[thisButton] = 0; //set to idle
else
btn_states[thisButton] = 0xFF; //button just released
}
pinMode(btn_pins[thisButton], INPUT); //disable internal pull up resistors to save power
}

// TFTLCD button update
lcd.buttonUpdate();
}

uint8_t M5Stack::bootSetup()
{
//-------BOOT MENU---------
lcd.fillScreen(WHITE);
lcd.setFont(&FreeSansOblique9pt7b);

//--------APP STORE---------
lcd.drawPicture(0, 0, 220, 175, gImage_select_backguand);
lcd.fillRect(0, 30, 219, 120, WHITE);
uint8_t select_app_id = selectMenu();
Serial.printf("downloading the app:%d\r\n", select_app_id);
lcd.fillScreen(BLACK);
lcd.setTextColor(WHITE);
lcd.setFont();
lcd.setTextSize(1);
lcd.setCursor(2, 20);

//----------downloading----------
lcd.fillScreen(WHITE);
drawTitle("LOADING...", GRAY);
lcd.drawPicture(47, 38, 120, 96, gImage_logo);

int progress_p=0;
while(++progress_p < 100) {
// Serial.printf("progress:%d%%\r\n", progress_p);
lcd.ProgressBar(20, 146, 180, 13, progress_p);
delay(1);
}
return select_app_id;
}

/*
* Returns true when 'button' is pressed.
* The button has to be released for it to be triggered again.
*/
bool M5Stack::pressed(uint8_t button) {
if (btn_states[button] == 1)
return true;
else
return false;
}

/*
* return true if 'button' is released
*/
bool M5Stack::released(uint8_t button) {
if (btn_states[button] == 0xFF)
return true;
else
return false;
}

/**
* returns true ONCE when 'button' is held for 'time' frames
* @param button The button's ID
* @param time How much frames button must be held, between 1 and 254.
* @return true when 'button' is held for 'time' frames
*/
bool M5Stack::held(uint8_t button, uint8_t time){
if(btn_states[button] == (time+1))
return true;
else
return false;
}

/**
* returns true every 'period' frames when 'button' is held
* @param button The button's ID
* @param period How much frames button must be held, between 1 and 254.
* @return true if the button is held for the given time
*/
bool M5Stack::repeat(uint8_t button, uint8_t period) {
if (period <= 1) {
if ((btn_states[button] != 0xFF) && (btn_states[button]))
return true;
} else {
if ((btn_states[button] != 0xFF) && ((btn_states[button] % period) == 1))
return true;
}
return false;
}

/**
*
* @param button The button's ID
* @return The number of frames during which the button has been held.
*/
uint8_t M5Stack::timeHeld(uint8_t button){
if(btn_states[button] != 0xFF)
return btn_states[button];
else
return 0;
}

M5Stack m5;
43 changes: 43 additions & 0 deletions M5Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) M5Stack. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef _M5STACK_H_
#define _M5STACK_H_

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiMulti.h>
#include <Wire.h>
#include "FS.h"
#include "SD.h"
#include "utility/display.h"
#include "utility/bmp_map.h"
#include "utility/bootmenu.h"
#include "utility/config.h"

class M5Stack {
public:
void begin();
uint8_t bootSetup();
void loop();

// button API
bool pressed(uint8_t button);
bool released(uint8_t button);
bool held(uint8_t button, uint8_t time);
bool repeat(uint8_t button, uint8_t period);
uint8_t timeHeld(uint8_t button);

M5STACK_TFTLCD lcd;

private:
uint8_t btn_pins[NUM_BTN];
uint8_t btn_states[NUM_BTN];
};

extern M5Stack m5stack;
#define m5 m5stack
#define M5 m5stack

#endif
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# M5Stack
M5stack Arduino library
Binary file added examples/.DS_Store
Binary file not shown.
Binary file added examples/Advanced/.DS_Store
Binary file not shown.
Binary file added examples/Basics/.DS_Store
Binary file not shown.
163 changes: 163 additions & 0 deletions examples/Basics/AppStore/AppStore.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include <M5Stack.h>
#include "clock.h"
#include "DHT.h"

#define DHTPIN 22 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
uint8_t select_app_id;

void setup()
{
m5.begin();
select_app_id = m5.bootSetup();

m5.lcd.fillScreen(WHITE);
m5.lcd.drawPicture(47, 38, 120, 96, gImage_logo);

switch (select_app_id) {
case 0:
wifi_scan_setup();
break;

case 1:
DHT11_setup();
break;

case 2:
clock_setup();
break;
}
}

void loop()
{
switch (select_app_id) {
case 0:
wifi_scan_loop();
break;

case 1:
DHT11_loop();
break;

case 2:
clock_loop();
break;
}

m5.loop();
}

//-------------------------
void DHT11_setup()
{
dht.begin();
m5.lcd.fillScreen(WHITE);
m5.lcd.drawTitle("Environment", 0x09F1);
#define X_OFFSET 40
#define Y_POS 165
}

void DHT11_loop()
{
static uint32_t sampling_tick;
static float pre_hif, pre_hic;
static bool f2c_flag=0;

if(millis() > sampling_tick) {
sampling_tick = millis() + 1000;
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");

if((pre_hif!=hif) || (pre_hic!=hic)) {
pre_hif = hif;
pre_hic = hic;

m5.lcd.setFont(&FreeMonoBoldOblique12pt7b);
// m5.lcd.setFont(&FreeMonoBoldOblique18pt7b);
m5.lcd.fillRect(19, 70, 190, 30, WHITE);
m5.lcd.setTextColor(GRAY);
m5.lcd.setTextSize(1);
m5.lcd.setCursor(25, 90);
if(f2c_flag) {
m5.lcd.printf("%2.1fF %2.1f%%", hif, h);
} else {
m5.lcd.printf("%2.1fC %2.1f%%", hic, h);
}
}
}
if(m5.pressed(BUTTON_B)) {
f2c_flag = !f2c_flag;
sampling_tick = 0;
pre_hif=0;
}
}

void wifi_scan_setup()
{
m5.lcd.setFont();
}

void wifi_scan_loop()
{
m5.lcd.setCursor(0, 0);
// m5.lcd.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
m5.lcd.fillScreen(BLACK);
m5.lcd.println("wifi scan done");
if (n == 0)
{
m5.lcd.println("no networks found");
}
else
{
m5.lcd.print(n);
m5.lcd.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
m5.lcd.printf("%2d", i + 1);
m5.lcd.print(": ");
m5.lcd.print(WiFi.SSID(i));
m5.lcd.print(" (");
m5.lcd.print(WiFi.RSSI(i));
m5.lcd.print(")");
m5.lcd.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
delay(3000);
}
Loading