-
@Wilberforce @jumjum
ESP32 Board: widora v5.0https://world.taobao.com/item/538209801368.htm
SD Card Adapter: Catalex Micro SD Card Adapterhttps://detail.tmall.com/item.htm?id=41690559539&spm=a1z09.2.0.0.816EuT&_u=61fd91teef0
Wiring:| Orange | Black | Blue | White | Yellow | Green | | 3V3 | GND | IO5 | IO18 | IO23 | IO19 | ESP32 Board | | VCC | GND | CS | SCK | MOSI | MISO | SD Card Adaptern |
Esrupino: master branch
env vars: export USE_FILESYSTEM=1
IDE: ESPRUINO WEB IDE
Javascript:/* | SPI | MOSI | MISO | CLK | SS/CS | PLATFORM | | SPI1 | 13 | 12 | 14 | 15 | ESP8266/ESP32 | | SPI2 | 23 | 19 | 18 | 5 | ESP32 only | */ var opts = {sck:18, miso:19, mosi:23}; SPI2.setup(opts); E.connectSDCard(SPI2, 5 /*CS*/); var fs = require("fs"); // list files on SD card console.log("\nFiles on SD Card -->\n"); console.log(fs.readdirSync()); // read a exitsting file on SD card console.log("\nContent of example.txt : -->\n"); console.log(fs.readFileSync("example.txt")); // creat a file fs.writeFile("test.txt", "Hello world!"); fs.appendFileSync("test.txt", " I'm Esrpusino."); // list files on SD card console.log("\nFiles on SD Card -->\n"); console.log(fs.readdirSync()); // read text.txt created before on SD card console.log("\nContent of test.txt : -->\n"); console.log(fs.readFileSync("test.txt")); // list files on SD card console.log("\nFiles on SD Card -->\n"); console.log(fs.readdirSync("mydir")); // read text2.txt in /mydir on SD card console.log("\nContent of test2.txt : -->\n"); console.log(fs.readFileSync("mydir/test2.txt"));
Content of SD Card before running the codes:
/EXAMPLE.txt
/mydir/test2.txtoutput in left hand side of WEB IDE:
>WARNING: jshReset(): To implement - reset of i2c _____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |_____|___| _|_| |___|_|_|_|___| |_| http://espruino.com 1v91.2721 Copyright 2016 G.Williams Espruino is Open Source. Our work is supported only by sales of official boards and donations: http://espruino.com/Donate > Files on SD Card --> [ "System Volume Information", "EXAMPLE.TXT", "._.Trashes", ".Trashes", ".Spotlight-V100", "mydir" ] Content of example.txt : --> A1234567890abcdefghigklmnopqrstuvwxyz Files on SD Card --> [ "System Volume Information", "EXAMPLE.TXT", "._.Trashes", "test.txt", ".Trashes", ".Spotlight-V100", "mydir" ] Content of test.txt : --> Hello world! I'm Esrpusino. Files on SD Card --> [ ".", "..", "test2.txt" ] Content of test2.txt : --> Hello, I'm in directory /mydir. =undefined
-
-
in mySSD1360.js, in exports.connectSPI function ,
if (rst) { digitalPulse(rst,0,10); /* for(var i = 0; i<10; i++){ digitalWrite(rst,0); digitalWrite(rst,1); } */ }
the OLED dosn't work.
then , try
if (rst) { //digitalPulse(rst,0,10); for(var i = 0; i<10; i++){ digitalWrite(rst,0); digitalWrite(rst,1); } }
the OLDE works well.
so, I think digitalPulse(rst,0,10) doesn't works correctly.
-
-
-
mySSD1306.js
/* Copyright (c) 2014 Sam Sykes, Gordon Williams. See the file LICENSE for copying permission. */ /* Module for the SSD1306 OLED controller in displays like the Crius CO-16 function go(){ // write some text g.drawString("Hello World!",2,2); // write to the screen g.flip(); } // I2C I2C1.setup({scl:B6,sda:B7}); var g = require("SSD1306").connect(I2C1, go); // or var g = require("SSD1306").connect(I2C1, go, { address: 0x3C }); // SPI var s = new SPI(); s.setup({mosi: B6, sck:B5}); var g = require("SSD1306").connectSPI(s, A8, B7, go); */ var C = { OLED_WIDTH : 128, OLED_CHAR : 0x40, OLED_CHUNK : 128 }; // commands sent when initialising the display var extVcc=false; // if true, don't start charge pump var initCmds = new Uint8Array([ 0xAe, // 0 disp off 0xD5, // 1 clk div 0x80, // 2 suggested ratio 0xA8, 63, // 3 set multiplex, height-1 0xD3,0x0, // 5 display offset 0x40, // 7 start line 0x8D, extVcc?0x10:0x14, // 8 charge pump 0x20,0x0, // 10 memory mode 0xA1, // 12 seg remap 1 0xC8, // 13 comscandec 0xDA, 0x12, // 14 set compins, height==64 ? 0x12:0x02, 0x81, extVcc?0x9F:0xCF, // 16 set contrast 0xD9, extVcc?0x22:0xF1, // 18 set precharge 0xDb, 0x40, // 20 set vcom detect 0xA4, // 22 display all on 0xA6, // 23 display normal (non-inverted) 0xAf // 24 disp on ]); // commands sent when sending data to the display var flipCmds = [ 0x21, // columns 0, C.OLED_WIDTH-1, 0x22, // pages 0, 7 /* (height>>3)-1 */]; function update(options) { if (options) { if (options.height) { initCmds[4] = options.height-1; initCmds[15] = options.height==64 ? 0x12 : 0x02; flipCmds[5] = (options.height>>3)-1; } if (options.contrast!==undefined) initCmds[17] = options.contrast; } } exports.connect = function(i2c, callback, options) { update(options); var oled = Graphics.createArrayBuffer(C.OLED_WIDTH,initCmds[4]+1,1,{vertical_byte : true}); var addr = 0x3C; if(options) { if (options.address) addr = options.address; // reset display if 'rst' is part of options if (options.rst) digitalPulse(options.rst, 0, 10); } setTimeout(function() { // configure the OLED initCmds.forEach(function(d) {i2c.writeTo(addr, [0,d]);});; }, 50); // if there is a callback, call it now(ish) if (callback !== undefined) setTimeout(callback, 100); // write to the screen oled.flip = function() { // set how the data is to be sent (whole screen) flipCmds.forEach(function(d) {i2c.writeTo(addr, [0,d]);});; var chunk = new Uint8Array(C.OLED_CHUNK+1); chunk[0] = C.OLED_CHAR; for (var p=0; p<this.buffer.length; p+=C.OLED_CHUNK) { chunk.set(new Uint8Array(this.buffer,p,C.OLED_CHUNK), 1); i2c.writeTo(addr, chunk); } }; // set contrast, 0..255 oled.setContrast = function(c) { i2c.writeTo(addr, 0, 0x81, c); }; // set off oled.off = function() { i2c.writeTo(addr, 0, 0xAE); } // set on oled.on = function() { i2c.writeTo(addr, 0, 0xAF); } // return graphics return oled; }; exports.connectSPI = function(spi, dc, rst, callback, options) { update(options); var cs = options?options.cs:undefined; var oled = Graphics.createArrayBuffer(C.OLED_WIDTH,initCmds[4]+1,1,{vertical_byte : true}); if (rst) { //digitalPulse(rst,0,10); for(var i = 0; i<10; i++){ digitalWrite(rst,0); digitalWrite(rst,1); } } setTimeout(function() { if (cs) digitalWrite(cs,0); // configure the OLED digitalWrite(dc,0); // command spi.write(initCmds); digitalWrite(dc,1); // data if (cs) digitalWrite(cs,10); // if there is a callback, call it now(ish) if (callback !== undefined) setTimeout(callback, 10); }, 50); // write to the screen oled.flip = function() { // set how the data is to be sent (whole screen) if (cs) digitalWrite(cs,0); digitalWrite(dc,0);// command spi.write(flipCmds); digitalWrite(dc,1);// data spi.write(this.buffer); if (cs) digitalWrite(cs,1); }; // set contrast, 0..255 oled.setContrast = function(c) { if (cs) digitalWrite(cs,0);//cs.reset(); spi.write(0x81,c,dc); if (cs) digitalWrite(cs,1);//cs.set(); }; // set off oled.off = function() { if (cs) digitalWrite(cs,0);//cs.reset(); spi.write(0xAE,dc); if (cs) digitalWrite(cs,1);//cs.set(); }; // set on oled.on = function() { if (cs) digitalWrite(cs,0);//cs.reset(); spi.write(0xAF,dc); if (cs) digitalWrite(cs,1);//cs.set(); }; // return graphics return oled; };
-
In SSD1306.js, connectSPI() has some codes not work well on ESP32, such as digitalPulse(rst,0,10), cs.reset() and cs.set() .
Just rewrite some lines:
cs.reset() --> digitalWrite(cs,0)
cs.set() --> digitalWrite(cs,1)
digitalPulse(rst,0,10) --> for(var i = 0; i<10; i++){ digitalWrite(rst,0); digitalWrite(rst,1); }
:-)The OLED module works well.
But, the initial action of SPI and Graphics module must be in the onInit() function. why?
And the RST pin must be connected! The cs option must be defined too.var g; function start(){ // write some text g.drawString("Hello Espruino!", 2, 2); // write to the screen g.flip(); } function onInit(){ SPI2.setup({sck:18, mosi:23}); g = require("mySSD1306").connectSPI(SPI2, 16 /*DC*/, 12 /*RST*/, start, {cs:17}); } save();
-
-
SPI1.setup({sck:14, miso:12, mosi:13}); E.connectSDCard(SPI1, 15); console.log(require("fs").readdirSync());
result:
Uncaught Error: Function "connectSDCard" not found!
at line 1 col 3
E.connectSDCard(SPI1,15);
^
WARNING: Module "fs" not found
Uncaught Error: Field or method "readdirSync" does not already exist, and can't create it on undefined
at line 1 col 26
console.log(require("fs").readdirSync());
=undefined
FirmVer: 1v91 Copyright 2016 G.Williams -
-
-
Thanks!
yes.
But, when flash a new firmware, I have to upload/save the js file again.
Can some js file be added AS A PART of the firmware , and can be flashed(not upload or save) to the ESPs, and be executed when the ESPs boot/reboot.
When we change the js file, we make a new firmware that contains the js file, and then we flash or OTA the ESPs. -
eg. digitalPulse(A0,1,5); pulses A0 high for 5ms. digitalPulse(A0,1,[5,2,4]); pulses A0 high for 5ms, low for 2ms, and high for 4ms.
It seems that digitalPulse(rst,0,10) sets rst pin( rst = 1) , and then resets it for 10ms(rst = 0), at last sets it again(rst = 1).
Yet, it doesn't work.